peek()用於看接下來讀入的那個字符是什么,沒有輸入也沒有丟棄,你可以正常使用cin。
返回值是字符。
使用方法為cin.peek()。
#include <iostream> #include<ctype.h> #include<vector> #include<string> using namespace std; int main() { vector<int> a; vector<string> b; for (int c = 0; c < 3; c++) { char temp; temp=cin.peek();//獲取下一次輸入的首字符 if (isdigit(temp))//判斷該字符是否為整數字符 { int temp; cin >> temp; a.push_back(temp); } else { string temp; cin >> temp; b.push_back(temp); } getchar();//cin>>輸入時沒有丟棄掉空格或換行,要丟棄掉他們。 } return 0; }