How to update recently entered element in std::vector<pair<int, int>>?

How can one update the value for pairs in any vector class of pair type?

Example :

V.push_back(make_pair(1, 3));

If I wish to update 3 to say 5 or something, how do I achieve that?

Assuming that you want to update the last std::pair input just after inserting to the std::vector<std::pair<int, int>>.

In c++17 you can make use of second overload of std::vector::emplace_back, which returns a referance to the element inserted:

#include <vector>

std::vector<std::pair<int, int>> vec;
auto &pair = vec.emplace_back(1, 3); // construct in-place and get the reference to the inserted element
pair.second = 5;                     // change the value directly like this

Update:

In c++11, the same can be achieved by the std::vector::insert member, which returns iterator pointing to the inserted element.

#include <vector>

std::vector<std::pair<int, int>> vec;
// insert the element to vec and get the iterator pointing to the element
const auto iter = vec.insert(vec.cend(), { 1, 3 });
iter->second = 5; // change the value

Last element of vector in C++ (Accessing and updating , #include "Person.h" using std::vector; using std::unique_ptr; using std:: make_unique; char secondname[maxlength]; while(true) { std::cout << "Enter a first name or press Enter to The first change in main() from Ex10_02 is in the definition  std::vector<int> vecOfInts { 1, 4, 5, 22, 33, 2, 11, 89, 49 }; Let’s add all elements in initialisation list to the existing vector i.e. // Insert all elements from initialization_list to vector at 3rd position auto iter2 = vecOfInts.insert(vecOfInts.begin() + 3, {34,55,66,77}); Contents of vecOfInts will be now,

You access a value within your vector and just set the values you want to change. Assuming you have mutable access to your vector.

V.back().first = 1;
V.back().second = 2;

If you know the index of the item within the vector, you can use operator[] or at to get a reference to the item. You can also copy a new value into the same position.

V[0] = std::make_pair(3, 5);

Ivor Horton's Beginning Visual C++ 2013, vector::push_back #include <iostream> #include <vector> int main () { std::vector < int > myvector; int myint; std::cout << "Please enter some integers (enter 0 to  1) The new element is initialized as a copy of value. 2) value is moved into the new element. If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated.

If i is the index in the std::vector containing the std::pair you wish to update:

vec.at(i).second = 5;

Also note that std::pair overrides the = operator so you can assign the entire pair again with:

vec.at(i) = std::make_pair(val1, val2);

vector::push_back - C++ Reference, This separation between the plant and the controller is standard in modern input vector u as a vector with elements corresponding to all the parts that enter as  Insertion or removal of elements at the end - amortized constant O(1) Insertion or removal of elements - linear in the distance to the end of the vector O(n) std::vector (for T other than bool) meets the requirements of Container, AllocatorAwareContainer, SequenceContainer, ContiguousContainer (since C++17) and ReversibleContainer.

Flexible Manufacturing Systems: Recent Developments, An iterator that points to the first of the newly inserted elements. 20 2 3 4 5 --it; // it points now to the second 20 ^ std::vector< int > myvector (2,30); mylist.insert  std::vector<int> v; v.push_back(999); std::vector<int>::reverse_iterator r = v.rbegin(); std::vector<int>::iterator i = r.base(); // will point to the last // element in the sequence Element access We have seen the subscript operator [] that provides unchecked access and the member function at() , which will throw an object of type std::out_of

list::insert - C++ Reference, Unlike std::array whose length is specified at the time of declaration and remains constant till compile time, we can change the length of std::vector dynamically  MS’s std::vector is pretty close to useless. I wasn’t as decisive until recently, when several things happened: (1) I discovered gcc’s implementation does not suffer from the same issue, so it’s nothing inherent in the c++ standard. (2) I manually modified vector::resize’s signature to accept a const reference, and the code compiled

C++ std::vector : declare, initialize, functions of vector, etc, std::vector<int> vecOfNums { 1, 4, 5, 22, 33, 2, 11, 89, 49 }; position pos and returns the iterator pointing to position first newly added element. I entered the ID# 3 to be removed as a test. This loops until it correctly finds the object that has the ID number I enter but this is the output it gives: ID# not found ID# not found ID# not found ID# not found It stops there because in my program, 3 is the 4th ID number on the list. Can anyone see what's wrong with my code? Why is it doing that?

Comments
  • Would probably suggest using back() in the example given that the OP sort of suggests it's the most recently push_back()'d element they want to modify ... but this is really nitpicking
  • Oh man you really are nit-picking @LightnessRacesinOrbit ;-).