题目本身很简单,有几点值得注意的:
1, string 类的逆向迭代器,
可以用string s( s0.rbegin(), s0.rend()) 来很方便的逆转字符串。
2, >> 与 getline 混用时,注意 '/n', 用 成员函数ignore()开跳过它。
3,getline的几个版本
1) string getline: istream & getline( istream & is, string &s)
2) istream的成员 : istream & istream::getline( char *buffer, streamsize num)
(其实这两种都各自有两个版本)
#include<iostream> #include<string> #include<sstream> using namespace std; int main(void) { int N; for( cin >> N, cin.ignore(); N > 0; N--) { cin.ignore(); // over the bland line betweet input block int n; for( cin >> n, cin.ignore(); n > 0; n--) { string line, word; getline( cin, line); istringstream instr( line); while( instr >> word) { cout<< string( word.rbegin(), word.rend()); if( ! instr.eof() ) cout<<" "; } cout<<endl; } if( N != 1) cout<<endl; // for output block } return 0; }