The following code gives error.when you pass maps through function.and run loop over it.Kindly help

Write your question here.
The following code gives error.when you pass maps through function.and run loop over it.Kindly help.

#include<iostream>
#include<string.h>
#include<map>
#include <iterator>
#include <vector>
int search(char c[ ],const std::map < std::string, std::string> &list);
using namespace std;
int main()
{

map <string,string> list;
int count,x; char name[20];
char a[ ][20]={"Ram","Akil","Nikil","Subhash","Karthik"};
char b[ ][20]={"Syam","Syam","Ram","Ram","Akil"};
for(int i=0;i<5;i++)
{
list.insert(a[i],b[i]);
}
cout<<"\n input name";
cin.get(name,20);
cout<<"\n input level";
cin>>x;
if(x==1)
{count=0;
map <string,string> :: iterator it;
for(it=list.begin();it!=list.end();it++)
{ char temp[20];
strcpy(temp,it->second.c_str());
if(strcmp(temp,name)==0)
{
count++;
}

}
cout<<"n total number of children "<<count;
}

if(x==2)
{
count=0;
map <string,string> :: iterator it;
for(it=list.begin();it!=list.end();it++)
{ if(strcmp(it->second.c_str(),name)==0)
{char temp[20];
strcpy(temp,it->second.c_str());
int x =search(temp,list);
count=count+x;
}
}
cout<<"n total number of grand children "<<count;
}


return 0;
}
int search(char c[ ],const std::map < std::string,std::string> &list)
{
int count=0;
const std::map < std::string, std::string> :: const_iterator it;
for(it=list.begin();it!=list.end();it++)
{
if(strcmp(it->second.c_str(),c)==0)
count++;
}

return count;
}





//60 7 C:\c projects\hello.cpp [Error] passing 'const const_iterator {aka const std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >}' as 'this' argument of 'std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >& std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >::operator=(const std::_Rb_tree_const_iterator<std::pair<const std::basic_string<char>, std::basic_string<char> > >&)' discards qualifiers [-fpermissive]
You probably would've got help earlier if you used code tags and spaced your code so that it's readable.

When inserting your elements into the map, you either need to explicitly create a pair, like so:

 
        list.insert(make_pair(a[i], b[i]));

or use emplace, like so:

 
        list.emplace(a[i], b[i]);

And although you want a const_iterator in your search function, you don't actually want the iterator itself to be const, since it needs to be modifiable in order to step through the container. It's best to use auto and cbegin and cend instead of trying to spell it all out yourself.

1
2
3
4
5
6
7
8
9
10
int search(char c[], const map<string,string> &list)
{
    int count = 0;
    for ( auto it = list.cbegin(); it != list.cend(); it++ )	
    {
        if (strcmp(it->second.c_str(), c) == 0)
            count++;
    }
    return count;
}

Also, I don't see why you're using strcmp and C-arrays when you could just use strings and ==.
Topic archived. No new replies allowed.