PTA 1009 说反话 (20 分)

文章目录

  • code1 vector
  • code2 stack
  • stack 常见函数
    • top() 取栈顶元素
    • push()类似push—back
    • pop() 弹出元素
    • size() 大小
    • empty()判断是否为空

code1 vector

#include
#include
#include
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 
#include 
#include
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 a;
stack b;

top() 取栈顶元素

push()类似push—back

pop() 弹出元素

size() 大小

empty()判断是否为空

你可能感兴趣的:(PTA 1009 说反话 (20 分))