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

标签 c++ smart-pointers

下面是我的.cpp 文件和.h 文件。在得到 Mike 的大量帮助后,我终于让它工作了;然而,当我在 Visual Studio 2012 上编译它时,它在“for (int i = 0; i < s.length(); i++)”这一行上给出了 2 个关于“<”有符号/无符号不匹配的警告。谁能告诉我我在那里做错了什么?

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

关于c++ - 使用唯一指针 C++ 反转字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18979716/

相关文章:

c++ - 与模板类中的友元函数链接错误

c++ - 减少 std::bind 模板代码膨胀?

c++ - 避免对非虚拟析构函数进行对象切片

c++ - std::shared_ptr 复制构造函数线程安全

C++ 使用智能指针来改变指针值

c++ - 如何跟踪进程创建的 ActiveX 控件?

c++ - 自动检测模板函数的返回类型

c++ - std::is_pointer 检查通用引用

rust - 什么时候应该使用智能指针?

c++ - 如何将 std::shared_ptr 添加到多个 STL 容器?