2014-10-27 83 views
0

我想使用堆栈来反转字符串。它正确地反转字符串,但for循环崩溃时达到0.我得到一个“字符串下标超出范围”错误。目前for循环只能递减到1.我怎样才能得到它来推送和显示s1 [0]?(C++)使用堆栈反转字符串?

这是主代码:

#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

看到S1是如何初始化这将是有趣 – 2014-10-27 21:48:54

+0

S1通过询问用户输入然后使用该输入作为反向函数的参数进行初始化,如下所示: cout << reverse(user_input)<< endl; – Shroomies 2014-10-27 22:01:09

回答

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

我是无符号的,因此当它递减时它会环绕,如果它等于0.您需要使用它的签名类型或检查边界条件而不涉及负数(也就是说,不要比较它与-1,如果它是0不减少它)。