简体   繁体   English

迭代结构列表

[英]Iterating over list of structs

I'm creating a list of structs: 我正在创建一个结构列表:

struct task{
    int task_id;
    bool is_done;
    char* buffer;
    int length;

} task;
list<task> taskList;

And trying to iterate over the tasks in order to check the is_done status: 并尝试迭代任务以检查is_done状态:

    for (std::list<task>::const_iterator iterator = taskList.begin(), end = taskList.end(); iterator != end; ++iterator) {

        if(iterator->is_done) {
            return 1;
        } else {
            return 2;
        }
    }

Where am I wrong? 我哪里错了? I get: Missing template argument before '->' token 我得到:在' - >'令牌之前缺少模板参数

The iterator's operator-> does the dereferencing already. 迭代器的operator->已经解除引用。 So instead of 而不是

if(*iterator->is_done==true)

you need 你需要

if(iterator->is_done==true)

is equivalent to 相当于

if((*iterator).is_done==true)

which as a sidenote is equivalent to the easier to read 作为旁注相当于更容易阅读

if((*iterator).is_done)

or 要么

if(iterator->is_done)

. Even better, you could also use std::any_of : 更好的是,你也可以使用std::any_of

#include <algorithm>

....

if (any_of(begin(taskList), end(taskList), 
    [](task const &t) { return t.is_done; }))
{
    return 1;
} else {
    return 2;
}

Informal note: There is no need to qualify any_of , begin and end with std:: , because taskList is of type std::list<?> , and the C++-compiler will look up those functions in the std -namespace for you already. 非正式注意:没有必要使用std::来限定any_ofbeginend ,因为taskList的类型是std::list<?> ,而C ++编译器会在std -namespace中查找这些函数。

Like this 像这样

if (iterator->is_done==true){

no need for * and -> . 不需要*->

And not the question you asked but 而不是你问的问题

if (iterator->is_done==true) {

is exactly the same as the easier to understand 与更容易理解完全相同

if (iterator->is_done) {

Don't compare booleans to true and false, they already are true and false. 不要将布尔值与真假相提并论,它们已经是真假。

Use std::find_if instead: 请改用std::find_if

#include <algorithm>

...

bool isDone(const task &task)
{
    return task.is_done;
}

...

return std::find_if(taskList.begin(), taskList.end(), isDone) == taskList.end() ? 2 : 1;

Try this. 尝试这个。 Note change to task struct and referencing iterator. 注意更改任务结构并引用迭代器。 (I changed name of iterator - to be more concise - but not actually required). (我更改了迭代器的名称 - 更简洁 - 但实际上并不需要)。 I just think looks less confusing. 我只是觉得看起来不那么混乱。

#include <list>

using namespace std;

struct task{
    int task_id;
    bool is_done;
    char* buffer;
    int length;

};


int main() {

    std::list<task> taskList;
    task task1;
    task1.buffer = "qwerty";
    task1.is_done = true;
    task1.length = 6;
    task1.task_id = 1;
    taskList.push_back(task1);


    for (std::list<task>::const_iterator it = taskList.begin(), end = taskList.end(); 
       it != end; ++it) {
        if((*it).is_done==true)
            return 1;
        else
            return 2;
    }

    return 0;
}

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

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