c++ - stoi 没有在此范围内声明?

标签 c++ string gcc

<分区>

我正在尝试使用 stoi()string 转换为 int 但我收到的错误是 error: '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';
}

最佳答案

stoi 来自 C++11,你应该试试 atoi

关于c++ - stoi 没有在此范围内声明?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26591446/

相关文章:

java - java字符串如何不可变?

ios - 如何在Swift中正确使用NSRegular Expression和过滤字符串数组

c++ - 从编译器获取参数时,有没有办法避免预处理器宏?

c++ - 在 C++ 中检查哈希的大小

c++ - 模板约束 C++

c++ - 赛通 : exposing C++ classes with nested typedef (s)

c++ - 派生类中的抽象类实现

c# - 在 XML 中查找字符串

linux - 构建 GCC 4.6 - libmpfr.so.4 - 无法打开共享对象文件

php - 如何开始用 C++ 编写 PHP5 扩展