How to correctly resize a complex vector?

241 Views Asked by At

I have a class like so:

class A {
    std::vector<
        std::vector<
            std::pair<
                uint32_t, std::reference_wrapper<std::vector<uint8_t>>>>>
        table;
};

In the constructor of the class I resize the vector, like so table.resize(indexSize)

but when I try to push items in I get an error:

void A::insertItem(const uint32_t &g, const vector<uint8_t> &point)
{
    this->table[g % this->indexSize].push_back(make_pair(g, point));
}

I guess when I resize I need to pass a constructor of some sort?

The error I get is:

no instance of overloaded function "std::vector<_Tp, _Alloc>::push_back [with _Tp=std::pair<uint32_t, std::reference_wrapper<std::vector<uint8_t, std::allocator<uint8_t>>>>, _Alloc=std::allocator<std::pair<uint32_t, std::reference_wrapper<std::vector<uint8_t, std::allocator<uint8_t>>>>>]" matches the argument list -- argument types are: (std::pair<uint32_t, std::vector<uint8_t, std::allocator<uint8_t>>>) -- object type is: std::vector<std::pair<uint32_t, std::reference_wrapper<std::vector<uint8_t, std::allocator<uint8_t>>>>, std::allocator<std::pair<uint32_t, std::reference_wrapper<std::vector<uint8_t, std::allocator<uint8_t>>>>>>

1

There are 1 best solutions below

0
On

This example is functional:

#include <vector>
#include <functional>

int main(int, const char**) {
    std::vector<uint8_t> abc = {5, 6};
    std::vector<
        std::vector<
            std::pair<uint32_t, std::reference_wrapper<std::vector<uint8_t>>>
            >
        > table;
    table[0].push_back(std::make_pair(4, std::reference_wrapper<std::vector<uint8_t>>(abc)));
    return 0;
}

The second argument of your pair have a constructor that needs an argument.