gpt4 book ai didi

c++ - 如何获取 MAP 中的第一个非 NULL 值?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:13:12 24 4
gpt4 key购买 nike

我有一张STL map ;我想获得 map 中的第一个非 NULL 值;有没有一种有效/快速的方法来做到这一点?

最佳答案

#include <map>
#include <algorithm>
#include <iostream>

using namespace std;

bool IsNotNull(const pair<const int, int>& i)
{
return i.second != 0;
}

int main() {

map<int, int> m;
m[0] = 0;
m[1] = 1;
map<int, int>::const_iterator it = find_if(m.begin(), m.end(), IsNotNull);
cout << it->second << endl;
return 0;

}

Ideone demo

关于c++ - 如何获取 MAP 中的第一个非 NULL 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7195776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com