简体   繁体   English

遍历C ++中的自定义结构列表的小问题

[英]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? 我正在尝试打印出由带有迭代器的结构组成的列表,但由于迭代器无法指向我的worker.index(),所以我无法执行此操作,我是否知道自己在做错什么?

I am getting 我正进入(状态

[Error] 'std::list::iterator' has no member named 'index'. [错误]“ std :: list :: iterator”没有名为“ 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. 它知道如何使用包含焊料的std :: list。 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 . 如果是这种情况,应该告诉您,迭代器可以访问容器中的对象,而访问该对象的唯一方法是使用魔术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 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. 如果您打算成为一名熟练的c ++程序员,则必须学习该语言和标准库的工作方式。

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循环

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

Live demo: https://wandbox.org/permlink/ybiYdbGhcQEfYizH 现场演示: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM