c++ - atoi() 没有按预期工作

标签 c++ std atoi

#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
•
• //main func declaration etc...
•
//Vectors for storing information from file
vector<string> include;
vector<string> exclude;
string temp; //for storing whatever the stream is on
int len = atoi(puzzle_file >> temp); //first pos
int width = atoi(puzzle_file >> temp); //second pos

上面的代码应该读入一个文件并将数字存储在相应的整数中。尽管我的文件头中有#include <\cstdlib> 和#include <\stdlib.h>,但我收到一条错误消息“没有匹配函数来调用‘atoi’”。不确定从这里去哪里。在 stackoverflow 和其他论坛上做了一些研究,找不到任何真正帮助我的东西。有什么建议吗?谢谢

最佳答案

你应该使用 stoi而不是 atoi .

stoistd::string 作为参数,而 atoiconst char* 作为参数。

并且不要忘记 stoi 是自 c++11 以来的新内容。

关于c++ - atoi() 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42849750/

相关文章:

c++ - 在 C++ 中实现弱侵入指针

c++ - 为什么STL集合大小的复杂度是O(1),它是如何计算的?

c++ - 当我们尝试从 hash_set 中删除不存在的键时会发生什么

random - 从数组中采样 N 个元素的函数

c++ - 如何使用 SIMD 实现 atoi?

c++ - 显示多个壳对象的属性表

c++ - 具有默认模板参数的转换运算符

c++ - 构造函数 : init() method, 指针中的异常,大型 try/catch 或..?

c - 为什么 atoi() 的结果没有出现在我的程序的输出中?

c - 了解 atoi(var-1) 与 atoi(var)-1?