stl的map使用find_if时最后的函数怎么写啊

huqitu 2005-06-29 10:47:16
我用map<string,int>的方法建立的map,但是不知道find_if的第三个参数的函数怎么写啊,请帮忙啊
...全文
592 8 打赏 收藏 转发到动态 举报
写回复
用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,为什么啊?
不过先给分了吧,谢谢各位。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧