C++ 读取未知长度的数字
我有一个输入文件,其中包含坐标模式下的一些数据 例如,(2,3,5)转换为第2列、第3行和第5级。我对使用getline(cin,string)获取数据后读取数字的方法很好奇。我不知道数据点中有多少位数字,所以我不能假设第一个字符的长度为1。是否有任何库可以帮助更快地解决问题 我的游戏计划到目前为止还没有完成C++ 读取未知长度的数字,c++,string,pointers,char,C++,String,Pointers,Char,我有一个输入文件,其中包含坐标模式下的一些数据 例如,(2,3,5)转换为第2列、第3行和第5级。我对使用getline(cin,string)获取数据后读取数字的方法很好奇。我不知道数据点中有多少位数字,所以我不能假设第一个字符的长度为1。是否有任何库可以帮助更快地解决问题 我的游戏计划到目前为止还没有完成 void findNum(string *s){ int i; int beginning =0; bool foundBegin=0; int end=0; bool foundEnd=
void findNum(string *s){
int i;
int beginning =0;
bool foundBegin=0;
int end=0;
bool foundEnd=0
while(*s){
if(isNum(s)){//function that returns true if its a digit
if(!foundBegin){
foundBegin=1;
beginning=i;
}
}
if(foundBegin==1){
end=i;
foundBegin=0;
}
i++;
}
}
试试这个:
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <string>
int main() {
std::vector <std::string> params;
std::string str;
std::cout << "Enter the parameter string: " << std::endl;
std::getline(cin, str);//use getline instead of cin here because you want to capture all the input which may or may not be whitespace delimited.
std::istringstream iss(str);
std::string temp;
while (std::getline(iss, temp, ',')) {
params.push_back(temp);
}
for (std::vector<std::string>::const_iterator it=params.begin(); it != params.end(); ++it) {
std::cout << *it << std::endl;
}
return 0;
}
输出:
1
2
3
解析这些参数后,您可以通过以下方法将其从字符串转换为(示例)整数:
template <typename T>
T convertToType(const std::string &stringType) {
std::stringstream iss(stringType);
T rtn;
return iss >> rtn ? rtn : 0;
}
1 , 2, 3 , 4
这将产生:
1
2
3
4
#包括
无效findNums(常量字符串和str、int&i、int&j、int&k)
{
std::stringstream ss(str);
字符c;
ss>>c>>i>>c>>j>>c>>k;
}
jrd1的答案很好,但如果您愿意,C标准库(cstdlib)中已经有了将字符转换为整数(或返回)的函数。你在找阿托伊
只需使用提取器运算符即可读取相应变量类型中的任何类型的值
#incude<ifstream> // for reading from file
#include<iostream>
using namespace std;
int main()
{
int number;
ifstream fin ("YouFileName", std::ifstream::in);
fin >> number; // put next INT no matter how much digit it have in number
while(!fin.eof())
{
cout << number << endl;
fin >> number; // put next INT no matter how much digit it have in number and it will ignore all non-numeric characters between two numbers as well.
}
fin.close();
return 0;
}
#incude//用于从文件读取
#包括
使用名称空间std;
int main()
{
整数;
ifstream-fin(“YouFileName”,std::ifstream::in);
fin>>number;//输入下一个INT,不管它在数字中有多少位数
而(!fin.eof())
{
cout number;//无论下一个INT在数字中有多少位数,它都会忽略两个数字之间的所有非数字字符。
}
fin.close();
返回0;
}
查看更多详细信息
注意:在将其用于字符数组和字符串时要小心……) 什么是
string
使while(*s){
好吗?我希望string*s指向调用函数的字符串。因此,只要指向一个字符,while循环就会运行。你读到了这个吗?你的意思是想把字符串变成“(2,3,5)”并将其解析为三个数字?是的,我应该首先发送三个参数,以便在找到它们后通过引用进行传递以复制它们。为什么它们需要非空格分隔?它们不需要以已知的方式统一分隔(即,作为getline分隔符的任何内容)@Namfuak:关于getline
,你是对的。我的示例之所以必须用空格分隔是因为cin
。我将对其进行编辑以反映更正确的版本。哦,我明白你的意思。如果OP出于任何原因需要空间支持,使用cin.getline()可能更有意义(例如,如果他想允许像“1,2,3”这样的输入。)@Namfuak:cin.getline()
需要一个固定的长度,以便确定何时停止捕获。getline
在这里更合适,因为它可以捕获无限量的空格分隔输入(新行除外)-考虑到问题的具体情况,我认为这在这里更合适。但是,谢谢你的建议!
1
2
3
4
#include <sstream>
void findNums(const string &str, int &i, int &j, int &k)
{
std::stringstream ss(str);
char c;
ss >> c >> i >> c >> j >> c >> k;
}
#incude<ifstream> // for reading from file
#include<iostream>
using namespace std;
int main()
{
int number;
ifstream fin ("YouFileName", std::ifstream::in);
fin >> number; // put next INT no matter how much digit it have in number
while(!fin.eof())
{
cout << number << endl;
fin >> number; // put next INT no matter how much digit it have in number and it will ignore all non-numeric characters between two numbers as well.
}
fin.close();
return 0;
}