C++ 如何在C++;?
我必须将整数输入到整数数组。我还必须在输入中识别换行符。 更清楚地说,我举了一个例子。 我的意见是:-C++ 如何在C++;?,c++,C++,我必须将整数输入到整数数组。我还必须在输入中识别换行符。 更清楚地说,我举了一个例子。 我的意见是:- 2 3 4 45 6 78 45 34 34 我想根据输入中的换行符处理输入 编程语言是C++,编译器是G++。我不想在数组中存储换行符;我只想进行相应的处理。您可以使用std::getline from来读取整行,并使用std::stringstream,from来解析这些行。您可以使用std::getline from来读取整行,并使用std::stringstream,从解析线条。
2
3
4
45
6
78
45
34
34
我想根据输入中的换行符处理输入
编程语言是C++,编译器是G++。我不想在数组中存储换行符;我只想进行相应的处理。您可以使用std::getline from来读取整行,并使用std::stringstream,from来解析这些行。您可以使用std::getline from来读取整行,并使用std::stringstream,从解析线条。虽然你提到C++,但是这里我经常使用的是从文件中的双倍数组中读取的东西。
char line[512+1];
unsigned int row = 0 ;
unsigned int col = 0 ;
while( fgets(line, sizeof(line), file) )
{
col = 0 ;
tempChr = strtok(line, delimters);
// ignore blank lines and new lines
if ( tempChr[0] == '\n' )
{
continue ;
}
tempDbl = atof(tempChr);
data[row*numCols+col] = tempDbl ;
for(col = 1 ; col < numCols ; ++col)
{
tempDbl = atof(strtok(NULL, delimters));
data[row*numCols+col] = tempDbl;
}
row = row + 1;
if( row == numRows )
{
break ;
}
}
char行[512+1];
无符号整数行=0;
无符号整数列=0;
while(fgets(行、sizeof(行)、文件))
{
col=0;
tempChr=strtok(生产线、交货线);
//忽略空行和新行
如果(tempChr[0]='\n')
{
持续
}
tempDbl=atof(tempChr);
数据[行*numCols+col]=tempDbl;
for(col=1;col <>代码> < p>虽然这里提到C++,但我经常使用的是从文件中的双倍数组中读取的内容。
char line[512+1];
unsigned int row = 0 ;
unsigned int col = 0 ;
while( fgets(line, sizeof(line), file) )
{
col = 0 ;
tempChr = strtok(line, delimters);
// ignore blank lines and new lines
if ( tempChr[0] == '\n' )
{
continue ;
}
tempDbl = atof(tempChr);
data[row*numCols+col] = tempDbl ;
for(col = 1 ; col < numCols ; ++col)
{
tempDbl = atof(strtok(NULL, delimters));
data[row*numCols+col] = tempDbl;
}
row = row + 1;
if( row == numRows )
{
break ;
}
}
char行[512+1];
无符号整数行=0;
无符号整数列=0;
while(fgets(行、sizeof(行)、文件))
{
col=0;
tempChr=strtok(生产线、交货线);
//忽略空行和新行
如果(tempChr[0]='\n')
{
持续
}
tempDbl=atof(tempChr);
数据[行*numCols+col]=tempDbl;
for(col=1;col
省略错误检查
错误检查回避。
< P> ISTRAM提取操作符将自动跳过输入中的空行,因此下面示例中的Read TysSo()函数将返回输入流中空白(包括新行)分隔的向量。
#include <vector>
#include <iostream>
using namespace std;
vector<int>
read_ints(istream & is)
{
vector<int> results;
int value;
while ( is >> value )
{
results.push_back(value);
}
return results;
}
int
main(int argc, char * argv[])
{
vector<int> results = read_ints(cin);
for ( vector<int>::const_iterator it = results.begin(); it != results.end(); ++it )
cout << *it << endl;
return 0;
}
#包括
#包括
使用名称空间std;
矢量
读取内容(istream&is)
{
矢量结果;
int值;
while(是>>值)
{
结果:推回(值);
}
返回结果;
}
int
main(int argc,char*argv[])
{
矢量结果=读数(cin);
for(vector::const_迭代器it=results.begin();it!=results.end();++it)
CUT< P> ISTRAM提取操作符将自动跳过输入中的空行,因此下面示例中的Read TysSo()函数将返回输入流中的空白(包括新行)分隔的向量。
#include <vector>
#include <iostream>
using namespace std;
vector<int>
read_ints(istream & is)
{
vector<int> results;
int value;
while ( is >> value )
{
results.push_back(value);
}
return results;
}
int
main(int argc, char * argv[])
{
vector<int> results = read_ints(cin);
for ( vector<int>::const_iterator it = results.begin(); it != results.end(); ++it )
cout << *it << endl;
return 0;
}
#包括
#包括
使用名称空间std;
矢量
读取内容(istream&is)
{
矢量结果;
int值;
while(是>>值)
{
结果:推回(值);
}
返回结果;
}
int
main(int argc,char*argv[])
{
矢量结果=读数(cin);
for(vector::const_迭代器it=results.begin();it!=results.end();++it)
我想你必须澄清你的问题。输入是2“\n”3“\n”4“\n”“\n”“\n”45“\n”6“\n”等等..但是你想得到什么样的数据结构?我在问题中说我想输入一个整数数组..我想你必须澄清你的问题。输入是2“\n”3“\n”4“\n”“\n”“\n”45“\n”6“\n”等等..但您希望最终得到什么样的数据结构?我在问题中说过,我希望输入到整数数组中。。