jeudi 30 avril 2015

Iterating a huge std::vector of std::vectors and modifying elements

I have to iterate a huge std::vector vec, it is defined as follows:

std::vector<std::vector<int>> vec;

Since I have to modify elements of vec multiple times depends on flag I have to create another vector as follows:

std::vector<std::vector<int>> eVecTemp;

And how I am populating eVecTemp is shown below:

for(auto &x: vec)
 {
    std::vector<int> pers_vec(x);
    int arry[]={4,5,6,7}
    while(i < 4)
    {
        int candidate=arr[i];   

        if( !flag )
        {
            x.push_back(candidate);
            flag=true;                        
        }
        std::vector<int> new_vec(pers_vec);
        new_vec.push_back(candidate);
        eVecTemp.emplace_back(new_vec);
    }
    ++i;
}

I feel there may be better implementation possible for the following snippets, any suggestions please.

std::vector<int> new_vec(pers_vec);
new_vec.push_back(candidate);
eVecTemp.emplace_back(new_vec);

Aucun commentaire:

Enregistrer un commentaire