C++中已经自带堆栈对象stack,无需编写堆栈操作的具体实现代码。
本题目主要帮助大家熟悉stack对象的使用,然后实现字符串的逆序输出
输入一个字符串,按字符按输入顺序压入堆栈,然后根据堆栈后进先出的特点,做逆序输出
stack类使用的参考代码
: #include s(注意stack是模板类):stack s; //堆栈的数据类型是字符型 ct压入堆栈:s.push(ct);s.pop();c2:c2 = s.top();s.empty(),如果为空则函数返回true,如果不空则返回false第一行输入t,表示有t个测试实例 第二起,每一行输入一个字符串,注意字符串不包含空格
字符串的输入可以考虑以下代码:
- #include
- int main()
- {
- string str;
- int len;
- cin >> str; //把输入的字符串保存在变量str中
- len = str.length() //获取输入字符串的长度
- }
每行逆序输出每一个字符串
2 abcdef aabbcc
fedcba ccbbaa
- #include
- #include
- #include
- using namespace std;
- //答案仅供参考,请勿直接复制粘贴
- int main() {
- int t;
- cin >> t;
- string str;
- int len;
-
- while (t--) {
- stack <char> s;
- cin >> str;
- len = str.length();
- for (int i = 0; i < len; i++) {
- s.push(str[i]);
- }
- if (!s.empty()) {
- for (int i = 0; i < len; i++) {
- cout << s.top();
- s.pop();
- }
- cout << endl;
- }
- }
- }