Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combining map and array of string c++

Tags:

c++

arrays

map

I'm writing a code to check if a person have visited a particular place before or not if true do nothing else 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 follow

#include<map>
using namespace std;

map<string,string[]> stu_plac;
map<string,string[])::iterator it;

I could not find the roght 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??

like image 841
user1749118 Avatar asked Dec 20 '22 14:12

user1749118


1 Answers

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.

like image 71
shirley Avatar answered Dec 23 '22 03:12

shirley