使用唯一指针 c++ 反转字符串



下面是我的.cpp文件和.h文件。在得到迈克的很多帮助后,我终于让它工作了;但是,当我在 Visual Studio 2012 上编译它时,它会<在"for><s.length();i++)"。谁能告诉我我在那里做错了什么?>

[code]
#include"DownwardStack.h"
#include<iostream>
#include<string>
#include<memory>
#include<cassert>
using namespace std;
unique_ptr<string> reverse_string(string const &s);
int main()
{
    int count = 0;
    string s;
    unique_ptr<string> reverse(new string());
    unique_ptr<DownwardStack<int>> ptr(new DownwardStack<int>());
    cout << "Your string: ";
    cin >> s;
    reverse = reverse_string(s);
    cout << "Your reverse string is: " << *reverse << endl;
    if(ptr->IsEmpty())
        cout << "ptr is empty" << endl;
    else
        cout << "ptr is not empty" << endl;
    assert(ptr->IsEmpty());
    assert(!ptr->IsFull());
    ptr->Push(5);
    ptr->Push(7);
    ptr->Push(10);
    ptr->Push(15);
    ptr->Push(4);
    cout << "Stack size: " << ptr->GetSize() << endl;
    cout << "Top element: " << ptr->Peek() << endl;
    cout << "Pop one element out." << endl;
    ptr->Pop();
    cout << "Top element: " << ptr->Peek() << endl;
    return 0;
}
unique_ptr<string> reverse_string(string const &s)
{
    DownwardStack<char> stack;
    cout << s.length() << endl;
    // Here it gives me a warning on the for loop
    for (int i = 0; i < s.length(); i++)
    {
        stack.Push(s[i]);
    }

    unique_ptr<string> result(new string);

    // Again it gives me a warning on the for loop
    for(int i = 0; i < s.length(); i++)
    {
        *result += stack.Peek();
        stack.Pop();
    }
    return result;
}
[/code]

这是我的头文件 .h

[code]
#pragma once
#include<cassert>
#include<stack>
#include<string>
// size: number of elements inside the array
const int FIXED_ARRAYED_STACK_CAPACITY = 100;
template<class T>
class DownwardStack
{
public:
    DownwardStack();
    ~DownwardStack();
    // 1 step
    // O(0)
    int GetSize() const {return size;}
    bool IsEmpty() const {return (size==0);}
    bool IsFull() const {return (size==FIXED_ARRAYED_STACK_CAPACITY);}
    T Peek();
    void Pop();
    void Push(T val);
    void Clear();
    void DisplayStack();
private:
    int size;
    T elements[FIXED_ARRAYED_STACK_CAPACITY];
};
// O(1)
template<class T>
DownwardStack<T>::DownwardStack()
{
    size = 0;
}
template<class T>
DownwardStack<T>::~DownwardStack()
{
}
// assert = 1 step
// IsEmpty() = 1 step
// total = 2 steps
// f(n) = 2
// O(1)
template<class T>
T DownwardStack<T>::Peek()
{
    assert(!IsEmpty());
    return elements[FIXED_ARRAYED_STACK_CAPACITY - size];
}
// In order to take something out, it must not be empty
// assert = 1 step
// IsEmpty() = 1 step
// size-- = 1 step
// total = 3 steps
// O(1)
template<class T>
void DownwardStack<T>::Pop()
{
    assert(!IsEmpty());
    size--;
}
// In order to put in something, the stack must not be full
// assert = 1 step
// IsFull = 1 step
// assignment = 1 step
// size++ = 1 step
// total = 4 steps
// O(1)
template<class T>
void DownwardStack<T>::Push(T val)
{
    assert(!IsFull());
    elements[FIXED_ARRAYED_STACK_CAPACITY - size - 1] = val;
    size++;
}
template<class T>
void DownwardStack<T>::Clear()
{
    size = FIXED_ARRAYED_STACK_CAPACITY;
    assert(IsEmpty());
}

[/code]

我会将其实现为

std::string reverse_string(std::string const & s) {
    return {s.rbegin(), s.rend()};            // C++11 or later
    return std::string(s.rbegin(), s.rend()); // historical dialects of C++
}

如果你真的必须通过使用堆栈让自己的生活变得困难:将每个字符推入其中,然后将每个字符弹出到新字符串中。根据堆栈的性质,您将以相反的顺序弹出字符。

std::string reverse_string(std::string const & s) {
    DownwardStack<char> stack;
    // write a loop to push each character of "s" onto "stack"
    std::string result;
    // write a loop to pop each character from "stack" into "result"
    return result;
}

如果你绝对必须返回一个唯一的指针(你真的,真的不应该),那么这就变成了

std::unique_ptr<std::string> reverse_string(std::string const & s) {
    DownwardStack<char> stack;
    // write a loop to push each character of "s" onto "stack"
    std::unique_ptr<std::string> result(new std::string);
    // write a loop to pop each character from "stack" into "*result"
    return result;
}

帮世界一个忙:只在你真正需要的时候使用new。没有理由将unique_ptr用于堆栈(使其自动)或返回值(因为std::string是可移动的)。

相关内容

  • 没有找到相关文章

最新更新