samedi 16 mai 2015

How to initialize std::vector of std:pair of fixed length with default values?

Assuming a struct containing a std::vector with items of type std::pair.

struct block {
    std::vector<std::pair<key_t, value_t>> items;
    block(const Data& d) : items()
    {
        items = std::vector<std::pair<key_t, value_t>>(d.size_);
    }
};

Later in the code, I assign a value to the vector at position 0:

block<key_t, value_t> b(*this);
std::pair<key_t, value_t> item = std::pair<key_t, value_t>(k, v);
b.items[0] = item;

Later in the code, I want to iterate over the vector and expect &bucket_item != NULL to be true only at position 0, because I only assigned a value at this position. In fact, &bucket_item != NULL is always true.

for (std::pair<key_t, value_t> item : b.items)
{
    if (&item != NULL)
    {
       ...
    }
}

I am not able to initialize the vector with NULL values like so:

items = std::vector<std::pair<key_t, value_t>>(d.size_, NULL);

How to solve this?

Aucun commentaire:

Enregistrer un commentaire