gpt4 book ai didi

c++ - 在 Stack 实现中使用哪个智能指针?

转载 作者:行者123 更新时间:2023-11-30 02:38:41 36 4
gpt4 key购买 nike

根据我的正确理解:

scoped_ptr:无开销,无法复制或移动。

unique_ptr:无开销,不可复制,可移动。

shared_ptr:一些开销(引用计数),可以被复制。

话虽如此,如果需要多个所有者,则应使用 shared_ptr。

现在,在下面的这个程序中,它是 C++ 中堆栈的简单实现。我不明白应该使用哪种类型的智能指针。

我问这个问题的原因是 unique_ptr 和 shared_ptr 都不能被复制,而这正是我在这个简单堆栈的实现中所做的。我在我使用 C++ 指针的程序中注释掉了//HERE,如果您正确阅读该程序,您将看到数据是如何在几乎所有函数中被复制的。

游戏状态堆栈.h

#ifndef _H_GAMESTATE_
#define _H_GAMESTATE_

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>
#include <memory>

class node
{
public:
std::string gameState;
node * nextGameState; // HERE
};

class GameStateStack
{
private:
node * _topState; // HERE
void Destory();

public:
int gameStatesCount;
void PushGameState(std::string element);
void PopGameState();
std::string CurrentGameState();
GameStateStack();
~GameStateStack();
};

extern GameStateStack state;

#endif

游戏状态堆栈.cpp

#include <iostream>
#include <stdlib.h>
#include <string>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
#include "GameStateStack.h"
#include "template.h"

GameStateStack state;

GameStateStack::GameStateStack()
{
_topState = NULL;
gameStatesCount = 0;
}

GameStateStack::~GameStateStack()
{

}

void GameStateStack::PushGameState(std::string gameStateName)
{
node *newTopState = new node; // HERE
if (_topState == NULL)
{
newTopState->gameState = gameStateName;
newTopState->nextGameState = NULL;
_topState = newTopState;
gameStatesCount++;
}

else
{
newTopState->gameState = gameStateName;
newTopState->nextGameState = _topState;
_topState = newTopState;
gameStatesCount++;
}
}

void GameStateStack::PopGameState()
{
if (_topState == NULL)
std::cout << "Error: no gamestates available to pop";
else
{
node * old = _topState; // HERE
_topState = _topState->nextGameState;
delete(old);
gameStatesCount--;
}
}

std::string GameStateStack::CurrentGameState()
{
node *temp; // HERE
temp = _topState;
return temp->gameState;
}

void GameStateStack::Destory()
{
node *abc; // HERE
delete _topState;
delete abc->nextGameState;
}

最佳答案

这里是如何使用 std::unique_ptr 实现堆栈。请注意,使用 std::move() 重新分配 std::unique_ptr,使原始指向空无一物。

此外,我使用了更惯用的 if(topState) 而不是 if(topState == NULL)。如果 std::unique_ptr 指向某处则返回 true,否则返回 false。

此外,标准 C++ 规定我们不应该以 _ 开头的变量名。

#include <string>
#include <memory>
#include <iostream>

struct node
{
std::string gameState;
std::unique_ptr<node> nextGameState;

~node()
{
std::cout << "deleting: " << gameState << '\n';
}
};

class GameStateStack
{
// should not use _ to begin variable names in std C++
std::unique_ptr<node> topState;
int gameStatesCount;

public:
GameStateStack();
void PushGameState(std::string gameStateName);
void PopGameState();
std::string CurrentGameState();
void Destory();
};

GameStateStack::GameStateStack()
: gameStatesCount(0) // initialize here
{
//topState = NULL; // no need to initialize unique_ptr
//gameStatesCount = 0; // not here
}

void GameStateStack::PushGameState(std::string gameStateName)
{
std::unique_ptr<node> newTopState(new node);
newTopState->gameState = gameStateName;

newTopState->nextGameState = std::move(topState);
topState = std::move(newTopState);

gameStatesCount++;
}

void GameStateStack::PopGameState()
{
if(!topState)
std::cout << "Error: no gamestates available to pop";
else
{
topState = std::move(topState->nextGameState);
gameStatesCount--;
}
}

std::string GameStateStack::CurrentGameState()
{
if(topState)
return topState->gameState;
return "error: nothing on stack"; // error
}

void GameStateStack::Destory()
{
// deleting topState will first destroy the pointed to
// node's own unique_ptr<node> nextGameState
// which in turn will first delete its own nextGameState etc...
topState.reset();
}

int main()
{
GameStateStack stack;

std::cout << "\ndestroy test" << '\n';

stack.PushGameState("a");
stack.PushGameState("b");
stack.PushGameState("c");
stack.PushGameState("d");
stack.PushGameState("e");
stack.PushGameState("f");

stack.Destory();

std::cout << "\npush-pop test" << '\n';

stack.PushGameState("a");
stack.PushGameState("b");
stack.PushGameState("c");

std::cout << stack.CurrentGameState() << '\n';

stack.PopGameState();

stack.PushGameState("d");
stack.PushGameState("e");

std::cout << stack.CurrentGameState() << '\n';

stack.PopGameState();

std::cout << stack.CurrentGameState() << '\n';

stack.PopGameState();

std::cout << stack.CurrentGameState() << '\n';

stack.PopGameState();

std::cout << stack.CurrentGameState() << '\n';

stack.PopGameState();

std::cout << stack.CurrentGameState() << '\n';

stack.PopGameState();
}

关于c++ - 在 Stack 实现中使用哪个智能指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30564795/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com