dabs dabs - 1 month ago 10
C++ Question

Passing three parameter in map shows error

I want to pass three parameters in map in C++. It shows error while using insert function. Is there any alternate method for this? I want to make pair of ((integer integer) string)

int main()
{
map < map <int, int>, char[50] > Code;

int TestCases,Queries,a,b;
char Name[50];
scanf("%d",&TestCases);

for (int i = 0; i < TestCases; i++)
{
scanf("%d %d %s",&a,&b,Name);
Code.insert(make_pair(make_pair(a,b),Name[50]));// Error Line
}
}


I get the following error:


C2664: 'void std::_Tree
<
std::_Tmap_traits
<
_Kty,_Ty,_Pr,_Alloc,false
>>
::insert(std::initializer_list<const _Kty,_Ty
>>
)' : cannot convert argument 1 from 'std::pair
<
std::pair
<
int,int
>
,char
>
' to 'std::pair
<
const _Kty,_Ty
>


Answer

Judging by the usage, it looks like you really want the key to be a pair of ints, not a map of int to int, like so:

std::map< std::pair<int,int>, char[50] > Code;

Furthermore, if you are storing a string in a map, it's preferable to use the C++ string class instead of a C char array.

std::map< std::pair<int,int>, std::string > Code;
Comments