The Vector find function can search an inserted value even if you try and find by providing substring . Is this same in case of the keys of a map ?
int main()
{
vector<string> v;
v.push_back("text");
v.push_back("[[text");
v.push_back("text");
for (unsigned i = 0; i < v.size(); i++)
{
if (v[i].find("[[") == 0)
{
v[i].append("]]");
}
}
}
Here it finds “[[text” and makes it “[[text]]” .
I tried it in map but code crashes on run time ,. I am using Dev c++
int main()
{
std::multimap<string, string> m;
std::multimap<string, string> intersection;
std::multimap<string, string>::iterator it8;
m.insert(pair< string, string>("4-2"," 61-7" ));
m.insert(pair< string, string>("5-2"," 61-7" ));
multimap<string, string>::iterator it4;
multimap<string, string>::iterator it5;
for ( it4 = m.begin(); it4 !=m.end(); it4++)
{
if (m.find("5-") == 0)
it5=it4;
}
cout << " result of 5 search is" << (*it5).first << ", " << (*it5).second <<endl;
m.clear();
getchar();
}
(Sample correct code on ideone)
The error is that this line
will never succeed. You maybe be surprised at this.
m.find("5-")searches through the entire map looking for an entry whose key is exactly equal to “5-“. Your keys are “4-2” and “5-2”.Do you wish to find keys which contain the substring “5-“? Then you need something like
I think you want a loop like this:
As others have pointed out, you are not interested in the the
findmethod ofvectorormap. You are interested in the find method ofstring– this is quite different. The title of your question is a little misleading as a result (unintentionally).