简体   繁体   English

如何遍历 shared_ptr 向量

[英]How to iterate through shared_ptr vector

I have a vector:我有一个向量:

vector<MyClass> database;  

and a second one:第二个:

vector<shared_ptr<MyClass> > databasePtrs;

databasePtrs contains pointers to database elements in a certain order. databasePtrs包含按特定顺序指向数据库元素的指针。 I want to iterate through the pointers using an iterator, not for cycle.我想通过使用迭代器,而不是指针迭代for周期。 But I have no idea how to create the iterator.但我不知道如何创建迭代器。 The expression:表达方式:

vector<MyClass>::iterator it;

obviously doesn't work, nor do other variants I've tried.显然不起作用,我尝试过的其他变体也不起作用。

I have no idea, how to create the iterator.我不知道如何创建迭代器。

You can get the iterator to the first element of the vector using std::vector::begin .您可以使用std::vector::begin将迭代器获取到向量的第一个元素。

 vector<MyClass>::iterator it;

You cannot use that iterator to iterate a vector<shared_ptr<MyClass> > .您不能使用该迭代器来迭代vector<shared_ptr<MyClass> > You need a vector<shared_ptr<MyClass> >::iterator instead.你需要一个vector<shared_ptr<MyClass> >::iterator代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM