c++ - STL迭代器重置

标签 c++ stl iterator

我尝试重用 STL 迭代器,但找不到任何相关信息。这段代码有问题:

    std::vector< boost::shared_ptr<Connection> >::iterator poolbegin = pool.begin();
std::vector< boost::shared_ptr<Connection> >::iterator poolend = pool.end();
if( order ) {
    poolbegin = pool.rbegin(); // Here compilation fails
    poolend   = pool.rend();
}
    for( std::vector< boost::shared_ptr<Connection> >::iterator it = poolbegin; it<poolend; it++) {

但是出现错误:

error: no match for ‘operator=’ in ‘poolbegin = std::vector<_Tp, _Alloc>::rbegin() with _Tp = boost::shared_ptr, _Alloc = std::allocator >’

有没有办法将迭代器重置为新值?喜欢 shared_ptr::reset 吗?

最佳答案

rbegin() 返回一个 reverse_iterator,它是与普通 iterator 完全不同的类型。

它们不能相互赋值。

关于c++ - STL迭代器重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12605080/

相关文章:

c++ - 列表迭代器 Remove()

java - 为什么listiterator只为List设计,而Set不是?

c++ - C 静态数组初始化——我需要多详细?

c++ - 为什么 std::forward 返回 static_cast<T&&> 而不是 static_cast<T>?

c++ - 这段代码有什么作用? (关于指针)

python - 使用字典迭代图类

iterator - 在 Java 中加入(联合)集合内部的集合

c++ - 为什么逗号在 C++ 可变函数声明中是可选的?

c++ - STL中有 "magic"吗?

C++:使用用户定义的泛型函数