【问题标题】:stoi is not declared in this scope? [duplicate]stoi 没有在这个范围内声明? [复制]
【发布时间】:2014-10-27 15:49:21
【问题描述】:

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

【问题讨论】:

  • 您必须使用与 C++11 兼容的 libc++ 实现。

标签: c++ string gcc


【解决方案1】:

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

【讨论】:

  • 如果他有选择的话,strtolatoi 更好,尽管那是 C99 库。
猜你喜欢
  • 2018-07-12
  • 1970-01-01
  • 2015-08-20
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多