....
string CityName;
string StreetName;
GetName(CityName,StreetName);
typedef vector<string> StrName;
map<string,StrName>cityMap;
map<string,StreetName>::iterator iterMap;
iterMap=cityMap.find(CityName);
if(iterMap==cityMap.end())
{
cityMap.insert((std::make_pair(CityName,StrName.push_back(StreetName)));
}
else
{
//I want to insert value to the corresponding cityname or the returned iterator
}
I'm getting compilation error while i'm trying to insert the value. Also once the values are inserted i need to retrieve them using simple for loop.How to do that in this case.
Kindly help
Problem in inserting values in map
Hi
My current problem stmt is :- There are n no. of cities each having unique name and each city having m no. of streets. I have all the information as in which street belong to which city and so on. I'm able to extract information from the given input file using the function GetName now i want to store this information in some data structure. My current implementation goes like
Quote:Original post by tonyaim83
typedef vector<string> StrName;
map<string,StrName>cityMap;
map<string,StreetName>::iterator iterMap;
See the problem?
I m able to remove the compilation now.But still i have problem in inserting values for the cities whose name is already existing.
<br>....<br>string CityName;<br>string StreetName;<br>GetName(CityName,StreetName);<br>typedef vector<string> StrName;<br>map<string,StrName>cityMap;<br>map<string,StreetName>::iterator iterMap;<br>iterMap=cityMap.find(CityName);<br>if(iterMap==cityMap.end())<br>{<br> StrName.clear();<br> StrName.push_back(StreetName);<br> cityMap.insert((std::make_pair(CityName,StrName)));<br>}<br>else<br>{<br> //I want to insert value to the corresponding cityname or the returned iterator<br>}<br><br></code><br><br>How to write code for the else part
You've got the iterator to the pair in the map. The first value in the pair is the city name; the second value is the vector of street names... to which you want to append a new street name, correct?
The code should write itself at this point.
The code should write itself at this point.
The potentially missing piece of information: iterators are used just like pointers, so 'it->second' refers to the vector found in the map.
But really, you are making this too complicated. The operator[] of the map does exactly what you want:
(BTW, the forum "code" tag needs to be in lowercase.)
But really, you are making this too complicated. The operator[] of the map does exactly what you want:
string CityName;string StreetName;GetName(CityName, StreetName);typedef vector<string> StrName;map<string, StrName> cityMap;cityMap[CityName].push_back(StreetName);// If the CityName is not found, the operator[] will automatically insert it // with a default-constructed value (i.e. an empty vector). Then it returns a // reference to the value (whether it was already there, or just created now).// So we just add the StreetName to that vector; in either case, it does what// we want.
(BTW, the forum "code" tag needs to be in lowercase.)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement