boost :: shared_ptr的bimap和boost :: transform_iterator

问题描述

| 我有一个“ 0”容器,想将一个迭代器返回到左视图,以确保内容的一致性。返回“ 1”将返回一个取消引用“ 2”的迭代器。 显然,这不是我想要的,因为可以通过取消引用
shared_ptr
来更改
A
。我想要一个取消引用de5的迭代器(我并不在乎shared_ptr是否为const)。我知道我应该通过使用boost :: transform_iterator来做到这一点,但是我无法弄清楚“ casting”函数应该是什么样子。 有人可以帮我吗?还是有另一种更容易获得我想要的方法方法? 编辑:这是我到目前为止所能说的,足以给我提供2个值得修复的屏幕错误
typedef boost::bimap<unsigned int,boost::shared_ptr<A> > container_type;
typedef container_type::left_const_iterator base_const_iterator;
typedef boost::transform_iterator<makeIterConst<A>,base_const_iterator> const_iterator;

template <typename T>
struct makeIterConst : std::unary_function<std::pair<unsigned int const,boost::shared_ptr<T> const>,std::pair<unsigned int const,boost::shared_ptr<T const> const> >
{
  std::pair<unsigned int const,boost::shared_ptr<T const> const> operator() (std::pair<int const,boost::shared_ptr<T> const> const & orig) const
  {
    std::pair<int const,boost::shared_ptr<T const> const> newPair(orig.first,boost::const_pointer_cast<T const>(orig.second));
    return newPair;
  }
};
这是\“核心\”错误:   注意:候选函数不可行:   没有来自\'const的已知转换   boost :: bimaps :: relation :: structured_pa​​ir,   boost :: bimaps :: tags :: tagged,   boost :: bimaps :: relation :: member_at :: right>,   mpl _ :: na,   boost :: bimaps :: relation :: normal_layout> \'   到\'const std :: pair> \'为   第一个论点     

解决方法

问题在于您的
value_type
实际上不是
std::pair
(并且不能隐式转换为一个),因此无法传递给
makeIterConst::operator()
。 取take10ѭ代替。
#include <iostream>
#include <string>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/bimap.hpp>
#include <boost/iterator/transform_iterator.hpp>

struct A
{
    std::string data;
    A(const std::string& s) : data(s) {}
};

typedef boost::bimap<unsigned int,boost::shared_ptr<A> > container_type;
typedef container_type::left_map::const_iterator base_const_iterator;

template <typename T>
struct makeIterConst : std::unary_function<base_const_iterator::value_type const &,std::pair<unsigned int const,boost::shared_ptr<T const> const> >
{
     std::pair<unsigned int const,boost::shared_ptr<T const> const> operator()
             (base_const_iterator::value_type const & orig) const
     {
         std::pair<int const,boost::shared_ptr<T const> const> newPair(orig.first,boost::const_pointer_cast<T const>(orig.second));
         return newPair;
     }
};

typedef boost::transform_iterator<makeIterConst<A>,base_const_iterator> const_iterator;

int main()
{
    container_type m;
    boost::shared_ptr<A> p = boost::make_shared<A>(\"foo\");
    m.insert( container_type::value_type(1,p));

//  using regular iterator
    for( base_const_iterator left_iter = m.left.begin();
                         left_iter != m.left.end();
                         ++left_iter )
    {
        std::cout << left_iter->first << \" --> \" << left_iter->second->data << \'\\n\';
        left_iter->second->data =  \"bar\"; // compiles
    }

    // using constified iterator
    for( const_iterator left_iter = boost::make_transform_iterator(m.left.begin(),makeIterConst<A>() );
              left_iter != boost::make_transform_iterator(m.left.end(),makeIterConst<A>() );
            ++left_iter )
    {
        std::cout << left_iter->first << \" --> \" << left_iter->second->data << \'\\n\';
//   the following will give a compilation error,as expected
//      left_iter->second->data =  \"changed_foo\";
    }
}
测试:https://ideone.com/fHIUe