简体   繁体   中英

How Iterator works in C++

I am learning container in C++ and trying to insert and print element of list with the help of iterator. I am getting a different output then my expectations.

#include<iostream>
#include<list>
#include<vector>
#include<deque>
#include<string>

using namespace std;

int main()
{
    list<int> ilist(10);
    cout<<ilist.size()<<endl;
    list<int>::iterator litr = ilist.begin();
    int i =0;
    for(litr = ilist.begin();litr != ilist.end();litr++,i++)
    {
        cout<<"i:"<<i<<" ";
        //ilist.push_back(i);
        *litr=i;
        litr++;
    }
    litr = ilist.begin();
    cout<<endl;
    cout<<ilist.size();
    while(litr != ilist.end())
    {
        i=*litr;

        cout<<i<<endl;
        litr++;
    }

    return 0;
}

output : 10 i:0 i:1 i:2 i:3 i:4 100 0 1 0 2 0 3 0 4 0

why size changed after Inserting element and why elements are not properly inserted ?

Thanks in advance.

In the first loop, you are incrementing the iterator twice per iteration (once in the for statement and once more at the bottom of the loop).

This causes the iterator to skip every other element.

The size of the list remains unchanged: the 100 is 10 immediately followed by 0 . The 10 is the size of the list and the 0 is the first element of the list. Print a space or a newline after printing out the size and you'll see for yourself.

In this loop

for(litr = ilist.begin();litr != ilist.end();litr++,i++)
{
    cout<<"i:"<<i<<" ";
    //ilist.push_back(i);
    *litr=i;
    litr++;
}

you are increasing iterator litr twice: in the control statement and within the body of the loop. It could be written simpler

list<int>::iterator litr;
int i = 0;

for ( litr = ilist.begin(); litr != ilist.end(); litr++, i++ )
{
    cout<<"i:"<<i<<" ";
    *litr = i;
}

Or

list<int>::iterator litr;
int i = 0;

for ( litr = ilist.begin(); litr != ilist.end(); litr++ )
{
    cout<<"i:"<<i<<" ";
    *litr = i++;
}

As for the output then that it would be more clear you need to insert std::endl after printing the size the second time. For example

cout<<ilist.size() << endl;
//                 ^^^^^^^
while(litr != ilist.end())
{
    i=*litr;

    cout<<i<<endl;
    litr++;
}

Then the output will look like

10 
i:0 i:1 i:2 i:3 i:4 
10
0 0 1 0 2 0 3 0 4 0

Take into account that there are several standard algorithms declared in header <algorithm> that can do the same task. For example you could use algorithm std::generate .

#include <algorithm>

//...

int i = 0;
std::generate( ilist.begin(), ilist.end(), [&] { return i++; } ); 

Size did not change. Before second loop you display size of the list and then it's contents without any separator. Since the first element in the list is equal to 0 it looks as if the list grew to the length of 100. Also, like NPE has mentioned, in the first loop you increment the iterator twice.

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