开发者

how to get the first non NULL value in a MAP?

开发者 https://www.devze.com 2023-03-31 07:49 出处:网络
i have an STL map ; i would like to get the first non NULL value in the map; is there an efficie开发者_StackOverflownt/quick way to do that?#include <map>

i have an STL map ; i would like to get the first non NULL value in the map; is there an efficie开发者_StackOverflownt/quick way to do that?


#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


There's nothing quicker than just looping through and finding what you're looking for

for (map<X,Y>::const_iterator i = m.begin(); i != m.end(); ++i)
{
  if (i->second != NULL)
  {
    // do something with first non-NULL value
    break;
  }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消