簡體   English   中英

檢查相同的列表元素是否包含真或假

[英]Check if same list element contains true or false

假設我有一個像這樣的列表std::list<MyClass> myList

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

而這個 boolean 值來自 function bool isExpired(MyClass &a)

如何檢查一組元素是否具有相同或不同的 boolean 值? 例如,在這種情況下 1000 應該是假的,因為列表中的第二個 1000 具有假值。

這是假的

1000    true
1000    false //because of this
1000    true

這是假的

2000    false
2000    false

這是真實的

3000    true
3000    true

我試圖創建一個新的 map 它將覆蓋鍵和值。

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

但它沒有按預期工作。 它為 ID 為 1000 的元素返回true

你不想使用|| ,您想使用&& ,這意味着您必須默認為 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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM