c++ - boost shared_ptr vector 成员访问

标签 c++ algorithm boost

我是 boost c++ 库的新手,我有以下问题。

我的程序中有以下代码片段。

typedef std::vector<int> Indices;
typedef boost::shared_ptr<Indices> IndicesPtr;
...
IndicesPtr indices (new Indices);

在我将数据添加到 indices 之后,我想使用 operator [] 访问单个成员,但我收到一条错误消息。

错误:错误:indices[i] 中的 operator[] 不匹配。我不明白为什么会这样。使用 boost::shared_ptr<> 时是否有特定的访问方式。

最佳答案

你不应该在这里使用shared_ptr,只使用vector,但如果你想(例如训练)你可以使用

indices->operator[](index)

或取消引用 shared_ptr

(*indices)[index]

就像原始指针一样。

关于c++ - boost shared_ptr vector 成员访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31877563/

相关文章:

algorithm - 极坐标 map 生成

c++ - 带 Boost 的多态序列化

c++ - 函数绑定(bind)的目的

c++ - TR1 共享阵列

c# - 在 C++ 中扩展 C# 类

algorithm - 通过位掩码进行二进制搜索?

c++ - 通过 boost::asio 发送原始数据

c++ - AMD64 ABI 中的空类会怎样?

c++ - 有没有办法减少我的简单 2D DirectX 游戏中的 CPU 和 GPU 负载?

algorithm - 如何决定需要使用哪种方法来编写算法?