文件中的C++cin取消数字的空格
C++ cin from a file unspaced numbers
在c++中,如何生成一个不按数组分隔的数字文件?
例如78940725450327458,我如何才能将这些数字放置为list[0]=7,list[1]=8,list[2]=9,依此类推。
std::vector<int> numList;
while(std::cin) {
char c;
std::cin >> c;
if(std::cin.eof())
break;
if(c < '0' || c > '9') {
// handle error
}
numList.push_back(c - '0');
}
将列表列为char
s的数组。
CCD_ 2将读取单个字符。
我建议您将所有行读取到int变量中,然后使用以下内容进行循环:
int temp = a % 10;
每次都会给你最后一个数字,之后一定要更新原始数字,最后一件事就是把它放进数组,所以这是容易的部分。
有很多方法可以做到这一点。我可能会做以下事情:
#include <algorithm>
#include <fstream>
#include <iterator>
#include <vector>
int ascii2int(int value) // this could be a lambda instead
{
return value - '0';
}
int main()
{
std::vector<int> nums;
std::ifstream input("euler8Nums.txt");
if (input)
{
// read character digits into vector<int>
nums.assign(std::istream_iterator<char>(input), std::istream_iterator<char>());
// transform ascii '0'..'9' to integer 0..9
std::transform(nums.begin(), nums.end(), nums.begin(), ascii2int);
}
// your code here
return 0;
}
相关文章:
- std::regex:匹配由数字和空格组成的字符串,并提取数字.如何?
- 如何从文件中读取用空格分隔的数字
- 使用正则表达式C++计算数字中的十进制空格
- 有没有办法在带有空格C++源代码中写入大量数字以使其更具可读性?
- 使用 Get 的 C++ 输入被替换为空格、不正确的数字
- 不理解连续数字之间的空格代码
- C++ [std::regex] 尝试仅匹配中间有空格的数字
- 用递增的数字替换字符串中的空格
- 从文件中读取带有空格和数字数据的文本
- 如何从用户那里获得输入字符串而不是示例,然后计算空格、标点符号、数字和字母.C++
- 我有n个空格,在每个空格中,我可以放置一个数字0到m.编写一个程序来输出所有可能的结果.需要帮助:)
- 如果 C++ stdlib 流的解析数字紧跟非空格字符,如何引发错误
- 在 cpp 中使用 getline 输入数组中的空格分隔数字
- 如果字符串中没有空格,如何解析带有数字和字符的字符串并分隔数字和字符
- 从字符串中读取单词C++,同时忽略空格、数字和符号
- 如何将包含空格和数字的句子从大写转换为小写
- 扫描以空格分隔的数字
- C++ - 如何将空格分隔的数字输入数组
- 如何将 std::string 中的所有非字母字符(数字和特殊字符)替换为空格
- 计算字符串中尾随空格的数字