Atoi() 未按预期工作
atoi() not working as expected
#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
上面的代码应该读取一个文件并将数字存储在相应的整数中。我收到一个错误,说"调用'atoi'没有匹配函数",即使我的文件头中有 #include <\cstdlib>和 #include <\stdlib.h>。不知道从这里去哪里。对stackoverflow和其他论坛进行了一些研究,找不到任何真正帮助我的东西。有什么建议吗?谢谢
你应该使用stoi而不是atoi。
stoi
采用std::string
作为参数,而atoi
采用const char*
作为参数。
不要忘记stoi
自 c++11 以来是新的。
puzzle_file >> temp
表达式返回istream
,但没有接受这种参数的atoi
重载。
你应该打电话给atoi(temp.c_str());
您尝试跳过一条指令并丢失了。puzzle_file >> temp
返回puzzle_file
而不是temp
.因此,您将atoi
应用于转换为布尔值的输入流。用:
int len, width;
puzzle_file >> len >> width;
if (! puzzle_file)...
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- Atoi() 未按预期工作
- Atoi 转换分段错误在 Windows 上工作,但在 Ubuntu 中不起作用
- atoi() 在C++中如何工作