(C++)使用堆栈反转字符串

(C++) Reversing a string using stacks?

本文关键字:字符串 堆栈 C++      更新时间:2023-10-16

我正在尝试使用堆栈反转字符串。它正确地反转字符串,但当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,则不要递减)。