简体   繁体   中英

Small problem iterating through a list of custom struct in C++

I am trying to print out my list made up of structs with iterators, but I am unable to do so as the iterator is unable to point to my soldier.index(), may I know what I am doing wrong?

I am getting

[Error] 'std::list::iterator' has no member named 'index'.

#include <iostream>
#include <list>
#include <algorithm>

using namespace std;

struct Soldier
{
    int index;
    bool isAlive = true;
};

int main()
{
list<Soldier>company;
list<Soldier>::iterator it;


int N;
int number_of_reports;

cin >> N;
for(int i = 0; i < N; i++)
{
    Soldier soldier;
    soldier.index = i;
    company.push_back(soldier);
}

cin >> number_of_reports;
while(number_of_reports--)
{
    for(it = company.begin(); it != company.end(); it++)
    {
        cout << it.index() << endl;
    }
}
return 0;
}

This is actually a tough question. I don't know the curriculum of your class, but I can see that an introduction to the standard library has a lot of nuances.

An iterator is a full blown object. It really does not know anything about your Soldier structure. It knows how to work with the std::list that contains your Solders. It is full of overloaded operators. And at this point I can see where the waters get muddy. So you have not learned yet about operator overloading but you are going to use use it? You are expected to treat the iterator like a black box so you must follow certain rules blindly. If this is the case, you should have been told that an iterator has access to your objects in the container and the only way to get to that object is with a magic it->thing .

If you have learned about overloading it should have been pointed out that the -> is an iterator overload. the operator returns a pointer to your object. Or, at the least, you could just think of the iterator as a raw pointer, it looks the same. Have you been introduced to pointers yet?

And, as it has come up.

for( auto& item : container )...;

Is a language construct. It simply does the same as:

for (it = company.begin(); it != company.end(); it++)
{
    auto& item= *it;
    std::cout << item.index << std::endl;
}

And there it is again, treating the iterator like a pointer and this time using the asterisk operator.

https://en.cppreference.com/w/cpp/language/operator_member_access#Built-in_indirection_operator

If you are going to be a proficient c++ programmer you have to learn how the language and the standard library work.

Also, please get out of the habit of:

using namespace std;

它应该是it.index() it->index而不是it.index()

The neatest way to do this is with a ranged for loop :

for (auto& s : company)
{
    cout << s.index << endl;
}

Live demo: https://wandbox.org/permlink/ybiYdbGhcQEfYizH

I think you want to do this:

for (list<Soldier>::iterator it = company.begin(); it != company.end(); ++it)
{
    cout << it->index << endl;
}

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