使用c+使用迭代器、容器和向量从文件中读取特定数据+; 我想学习如何使用迭代器、容器和向量读取C++中的一个特定文件。
这是一个没有迭代器的特定读取的示例使用c+使用迭代器、容器和向量从文件中读取特定数据+; 我想学习如何使用迭代器、容器和向量读取C++中的一个特定文件。,c++,C++,这是一个没有迭代器的特定读取的示例 string rId; string classes; string rId1; string classes1; fstream file("resultInfo.txt"); cout<<"Registration Id:"; cin>>rId1; cout<<"Class:"; cin>>classes1; std::string theSubject;
string rId;
string classes;
string rId1;
string classes1;
fstream file("resultInfo.txt");
cout<<"Registration Id:";
cin>>rId1;
cout<<"Class:";
cin>>classes1;
std::string theSubject;
double theResult;
double sum =0;
int count = 0;
cout <<"\n\n PRINTING RESULTS\n\n\n";
while(file>>classes>>rId>>theSubject>>theResult){
if(rId==rId1){
if(classes==classes1){
cout <<theSubject<<" "<<theResult<<endl;
sum +=theResult;
count++;
}
else{
cout <<"CLASS NOT FOUND"<<endl;
break;
}
}
}
string-rId;
字符串类;
字符串rId1;
字符串类1;
fstream文件(“resultInfo.txt”);
coutrId1;
coutclasse1;
std::字符串主题;
双重结果;
双和=0;
整数计数=0;
cout classes>>rId>>主题>>结果){
如果(rId==rId1){
如果(类==classes1){
在“迭代器”版本中,实际上根本不需要迭代器或向量,您可以使用ss>>classes>>rId>>theSubject>>结果与上一个版本非常相似。然后您可以像第一个版本一样直接比较值。好的,谢谢,我将尝试一下
ifstream
file("studentInfo.txt");
string content;
cout<<endl<<"\t\t\t\t !** STUDENT LIST **!\t\t\n\n\n";
cout << setw(15) <<"REG.ID:" << setw(15) << "F.NAME" << setw(15) << "L.NAME" <<setw(15) << "CLASS"<<setw(15)<<"M.NUMBER"<< endl<<endl;
string line;
while(std::getline(file, line))
{
istringstream ss(line);
std::istream_iterator<std::string> begin(ss), end;
//putting all the tokens in the vector
std::vector<std::string> arrayTokens(begin, end);
vector<string>::iterator it;
for(it = arrayTokens.begin(); it != arrayTokens.end(); it++)
{
cout<<setw(15)<<*it;// prints d.
}
std::cout << '\n';
}