曾格的github

C++输入问题

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 
 5 int main(){
 6     vector<int> v;
 7     int i;
 8     do{
 9         cin>>i;
10         v.push_back(i);
11     }while(cin.get()!='\n');
12     vector<int>::iterator it;
13     for(it=v.begin();it!=v.end();it++){
14         cout<<*it<<" ";
15     }
16     cout<<endl;
17     return 0;
18 }

(一)不定长度的vector,关键词:do...while 和 cin.get()

(二)不定长度的stack,元素为string类型。关键词:string 类型可直接整体输入

 1 int main(){
 2     stack<string> init;
 3     do{
 4         string tmp;
 5         cin>>tmp;
 6         init.push(tmp);
 7     }while(cin.get()!='\n');
 8     while(!init.empty()){
 9         cout<<init.top();
10         init.pop();
11     }
12 }

 

posted @ 2021-09-08 19:15  曾格  阅读(47)  评论(0编辑  收藏  举报
Live2D