当输入的不是“pair”,就会跳出if循环
#include<bits/stdc++.h> using namespace std; string str = "", s; int flag = 1; void input() { if (cin >> s ) { str += s; if ( s == "pair" ) { str += '<'; input(); str += ','; input(); str += '>'; } } else flag = 0; } int main() { int n; cin >> n; input(); if (cin >> s ) flag = 0; if (!flag ) cout << "Error occurred\n"; else cout << str << endl; }