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

时间:2013-09-24 10:58:25

标签: c++ smart-pointers

下面是我的.cpp文件和.h文件。在得到Mike的大量帮助后,我终于开始工作了;但是,当我在Visual Studio 2012上编译它时,它给了我2个关于'<'的警告“for(int i = 0; i< 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]

1 个答案:

答案 0 :(得分:2)

我会将其实现为

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可移动)。