社区
C++ 语言
帖子详情
stl的map使用find_if时最后的函数怎么写啊
huqitu
2005-06-29 10:47:16
我用map<string,int>的方法建立的map,但是不知道find_if的第三个参数的函数怎么写啊,请帮忙啊
...全文
592
8
打赏
收藏
stl的map使用find_if时最后的函数怎么写啊
我用map的方法建立的map,但是不知道find_if的第三个参数的函数怎么写啊,请帮忙啊
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
8 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
foochow
2005-06-29
打赏
举报
回复
#include "stdafx.h"
#include<map>
#include<algorithm>
#include<iterator>
#include<string>
using namespace std;
typedef map<string,int>maps;
typedef pair<string,int>ps;
struct evens
{
bool operator()(const maps::value_type&x)const
{
if(x.first[0]=='1'&&x.first[1]=='2')return true;
else return false;
}
};
int main()
{
maps temp;
temp.insert(ps("abc",15));
temp.insert(ps("efg",11));
temp.insert(ps("12csd",12));
temp.insert(ps("hello",16));
maps::iterator it;
it=find_if(temp.begin(),temp.end(),evens());
cout<<it->first<<" "<<it->second<<endl;
system("PAUSE");
return 0;
}
huqitu
2005-06-29
打赏
举报
回复
能不能帮我写一个啊,我写的总是编译不通过
map< string, int >::iterator begin = TrMongolianIndex.begin();
map< string, int >::iterator end = TrMongolianIndex.end();
begin = find_if(begin, end, xxxxx);
比如我要找 string包含 "12"开头的所有字符串啊
帮我写一下xxxxx
foochow
2005-06-29
打赏
举报
回复
//这个根据测试条件自己写就可以啊
#include<iostream>
#include<map>
#include<algorithm>
#include<iterator>
#include<string>
using namespace std;
typedef map<string,int>maps;
typedef pair<string,int>ps;
struct evens
{
bool operator()(const maps::value_type&x)const
{
return (x.second&1)==0;
}
};
int main()
{
maps temp;
temp.insert(ps("abc",15));
temp.insert(ps("efg",11));
temp.insert(ps("csd",12));
temp.insert(ps("hello",16));
maps::iterator it;
it=find_if(temp.begin(),temp.end(),evens());
cout<<it->first<<" "<<it->second<<endl;
system("PAUSE");
return 0;
}
jsjjms
2005-06-29
打赏
举报
回复
学海浩荡,顶............................
oyljerry
2005-06-29
打赏
举报
回复
/* || How to find things in an STL list MkII */
#include <string>
#include <list>
#include <algorithm>
class EventIsIn1997 {
public:
bool operator () (string& EventRecord) {
// year field is at position 12 for 4 characters in EventRecord
return EventRecord.substr(12,4)=="1997";
}
};
int main (void) {
list<string> Events;
// string positions 0123456789012345678901234567890123456789012345
Events.push_back("07 January 1995 Draft plan of house prepared");
Events.push_back("07 February 1996 Detailed plan of house prepared");
Events.push_back("10 January 1997 Client agrees to job");
Events.push_back("15 January 1997 Builder starts work on bedroom");
Events.push_back("30 April 1997 Builder finishes work");
list<string>::iterator EventIterator =
find_if (Events.begin(), Events.end(), EventIsIn1997());
// find_if completes the first time EventIsIn1997()() returns true
// for any object. It returns an iterator to that object which we
// can dereference to get the object, or if EventIsIn1997()() never
// returned true, find_if returns end()
if (EventIterator==Events.end()) {
cout << "Event not found in list" << endl;
}
else {
cout << *EventIterator << endl;
}
}
xzgyb
2005-06-29
打赏
举报
回复
用一个函数对象就可,如下
#pragma warning(disable: 4786)
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
using namespace std;
typedef map< string, int > map_type;
class map_find_func {
public:
bool operator()( const map_type::value_type & arg )
{
if ( arg.first == "gyb" ) return true;
return false;
}
};
int
main()
{
map_type m;
m[ "hello" ] = 2;
m[ "gyb" ] = 3;
m[ "asdf" ] = 5;
map_type::iterator it = find_if( m.begin(), m.end(),
map_find_func() );
cout << it->first << " : " << it->second << endl;
return 0;
}
huqitu
2005-06-29
打赏
举报
回复
我如果想传递参数的话怎么传,比如events("12")
这种用啊
huqitu
2005-06-29
打赏
举报
回复
我用vc6编译时出现在很多warning,为什么啊?
不过先给分了吧,谢谢各位。
【
STL
】利用find_if
函数
结合
map
实现模糊查找
string内部的find
函数
本身可以实现对任意子串的查找,也就实现了模糊查找。问题在于必须给
map
的first准确的关键字,否则会查不出来,这就需要对find_if
函数
的谓词
函数
进行改造。 template<class InIt, class Pred...
C++
STL
find与find_if的
使用
STL
的find,find_if
函数
提供了一种对数组、
STL
容器进行查找的方法。
使用
该
函数
,需要加上#include <algorithm> 现在,我们要查找一个list中的数据,通常
使用
find(),vector,deque同理, 如果是c++内置的类型...
[
STL
]
map
按value值查找——find_if的
使用
[
STL
]
map
按value值查找——find_if的
使用
335046781 2012-05-09 15:29:00 1587 收藏 3 原文链接:http://www.cnblogs.com/xufeiyang/archive/2012/05/09/2491871.html 收起 最近是经常
使用
stl
中的
map
,于是就想记...
C++ find_if(
STL
find_if)查找算法详解
find_if() 同 find() 一样,为在输入迭代器所定义的范围内查找单个对象的算法,它...可以按如下方式
使用
find_if() 来查找 numbers 中第一个大于 value 的元素: int value {5}; auto iter1 = std::find_if(std::b
stl
.find_if用法总结
有
时
我们要在
map
、vector容器中查找符合条件的记录,
map
提供一个find...所以一般情况下进行值域的查找,要么自己遍历数据,要么求助于
STL
的find_if
函数
。前种方法我们这里就不赘述了,只讲find_if
函数
。1 -- find_i...
C++ 语言
64,654
社区成员
250,484
社区内容
发帖
与我相关
我的任务
C++ 语言
C++ 语言相关问题讨论,技术干货分享,前沿动态等
复制链接
扫一扫
分享
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
请不要发布与C++技术无关的贴子
请不要发布与技术无关的招聘、广告的帖子
请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下
试试用AI创作助手写篇文章吧
+ 用AI写文章