Search code examples
c++oopiteratorstd-pairpush-back

modifying vector at same iterator


Suppose i have the following vector:

std::vector<std::pair <int, bool> > myV;
std::vector<std::pair <int, bool> >::iterator it;

And then i initialize them for 10 row:

 for (i = 0 ; i < 10; i++){
           if (i % 2 == 0 )
                j = true;
            else 
                j = false
           myV.push_back(std::make_pair(i,j));
  } //end of for

Now , i need to modify my vector at node 5, or each node, one way is : i get backup from it, and erase it, and then push_back, But for a specific reason , i need to keep order of my vector, do you have any idea?


Solution

  • Vectors give random access; you can simply reassign the element:

    myV[5] = std::make_pair(42, true);
    *it = std::make_pair(35, false);