Originally Posted by
Masterx
how is it possible to search a string in a mapped array?
Yes, but it is slow since you would be performing a linear search, e.g.,
Code:
#include <iostream>
#include <map>
#include <string>
#include <algorithm>
template<typename K, typename V>
class MatchSecond
{
public:
MatchSecond(const V& value) : value(value) {}
bool operator()(const std::pair<K,V>& element) const
{
return element.second == value;
}
private:
V value;
};
using namespace std;
int main()
{
map<int,string> mymap;
mymap[2] = "hello";
mymap[3] = "world";
mymap[5] = "today";
// Search for "world" in mymap.
map<int,string>::iterator iter = find_if(mymap.begin(), mymap.end(), MatchSecond<int,string>("world"));
if (iter != mymap.end())
{
cout << "'world' is at " << iter->first << endl;
}
else
{
cout << "'world' could not be found" << endl;
}
}