例如輸入:
1 2 3
4 5 6
怎麼用cin輸入,最好能保存為vector>類型的二維向量。
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
void input_vector (vector<vector<int>>&);
int main ()
{
vector<vector<int>> vec;
input_vector (vec);
vector<vector<int>>::iterator it = vec.begin ();
for (; it != vec.end (); ++it) {
vector<int>::iterator i = it->begin ();
while (i != it->end ()) {
cout << *i++ << " ";
}
cout << endl;
}
return 0;
}
void input_vector (vector<vector<int> >& vec)
{
vector<int> v;
while (!cin.eof()) {
int tmp(0);
v.clear ();
string line;
getline(cin, line);
istringstream ss(line);
while (ss>>tmp) {
v.push_back (tmp);
}
vec.push_back (v);
}
}