如標題,程式為C++為何會這樣?
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<string>& split(const string &s, char delim, vector<string> &elems) {
stringstream ss(s);
string item;
while (getline(ss, item, delim)) {
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim) {
vector<string> elems;
split(s, delim, elems);
return elems;
}
void printVectorAsArray(const vector<string> arr, int r, int c) {
// print row
for (int i=0; i<r; i++) {
// for each row, print element
for (int j=0; j<c; j++) {
if (j!=0) {
cout << " ";
}
cout << arr[i*c+j];
}
cout << endl;
}
}
void printTrans(const vector<string> arr, int r, int c) {
// print col
for (int i=0; i<c; i++) {
// for each col, print element
for (int j=0; j<r; j++) {
if (j!=0) {
cout << " ";
}
cout << arr[j*c+i];
}
cout << endl;
}
}
int main() {
int r, c;
char delim = ' ';
while (cin >> r >> c) {
cin.get();
// initialize input array
vector<string> arr;
// read r rows
for (int i=0; i<r; i++) {
string s;
vector<string> elems;
// get a row
getline(cin, s);
elems = split(s, delim);
// push row into arr
for (vector<string>::iterator it=elems.begin(); it != elems.end(); ++it) {
arr.push_back(*it);
}
}
// printVectorAsArray(arr, r, c);
printTrans(arr, r, c);
}
}