Reputation: 57
I'm writing a code to check if a person has visited a particular place before or not. If true, do nothing. Otherwise, add the new place to the list of visited place I'm using map to store the student name as a key and array the store places as follows,
#include<map>
using namespace std;
map<string,string[]> stu_plac;
map<string,string[]>::iterator it;
I could not find the right way to search across the map.
I tried the following:
bool exist=false;
if(stu_plac.count(name))
{
it=stu_plac.find(name);
for(auto tt=(*it).second.begin(); tt!=(*it).second.end(); tt++)
{
if (tt.compare(place)==0)//exist
{
exist=true;
}
break;
}
if (!exist)
{
(*it)second[++tt]=place;
}
}
else
{
stu_plac.insert(pair<string,string[]>(name,place));
}
I think the problem is with the array of string iterator. Can you please help me to find the correct way to do this. Do I need to use multimap or other data structer to do this??
Upvotes: 4
Views: 5322
Reputation: 7072
I wouln't use an array because I can't know it's size, I prefer using a vector or a list, and instead of using a boolean variable I would insert and then break the loop, so my algorithm would be like:
std::map<std::string, std::list<std::string> > stu_plac;
std::map<std::string, std::list<std::string> >::iterator it;
std::string myName("Edu");
std::string myPlace("Madrid");
.....
for( it = stu_plac.begin(); it != stu_plac.end(); it++){
if(it->first == myName){
std::list<std::string>::iterator place = std::find(it->second.begin(), it->second.end(), myPlace);
if(place == it->second.end()){
it->second.push_back(myPlace);
break;
}
}
}
Look that you can use the iterator you got to add the city you want.
By the way, I wouldn't use "auto" variable if it's not C++11.
Upvotes: 2
Reputation: 392911
It looks like you wanted a map of string -> places
typedef map<string,std::set<string> > Map;
Adapting it for your snippet (which had many problems, starting with the confusing formatting...):
#include<map>
#include<set>
#include<string>
using namespace std;
typedef map<string,std::set<string> > Map;
typedef Map::iterator It;
int main()
{
bool exist=false;
Map stu_plac;
string name = "name";
string place = "0.0.0.0";
It it = stu_plac.find(name);
if (stu_plac.end() != it)
{
std::set<string>& visits = it->second;
exist = visits.end() != visits.find(place);
if (!exist)
visits.insert(place);
}
}
But in reality you may prefer to use std::multimap
Upvotes: 4
Reputation: 2016
An array in C++ is one of the types or constructs inherited from C so the built-in array "type" doesn't have any iterators. It also doesn't track its current size so you cannot get begin and end iterators of an array without also storing its size.
Use std::map< std::string, std::vector<std::string> >
instead.
Upvotes: 0
Reputation: 76
Data structure like map<string, vector<string> >
will work.
You can use for (size_t i = 0; i < it->second.size(); i++)
to traverse the vector.
Upvotes: 5