I need to get the first character of an std::string with a minimum amount of code.
It would be great if it would be possible to get the first char in one line of code, from an STL std::map<std::string, std::string> map_of_strings. Is the following code correct:
map_of_strings["type"][0]
EDIT
Currently, I am trying to use this piece of code. Is this code correct?
if ( !map_of_strings["type"].empty() )
ptr->set_type_nomutex( map_of_strings["type"][0] );
The prototype of the set_type function is:
void set_type_nomutex(const char type);
It’s not exactly clear from your question what your problem is, but the thing likely to go wrong with
map_settings["type"][0]is that the returned string may be empty, resulting in undefined behavior when you do[0]. You have to decide what you want to do if there is no first character. Here’s a possibility that works in a single line.It gets the first character or a default character.