#include <string>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#pragma warning(disable:4786)
#pragma warning(disable:4715)
#pragma warning(disable:4390)
class  UseinFind
{
public:
UseinFind(string &val):m_val(val)
{
};
bool operator()(const pair<string,int> & value)
{
string temp=value.first;
string::size_type pos=temp.find(m_val,0);
if(pos!=string::npos)
return true;
else
return false;
}
protected:
private:
string m_val;
};
int main()
{
multimap<string,int> stringmap;
stringmap.insert(make_pair(string("aaa"),1));
stringmap.insert(make_pair(string("aaa"),2));
stringmap.insert(make_pair(string("bab"),3));
stringmap.insert(make_pair(string("ccd"),4));
multimap<string,int>::iterator it;
for (it=stringmap.begin();it!=stringmap.end();++it)
{
cout<<(*it).first.c_str()<<' ';
}
cout<<endl;
multimap<string,int>::iterator mapit2;
string key="a";//查找关键字中有字母a
mapit2=find_if(stringmap.begin(),stringmap.end(),UseinFind(key));
if(mapit2!=stringmap.end())
{
cout<<"had find !"<<endl;
while(mapit2!=stringmap.end())
{
cout<<mapit2->first<<' ';
++mapit2;
}
}
else
{
cout<<"No find !"<<endl;
}
return 0;
}

解决方案 »

  1.   

    循环一下吧:)#include "stdafx.h"
    #include <string>
    #include <map>
    #include <iostream>
    #include <algorithm>
    using namespace std;class  UseinFind
    {
    public:
    UseinFind(string &val):m_val(val)
    {
    };
    bool operator()(const pair<string,int> & value)
    {
    string temp=value.first;
    string::size_type pos=temp.find(m_val,0);
    if(pos!=string::npos)
    {
    cout << value.first << endl;
    return true;
    }
    else
    return false;
    }
    private:
    string m_val;
    };
    int main()
    {
    multimap<string,int> stringmap;
    stringmap.insert(make_pair(string("aaa"),1));
    stringmap.insert(make_pair(string("aaa"),2));
    stringmap.insert(make_pair(string("bab"),3));
    stringmap.insert(make_pair(string("ccd"),4));
    string key="a";//查找关键字中有字母a
    for_each (stringmap.begin(),stringmap.end(),UseinFind(key)); return 0;
    }