1 /** 2 start: integer; // begins hear 3 stop: integer; // ends here 4 s: string; 5 c: char; // temp 6 **/ //测试数据 7 #include<iostream> 8 #include<sstream> 9 #include<string> 10 #include<vector> 11 using namespace std; 12 13 vector<string> txt[1314]; 14 string code, tmp; 15 int max_len[200]; //储存每列最大的字符串长度 16 17 void print(const string& s, int len, char extra) 18 { 19 cout << s; 20 for (unsigned int i = 0; i <= len-s.length(); i++) 21 cout << extra; 22 } 23 24 int main() 25 { 26 int cols = 0, row = 0; //列数, 行数 27 while (getline(cin, code)) 28 { 29 istringstream ss(code); //istringstream从string对象中读取 30 while (ss >> tmp) { 31 max_len[cols] = max(max_len[cols], (int)tmp.size()); //求出每行的每列上的最大字符串长度 32 cols++; //列数++ 33 txt[row].push_back(tmp); 34 } 35 row++; cols = 0; //行数++, 列数归零 36 } 37 for (int i = 0; i < row; i++) 38 { 39 unsigned int j = 0; 40 for ( ; j < txt[i].size() - 1; j++) 41 { 42 print(txt[i][j], max_len[j], ' '); //输出单个字符串,长度不够的输出空格 43 } 44 cout << txt[i][j] << endl; //每行每列 45 } 46 return 0; 47 }
这里有一篇关于:istringstream, ostringstream, stringstream的文章,感觉不错.
http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html