In code below:
map<string,vector<int>> create(ifstream& in, const vector<string>& vec)
{
/*holds string and line numbers into which each string appears*/
typedef map<string,vector<int>> myMap;
typedef vector<string>::const_iterator const_iter;
myMap result;
string tmp;
unsigned int lineCounter = 0;
while(std::getline(in,tmp))
{
const_iter beg = vec.begin();
const_iter end = vec.end();
while (beg < end)
{
if ( tmp.find(*beg) != string::npos)
{
result[*beg].push_back(lineCounter);//THIS IS THE LINE I'M ASKING FOR
}
++beg;
}
++lineCounter;
}
return result;
}
How should I do it (check line commented in code) if I want to use insert method of map instead of using operator[]?
Thank you.
Seriously, I would not do it.
You are only going to complicate your code unnecessarily. You would need a call to the insert to generate the new element in the map and then modify it.
Just for the sake of it (avoiding the double lookup, but building an unnecessary empty vector):
EDIT: Real equivalent (functionality and performance):