-2

I want to make a pair of int and string. As:

pair<int,string> car ;
car.first = 10;
car.second = 'Sarim';
cout<<car.first<<endl;
cout<<car.second;

"sarim" is converted into 'm' in output? enter image description here

SARIM
  • 992
  • 3
  • 14
  • 22

3 Answers3

3

Use

car.second = "Sarim";

The second of the pair is an std::string, not a character, hence you can provide an std::string or const char* to be used to construct one.

Note that the single quotes are for characters while the double ones I used make a const char* which can be used to construct an std::string

Demo

asmmo
  • 6,922
  • 1
  • 11
  • 25
2

r.second = 'Sarim'; is wrong. Single quotes are used for character constants and what you have there is not a single character. You want

r.second = "Sarim";

for a multiple character literal.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
1

A string in C++ is marked using double-quotes ("), the single quotes mark a character (hence only one character is stored). The warning is here to tell you that your “character” is too big (and indeed since it's supposed to be a string).

rajdakin
  • 74
  • 6