简体   繁体   中英

find in std::list

Hi I am getting compilation error from the following code. I am not able to figure out:

g++ stl_list_1.cc
stl_list_1.cc: In function \u2018int main()\u2019:
stl_list_1.cc:16: error: \u2018struct std::_List_iterator<int>\u2019 has no member named \u2018clist\u2019
stl_list_1.cc:19: error: \u2018struct std::_List_iterator<int>\u2019 has no member named \u2018clist\u2019
stl_list_1.cc:25: error: no match for call to \u2018(std::list<std::_List_const_iterator<int>, std::allocator<std::_List_const_iterator<int> > >) (int&)\u2019

Code:

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

main()
{
    std::list<int> clist;
    for (int i =0; i<10; i++)
    {
        clist.push_back(i);
    }

    std::list<std::list<int>::const_iterator> list1;

    std::list<int>::iterator itr1;
    itr1 = std::find(clist.begin().clist.end(),1);
    std::list<int>::iterator itr2 ;

    itr2 = std::find(clist.begin().clist.end(),7);

    list1.push_back(itr1);
    list1.push_back(itr2);

    for(int j =0; j< list1.size(); j++)
    {
       int k = *list1[j];
       std::cout << "cvalue " << k <<std::endl;
       j++;
     }
}

Thanks Ruchi

It's a typo, you used a dot instead of a comma:

itr1 = std::find(clist.begin().clist.end(),1);
                              ^

It should be like this:

itr1 = std::find(clist.begin(), clist.end(),1);

You made that mistake in both of your calls to std::find .

In addition, you are trying to use operator[] on a list, which won't work. Instead of:

int k = *list1[j];

You could use:

itr1 = list1.begin();
std::advance(itr1, j);
int k = *itr1;

As JohnB mentioned, the above code is inefficient. Isolated, it's the same, but since you are using this inside a loop, it would be better to just use the list iterators to perform the iteration:

for(itr1 = list1.begin(); itr1 != list1.end(); ++itr1)
{
   int k = *itr1;
   std::cout << "cvalue " << k <<std::endl;
}

Additionally, please bear in mind that

list1[j]

does not work because a list has no index operator so you have to iterate over it.

Suggestion: use a std::vector< int > instead of a std::list< int > .

As other people have noted there's a typo in the find calls ( . instead of , ).

But also once you get past that list doesn't have a [] operator so you'll find that part doesn't work right either. You'll need to use iterators to loop over the list in your final loop.

int k = *list1[j];

That will not compile either.

Maybe something like this might (not checking any safety etc, its a crap way, but just showing)

int k = *(find(clist.begin(), clist.end(), j));

Also you increment j twice, did you mean this?

for(int j =0; j< list1.size(); j++) //here
{
   int k = *list1[j];
   std::cout << "cvalue " << k <<std::endl;
   j++; //and here?
}

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