我试图基本上粘贴一个文本块作为输入,并使其正常工作。我实际上已经使用外部文本文件使其工作,但未能使用cin使其工作。以下是工作示例。正如您所看到的,我希望每个新行都是函数输入的新元素,这是很多麻烦所产生的。
int uaid;
string name;
float gpa;
int i = 0;
ifstream infile("student.txt");
while (infile >> uaid >> name >> gpa) {
students[i].Set(uaid, name, gpa);
i+=1;
}
答案 0 :(得分:0)
您可以使用Istream_Iterator stl.Continously添加到字符串向量
#include<iostream>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
int main()
{
vector<string> v;
istream_iterator<string> start_cin(cin);
istream_iterator<string> end_of_cin;
copy(start_cin,end_of_cin,back_inserter(v));
cout<<"Pressed ctrl D"<<endl;
cout<<"Below is the continous string"<<endl;
copy(v.begin(),v.end(),ostream_iterator<string>(cout," "));
}
使用CTRL + D(对于mac终止)
string 1
string 2 value
1 2 3 4 \n \d
everything is there
Pressed ctrl D
Below is the continous string
string 1 string 2 value 1 2 3 4 \n \d everything is there Program ended with exit code: 0