#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
struct FoundByValue
{
FoundByValue(string str):_s(str) {}
bool operator() (const pair<int, string>& v) const
{
return v.second == _s;
}
private:
string _s;
};
int main(int argc, char** argv)
{
char sz[] = "I am a map of int and string.";
string token;
stringstream ss (sz);
map<int, string> mis;
map<int, string>::iterator itmap;
int i;
i = 0;
while (getline(ss, token, ' '))
{
mis[i++] = token;
}
for (itmap = mis.begin(); itmap != mis.end(); ++itmap)
{
cout << (*itmap).first << " " << (*itmap).second << endl;
}
vector<string> vs;
vs.push_back("int");
vs.push_back("float");
for (vector<string>::iterator it = vs.begin(); it != vs.end(); ++it)
{
itmap = find_if(mis.begin(), mis.end(), FoundByValue(*it));
if ( itmap != mis.end())
cout << *it << " already exists." << endl;
else
cout << *it << " is not found." << endl;
}
return 0;
}
C/C++ map
最新推荐文章于 2023-11-01 16:19:02 发布