C++ stoi未在此范围内声明?
我正在尝试使用C++ stoi未在此范围内声明?,c++,string,gcc,C++,String,Gcc,我正在尝试使用stoi()将字符串转换为int,但我得到的错误是错误:未在此范围内声明“stoi”。下面是给定的代码 #include <iostream> #include <string> int main() { std::string str1 = "45"; std::string str2 = "3.14159"; std::string str3 = "31337 with words"; std::string str4
stoi()
将字符串
转换为int
,但我得到的错误是错误:未在此范围内声明“stoi”。下面是给定的代码
#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';
}
#包括
#包括
int main()
{
std::string str1=“45”;
std::string str2=“3.14159”;
std::string str3=“31337带单词”;
std::string str4=“单词和2”;
int-myint1=std::stoi(str1);
int-myint2=std::stoi(str2);
int myint3=std::stoi(str3);
//错误:“std::无效的参数”
//int-myint4=std::stoi(str4);
std::coutstoi
来自C++11,你应该尝试使用atoi
stoi
来自C++11,你应该尝试使用atoi
你必须使用与C++11兼容的libc++实现。你必须使用与C++11兼容的libc++实现。如果他有选择的话,比atoi
更好,尽管这是C99库y、 如果他有选择的话,那就比atoi好,尽管那是C99库。