创建相同向量的多个实例

时间:2017-01-29 19:56:46

标签: c++ vector

对于我正在研究的项目,我想为struct对象提供一种“每用户”向量。

所以,我这样做是为了声明向量:

// Defined in global namespace, and the pointer to structure objects is deliberate
typedef std::vector<s_AttachmentData*> AttachmentsContainer; 

struct s_User 
{
public: 
    AttachmentsContainer attachmentsData; 
};

我可以毫无问题地将这样的结构对象添加到向量中。我正在努力通过迭代器实际访问和修改数据。这是我尝试过的:

// 'p' is pointer to s_User structure above
for (AttachmentsContainer::iterator it = p->attachmentsData.begin(); it != p->attachmentsData.end(); ++it)
{
   // Do stuff here, however I can't get to this point because the application crashes. 
}

我只是想访问容器中的数据,我能够填充向量,但是当我循环并尝试访问任何东西时,我得到的“向量迭代器不兼容”错误。如果有人能够与此相关,或者可以将我推向正确的方向,我将感激不尽!我奇怪地找不到任何类似的问题。

1 个答案:

答案 0 :(得分:0)

*it会引用s_AttachmentData*

如果您真的想存储指向s_AttachmentData的指针,那么您可以访问类似

的指针
    (*it)->...