首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++里面的泛值函数count_if的使用,该怎么解决

2013-12-22 
C++里面的泛值函数count_if的使用count_if(stuMap.begin(),stuMap.end(),it-second-m_Age24)不知道

C++里面的泛值函数count_if的使用
count_if(stuMap.begin(),stuMap.end(),"it->second->m_Age<=24");
不知道哪里出错误了,麻烦各位高手帮忙!
[解决办法]
最后一个参数不是用字符串来表达,而是应该用函数,参考http://blog.csdn.net/jerryjbiao/article/details/6856975

引用:
count_if(stuMap.begin(),stuMap.end(),"it->second->m_Age<=24");
不知道哪里出错误了,麻烦各位高手帮忙!

[解决办法]
最后一个参数必须是函数,或lambda表达式
#include <vector>
#include <algorithm>
#include <iostream>

bool greater10(int value)
{
return value >10;
}

int main()
{
using namespace std;
vector<int> v1;
vector<int>::iterator Iter;

v1.push_back(10);
v1.push_back(20);
v1.push_back(10);
v1.push_back(40);
v1.push_back(10);

cout << "v1 = ( ";
for (Iter = v1.begin(); Iter != v1.end(); Iter++)
cout << *Iter << " ";
cout << ")" << endl;

vector<int>::iterator::difference_type result1;
result1 = count_if(v1.begin(), v1.end(), greater10);
//lambda表达式
//result1 = count_if(v1.begin(),v1.end(), [](int v)->bool{return v>10;});
cout << "The number of elements in v1 greater than 10 is: "
<< result1 << "." << endl;
}

热点排行