It is impossible to assign a variable of type const char to the variable st .

Tell me how you can solve this problem.

 char s; vector<string> str; map<char, string> encode; while (cin >> s) { encode[s]; } for (const auto &i : encode) { cout << i.first << " " << i.second << endl; } for (auto &i : encode) { string st = i.first; str.push_back(st); } 

    2 answers 2

    In this sentence

     string st = i.first; 

    there is no assignment, but the creation of an object of type std::string from an object of type const char . However, there is no corresponding constructor in the std::string class.

    Use the following entry instead.

     string st( 1, i.first ); 

    or

     std::string st { i.first }; 

    or

     std::string st = { i.first }; 

    In fact, there is no need for a loop to declare a local object st . You can write easier

     for (auto &i : encode) { str.push_back( { i.first } ); } 

      Try not to construct a string , but assign:

       string st; st = i.first; 

      There is no such constructor, but assignment - yes.

      The second option - designers

       string(int,const char) т.е. string st(1,i.first); string(const char*,int) т.е. string st(&i.first,1); 

      So, choose any of the three options :)