简体   繁体   中英

Check if same list element contains true or false

Let's say that I have a list std::list<MyClass> myList like this:

ID      Valid
--------------
1000    true
1000    false
1000    true
2000    false
2000    false
3000    true
3000    true

And this boolean value comes from a function bool isExpired(MyClass &a) .

How can I check if one group of elements have equal or different boolean values? For example in this case 1000 should be false because second 1000 in list has false value.

This is false

1000    true
1000    false //because of this
1000    true

This is false

2000    false
2000    false

This is true

3000    true
3000    true

I tried to create a new map which will override the key and value.

std::map<long, bool> status;
for(const auto &it : myList)
{
   status[it.ID] = status[it.ID] || isExpired(it); 
}

But it does not work as expected. It returns true for the element with an ID 1000.

You don't want to use || , you want to use && , which means that you have to default to true:

std::map<long, bool> status;
for(const auto &it : myList)
{
    auto entry = status.find(it.ID);
    if (entry != status.end()) {
        entry->second = entry->second && isExpired(it);
    } else {
        status[it.ID] = true;
    }
}

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