2

Please take a look at the MRE below:

#include <iostream>
#include <vector>
#include <iterator>

void display(const std::vector<std::string> &v){
    for(int i = 0; i < v.size(); i++){
        std::cout << v[i] << " ";
    }
    std::cout << std::endl;
}

int main(){
    std::vector<std::string> vect1;
    std::vector<std::string>::iterator i1 = vect1.begin();

    vect1.push_back("Test");
    display(vect1);
    std::cout << *i1 << std::endl;
    vect1.insert(i1, "Apple");
    std::cout << vect1.size() << std::endl;
    display(vect1);

    return 0;
}

When I try to add a new element to vect1 with use of insert function the vector content remains the same (according to display function output). Is there anything I'm missing here?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
  • 1
    `push_back` can invalidate all iterators if reallocation is necessary. "If the new size() is greater than capacity() then all iterators and references (including the past-the-end iterator) are invalidated. Otherwise only the past-the-end iterator is invalidated." from https://en.cppreference.com/w/cpp/container/vector/push_back – Retired Ninja Nov 25 '22 at 17:21
  • 1
    Once you have invalidated your iterator, dereferencing the iterator or incrementing the iterator can cause fun and interesting things to happen. It'd be nice if iterators would detect when they've become invalidated, however that's not what they do. – Eljay Nov 25 '22 at 17:22
  • @interjay, I don't really think any debugging details are required here. OP provided comprehensive MRE, precise tags and provided his intention. The only problem is poor English grammar, but the question is still understandable – The Dreams Wind Nov 25 '22 at 17:30
  • @TheDreamsWind The question didn't originally say what goes wrong, which is always required. You seem to have added that information yourself in an edit. – interjay Nov 25 '22 at 17:52
  • @interjay i did, but what the original question contained also had quite clear intention (at least to me): `I am trying to add a new element in vector using iterator and trying to display the element using iterator but can't` – The Dreams Wind Nov 25 '22 at 17:55
  • @TheDreamsWind If you're going to edit to *say* that the code is an MRE, please make sure that it's an *actual* MRE. The for loop and `cin`s were not necessary to reproduce the problem. I've edited it to turn it into an actual MRE. – Donald Duck Nov 26 '22 at 10:57

1 Answers1

2

The iterator

i1=vect1.begin();

becomes invalid after the for loop where new elements are added to the vector provided that the value of size is greater than 0. Otherwise this statement

cout<<*i1<<endl;

will invoke undefined behavior.

Move this statement after the for loop.

i1=vect1.begin();

if ( !vect1.empty() ) cout<<*i1<<endl;

cout<<endl<<endl<<"***Inserting the string (Apple) At Index 1"<<endl;
vect1.insert(i1,"Apple");

Pay attention to that the message

"***Inserting the string (Apple) At Index 1"

is incorrect. As the iterator it points to the first element of the vector then the string is stored at index 0. That is you should use message

"***Inserting the string (Apple) At Index 0"
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335