/**
start: integer; // begins hear
stop: integer; // ends here
s: string;
c: char; // temp
**/ //测试数据
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
using namespace std; vector<string> txt[];
string code, tmp;
int max_len[]; //储存每列最大的字符串长度 void print(const string& s, int len, char extra)
{
cout << s;
for (unsigned int i = ; i <= len-s.length(); i++)
cout << extra;
} int main()
{
int cols = , row = ; //列数, 行数
while (getline(cin, code))
{
istringstream ss(code); //istringstream从string对象中读取
while (ss >> tmp) {
max_len[cols] = max(max_len[cols], (int)tmp.size()); //求出每行的每列上的最大字符串长度
cols++; //列数++
txt[row].push_back(tmp);
}
row++; cols = ; //行数++, 列数归零
}
for (int i = ; i < row; i++)
{
unsigned int j = ;
for ( ; j < txt[i].size() - ; j++)
{
print(txt[i][j], max_len[j], ' '); //输出单个字符串,长度不够的输出空格
}
cout << txt[i][j] << endl; //每行每列
}
return ;
}

这里有一篇关于:istringstream, ostringstream, stringstream的文章,感觉不错.

http://www.cnblogs.com/gamesky/archive/2013/01/09/2852356.html

05-11 17:51