Converting a single char to std::string prepends x01

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Converting a single char to std::string prepends x01



I'm trying to receive values from an unordered map that has std::string as keys, where some of these strings contain only a single character. All my input is coming from a std::stringstream from which I get each value and cast it to a char, and then converting it to a string using
std::string result 1, character;, which seems to be valid according to the documentation and this answer.


std::string


std::stringstream


std::string result 1, character;



However, when I'm doing so the string gets a x01 prepended (which corresponds to the value 1). This makes the string to not be found in the map. My debugger also confirms that the string is of size 2 with the value "x01H".



Why is this happening and how can I fix it?


#include <iostream>
#include <sstream>
#include <unordered_map>

int main()

const std::unordered_map<std::string, int> map = "H", 1, "BX", 2 ;

std::stringstream temp "Hello world!";
char character = static_cast<char>(temp.get()); // character is 'H'
std::string result 1, character; // string contains "x01H"

std::cout << result << " has length " << result.size() << std::endl; // H has length 2
std::cout << map.at(result) << std::endl; // unordered_map::at: key not found




1 Answer
1



You are using braces , not parenthesis (), unlike the question you link to. That makes it an initializer list, not the constructor you were expecting.



()





Aah, such a simple mistake. Thanks for the help!
– Ted Klein Bergman
Aug 13 at 6:08





Ah, so it's reading 1 as another character ('x01')?
– melpomene
Aug 13 at 6:09


1


'x01'





@melpomene exactly.
– kabanus
Aug 13 at 6:09





Again another example why one should not use uniform initialisation... Stick with parentheses, that's more explicit; I personally go that far to even use parentheses with initialiser lists: std::string s('a', 'b', 'c'). (Admitted, there's the problem with trying to explicitly calling the default constructor actually declaring a function instead, but that rule is far less complicated...).
– Aconcagua
Aug 13 at 6:17



std::string s('a', 'b', 'c')





@Aconcagua I got in a habit using it due to this question (and the highly upvoted answer), but I feel that I'll try to use parenthesis more often from now on
– Ted Klein Bergman
Aug 13 at 6:37






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered