stoi是否在此范围内声明?

时间:2014-10-27 15:49:22

标签: c++ string gcc

我正在尝试使用stringint转换为stoi(),但我收到错误error: ‘stoi’ was not declared in this scope。这是给定的代码。

#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';
}

1 个答案:

答案 0 :(得分:4)

stoi来自C ++ 11,您应该尝试使用atoi