繁体   English   中英

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

[英](C++) Reversing a string using stacks?

我正在尝试使用堆栈来反转字符串。 它正确地反转了字符串,但是当达到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] << "\n\n";

    return "The function was a success. Now that's what I call reverse psychology."; 
}

我是无符号的,因此如果它等于0,则它在减量时会回绕。您需要对其使用带符号的类型,或者检查边界条件而不涉及负数(即,不要将其与-1比较并执行如果为0,则不递减)。

我可以想到以下几种选择。

使用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM