使用move_iterator访问vector
Accessing vector with move_iterator
我有一个std:vector,其中MyClass不能被复制(复制构造函数和赋值构造函数被删除),但可以移动
我想访问for循环中的元素,我该怎么做呢:
for(MyClass c : my_vector) {
//c should be moved out of my_vector
} // after c goes out of scope, it get's destructed (and no copies exist anymore)
我找到了move_iterator,但我不知道如何在for循环中正确使用它
按引用迭代并移动:
for (auto & x : v) { foo(std::move(x)); }
使用std::move
-算法可能更合适,从<algorithm>
开始,类似于std::copy
。或者,std::transform
和make_move_iterator()
可能适合。
类似
for(MyClass &c : my_vector) {
do_something_with(std::move(c));
}
是我通常会做的
相关文章:
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 使用 std::vector<double> 访问由 std::unique_ptr<double[2] 管理的数据>
- 如何访问vector<pair<int, pair<int, int>>> D[20]?
- 访问 vector 中的子结构体值
- 通过 vector.begin() 命令访问 vector 的第一个元素
- 如何在 Boost Python 中访问 Vector
- 使用move_iterator访问vector
- 访问vector的vector中的c++ vector而不复制它
- 如何使用常量引用访问 vector<int*> 的元素?
- 在c++中访问vector中的数组
- 当访问Vector中的实例时,shared_ptr的Vector给出seg错误
- 访问vector元素属性的最佳方法
- 访问vector的内容
- 在pop_back()之后无法访问vector(分段错误)
- 访问Vector的元素- c++
- 使用std::vector::at或std::vector::operator[]访问vector:中存储的对象
- 如何在c++中从指向vector的指针访问vector的内容
- 使用迭代器访问 vector<std::unique_ptr > 的元素<T>?
- 在类函数中访问vector的元素
- 通过多个线程访问vector