0

I would have expected the following code to produce an error:

    #include <iostream>
    using namespace std;
    #include <map>
    #include <string>
    int main(){
       map<string,string> x; 
       x["10"]=20;
       cout <<x["10"]<<endl;
    }

because the assigned value is not of type string. However, there is no error. But the output is incorrect -- nothing visible is printed. However, if I enclose 20 in quotes then the output is fine. Can anyone explain

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Ekalavya
  • 969
  • 1
  • 9
  • 14
  • 2
    You can shorten this to `int main() { string s; s = 20; cout << s;`} - the map is unimportant. Then look at [the 4th assignment operator](https://en.cppreference.com/w/cpp/string/basic_string/operator%3D) for `std::string` and try values like 65, 66, 67, ... Then examine an ASCII table. – molbdnilo Feb 25 '20 at 13:11
  • 20 is casted to a one-character string – Neywat Feb 25 '20 at 13:13
  • 1
    @Neywat A single character is not a one-character string. – molbdnilo Feb 25 '20 at 13:15
  • Thanks to both. I assume this is as per the C++ standard. But I would have expected the cast to produce "20". Is there a reason why it produces a single character, presumably of ASCII value 20? – Ekalavya Feb 25 '20 at 13:15
  • 3
    @Ekalavya An `int` can be implicitly converted to `char`; `char` is an integer type. (And there is no casting, but an implicit conversion. Casting is an explicit conversion.) – molbdnilo Feb 25 '20 at 13:17
  • [Don't](https://stackoverflow.com/a/1452738/4730685) do `using namespace std;`, _especially_ not before other `#include` directives. If you must use it, do it after all the `#include`s. – Erlkoenig Feb 25 '20 at 14:26
  • Yes, I see now that an int is naturally viewed as a char and hence naturally cast to a string. – Ekalavya Feb 25 '20 at 17:11
  • What is missing by the given answer? Do you need more information or is something wrong with it? It simply ask because you did not accept the answer... – Klaus Feb 26 '20 at 09:54
  • I upvoted the answers I liked, and my question has been answered satisfactorily. How do I "accept" ? I would like to accept the remarks made by molbdnilo and close this conversation for myself. – Ekalavya Feb 27 '20 at 02:44
  • @Ekalavya: Simply press the check mark under the vote buttons. BTW: If you answer to a comment, you should add a @ to your comment, so the person will be informed. – Klaus Feb 28 '20 at 09:23

1 Answers1

2

std::string has 3 versions of operator= ( C++03 ). One of them takes a single char. If you present s = 65; it takes the int as a char and calls the string& operator= (char c); version. std::string creates a single character string in this case.

The 3 versions in C++03 are:

string& operator= (const string& str);
string& operator= (const char* s);
string& operator= (char c);

For current standard we have some more, see std::string::operator=

Example: Prints out "A" because 65 is in ASCII the upper case A.

int main()
{   
    std::string s;
    s = 65; 
    std::cout << s << std::endl;
}

Your observation is not related to use in combination with the map, so I reduced it to the use of std::string.

Klaus
  • 24,205
  • 7
  • 58
  • 113