code1 vector
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main()
{
string s;
vector<string>v;
while(cin>>s)
{
v.push_back(s);
}
for (auto it = v.rbegin(); it != v.rend(); it++) {
if (it == v.rbegin()) cout << *it;
else cout << " " << *it;
}
return 0;
}
这个有一个问题是:while(cin>>s),在vs上跑不会终止,因为小黑窗从文件读取到结尾就结束。
我们可以通过ctrl +z进行结束。
code2 stack
#include <iostream>
#include <stack>
#include<string>
using namespace std;
int main() {
stack<string>v;
string s;
while(cin >> s) v.push(s);
cout << v.top();
v.pop();
while(!v.empty()) {
cout << " " << v.top();
v.pop();
}
return 0; }
利用stack
stack 常见函数
/可以使用list或vector作为栈的容器,默认是使用deque的。
比如:stack<int, list> a;
stack<int, vector> b;