Talk:cpp/string/basic string/stol
I would like to understand how the stoi function work and if there is a specific library stuff I need \to add to get the function to run. When I try the code bellow, my compilor said stoi is not a member of std. What am I doing wrong?
#include <iostream> #include <string> int main() { std::string str1 = "45"; std::string str2 = "3.14159"; std::string str3 = "31337 with words"; std::string str4 = "words and 2"; int myint1 = std::stoi(str1); int myint2 = std::stoi(str2); int myint3 = std::stoi(str3); // error: 'std::invalid_argument' // int myint4 = std::stoi(str4); std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n'; std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n'; std::cout << "std::stoi(\"" << str3 << "\") is " << myint3 << '\n'; //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n'; }
- The program compiles and runs. Based on the error, it's likely that your compiler is either too old to support C++11 or is not configured to do so. --Cubbi (talk) 14:12, 24 November 2014 (PST)
[edit] What is pos set to if the whole string is numeric?
The current description doesn't specify what happens to pos if the whole string is numeric. It simply says pos is set to the index of the first unconverted character. If there are no unconverted characters, then this sentence is meaningless. We know that the implementation of the string class makes the data end with a null character, which would indeed be the "first unconverted character", but the null is conceptually not part of the string itself, so it is invalid to say that it has an index within the string.
- the description is not referring to the implementation of the string class, it is referring to the string obtained from str.c_str(). Let's see if it can be made clearer. --Cubbi (talk) 06:42, 8 October 2015 (PDT)
[edit] Is there a reason for not using nullptr for pointers (instead of 0)?
Since C++11, we have the `nullptr` literal for null pointers. I see here that 0 is still being used. Is that because it's like that in the reference? --Alexis Wilke (talk) 14:48, 10 June 2018 (PDT)
[edit] string_view and future standards
Why is there no overload for string_view? Did it simply not make it in time, or has it deliberately been decided not to provide such an overload? - Søren Nissen