(C++)使用堆栈反转字符串
(C++) Reversing a string using stacks?
我正在尝试使用堆栈反转字符串。它正确地反转字符串,但当i达到0时,for循环崩溃。我得到一个"字符串下标超出范围"错误。目前for循环仅递减到1。如何将其推送并显示s1[0]?
这是主要代码:
#include <cstdlib> // Provides EXIT_SUCCESS
#include <iostream> // Provides cin, cout
#include <stack> // Provides stack
#include <string> // Provides string
using namespace std;
. . .
string reverse(string & s1)
{
stack<char> stk1;
string::size_type i;
// this for loop sets the rest of the characters
for (i = s1.size() - 1; i > 0; i--)
{
stk1.push(s1[i]);
cout << stk1.top();
}
return "The function was a success. Now that's what I call reverse psychology.";
}
这是头文件:
#ifndef MAIN_SAVITCH_STACK1_H
#define MAIN_SAVITCH_STACK1_H
#include <cstdlib> // Provides size_t
namespace main_savitch_7A
{
template <class Item>
class stack
{
public:
// TYPEDEFS AND MEMBER CONSTANT -- See Appendix E if this fails to compile.
typedef std::size_t size_type;
typedef Item value_type;
static const size_type CAPACITY = 30;
// CONSTRUCTOR
stack( ) { used = 0; }
// MODIFICATION MEMBER FUNCTIONS
void push(const Item& entry);
void pop( );
// CONSTANT MEMBER FUNCTIONS
bool empty( ) const { return (used == 0); }
size_type size( ) const { return used; }
Item top( ) const;
private:
Item data[CAPACITY]; // Partially filled array
size_type used; // How much of array is being used
};
}
#include "stack1.template" // Include the implementation.
#endif
这就是堆栈实现(一个模板文件):
#include <cassert> // Provides assert
namespace main_savitch_7A
{
template <class Item>
const typename stack<Item>::size_type stack<Item>::CAPACITY;
template <class Item>
void stack<Item>::push(const Item& entry)
// Library facilities used: cassert
{
assert(size( ) < CAPACITY);
data[used] = entry;
++used;
}
template <class Item>
void stack<Item>::pop( )
// Library facilities used: cassert
{
assert(!empty( ));
--used;
}
template <class Item>
Item stack<Item>::top( ) const
// Library facilities used: cassert
{
assert(!empty( ));
return data[used-1];
}
}
我想把for循环改成这个,但它不起作用:
// this for loop sets the rest of the characters
for (i = s1.size() - 1; i >= 0; i--) // i > -1 doesn't work either
{
stk1.push(s1[i]);
cout << stk1.top();
}
cout << s1[0] << "nn";
return "The function was a success. Now that's what I call reverse psychology.";
}
我可以想到以下几个选项。
使用循环计数器的string::size_type
:
string::size_type i;
for (i = s1.size(); i > 0; i--)
{
stk1.push(s1[i-1]);
cout << stk1.top();
}
或
使用int
作为循环计数器:
int i = 0;
for (i = s1.size()-1; i >= 0; i--)
{
stk1.push(s1[i]);
cout << stk1.top();
}
i是无符号的,因此当它等于0时,它会在递减时换行。您需要使用带符号的类型,或者在不涉及负数的情况下检查边界条件(即,不要将其与-1进行比较,如果它为0,则不要递减)。
相关文章:
- 如何使用堆栈将字符串输入拉伸 k 次
- 带有模板的字符串堆栈?
- 在堆或堆栈上分配的动态创建的字符串 - C
- GCC C++堆栈上的字符串
- JNI、C++在堆栈中推送字符串
- 我的 c 字符串复制函数正在损坏其他变量的堆栈
- 无法创建栅格堆栈,因为我无法连接字符串(dir_name + 文件名)
- 如何在 C++03 中没有重复代码的情况下在堆栈上创建一个非常量 C 字符串数组?
- 为什么字符串不会在堆栈中推送或弹出
- 我如何转换一个基于动态的,基于指针的int堆栈,以便将其打印成字符串
- 我在 36603 以上输入的任何数组大小时都会返回"堆栈溢出"错误。如何使字符串能够捕获整个.txt文件?
- 使用堆栈将字符串格式的十进制转换为双精度
- 查看使用堆栈是否相等的两个字符串
- C#到C代码P/调用多个std:字符串声明导致堆栈损坏
- palindrome功能(字符串,堆栈,队列)
- 堆栈上的 Const C 字符串
- 使用堆栈测试字符串中的x和y是否相等
- 将字符串放入堆栈C++时出错
- 无法序列化和反序列化 Qt/C++ 中的字符串堆栈
- C 字符串堆栈分配