简体   繁体   中英

Pushback to vector inside static vector

Question about static vector and pushbacks...

The idea is one vector storing a pair with a string and other vector...

[0] foo1: bar1,bar2... [1] foo2: bar3,bar4...

I declared in h file this vector:

class thing
{
public:
...
static std::vector<std::pair<std::string, std::vector<std::string>>> things;
...
}

In the cpp file:

vector<pair<string,vector<string>>> thing::things;

I can create the first entry of the vector with a simple:

typedef pair<string,vector<string>> p;
p P;
P.first = foo;
P.second.push_back(bar1);

But, when i try to update the inner pair vector with:

typedef pair<string,vector<string>> p;

BOOST_FOREACH(p P, thing::things)
{
    if(P.first==foo)
    {
        P.second.push_back(bar2);
    }
}

The thing::things vector it's not updated with the second value...

Any help?

Thanks!

If you want to modify the elements, then you need a reference :

BOOST_FOREACH(p & P, thing::things)
{
    if(P.first==foo)
    {
        P.second.push_back(bar2);
    }
}

Like you used it, it creates a copy of each element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM