#include #include //注意,如果使用stringstream,需要加入这个头文件,否则报 // c++ aggregate ‘std::stringstream ss’ has incomplete type and cannot be defined using namespace std; /* 测试用例: 3 i am huanghai ! */ int main() { int a; cin >> a; //准备接收一个带空格的字符串 string b; // 方法1: // getchar(); //如果输入的数据中,存在多个空格的时候,此方法无效,需采用getline(cin,b) // 方法2: getline(cin, b); //吃掉上一次输入后面的换行符 getline(cin, b); //读入有用的字符串数据 //字符串流,用于将带空格的字符串分割开,形成一个个不带空格的字符串 stringstream ss(b); string word; int cnt = 0; while (ss >> word) { cout << word << endl; cnt++; } cout << cnt << endl; return 0; }