2012-03-21 56 views
1

大家好,能告诉我为什么VS2010会给我一个这段代码的错误,我看不出有什么问题?有关std :: transform的疑惑 - 将std :: map的value_type :: second转换为std :: vector

错误代码:错误C2679:二进制“=”:没有操作员发现这需要类型的右边的操作数“的std ::矢量< _Ty>”(或没有可接受的转化率)

// Elements container 
typedef std::vector<CFVFElementPtr> ElementArray; 
typedef std::map<CVertexSemantic::Type, ElementArray> ElementsMap; 

// Create an empty array of elements 
ElementsMap::value_type::second_type allElements; 

// Concatinate each std::vector found within the map 
std::transform(m_elementsMap.begin(), m_elementsMap.end(), 
std::insert_iterator<ElementArray>(allElements, allElements.end()), 
select2nd<ElementsMap::value_type>()); 

所有我试图做的是

for (auto i = m_elementsMap.begin(); i != m_elementsMap.end(); ++i) 
{ 
    const ElementArray& elements = (*i).second; 
    allElements.insert(allElements.end(), elements.begin(), elements.end()); 
} 

至于巴勃罗的回应,我想创造一个接受的ElementArray数组的自定义迭代器,但现在我得到错误的水桶负荷。

template < class Container > 
class container_insert_interator 
{ 
public: 
    typedef container_insert_interator<Container> this_type; 
    typedef Container container_type; 
    typedef typename Container::const_reference const_reference; 
    typedef typename Container::value_type valty; 

    explicit container_insert_interator (Container& cont, typename Container::iterator iter) 
      : container(&cont), iter(iter) 
    { } 

    this_type& operator = (typename const_reference value) 
    { 
     iter = container->insert(iter, std::begin(value), std::end(value)); 
     ++iter; 
     return *this; 
    } 

    this_type& operator*() 
    { 
     return *this; 
    } 

    this_type& operator++() 
    { 
      return *this; 
    } 

    this_type operator++ (int) 
    { 
     return *this; 
    } 

protected: 
    Container* container; // pointer to container 
    typename Container::iterator iter ; // iterator into container 
}; 
+0

柜面你想知道什么[select2nd](http://www.sgi.com /tech/stl/select2nd.html),它只是返回std :: map 。第二个std :: map的value_type。 – Sent1nel 2012-03-21 02:11:03

回答

1

问题是ElementsMap::value_type::second_typeElementArray。也就是说,您试图将ElementArray的实例插入到ElementArray中,该实例确实拥有CFVFElementPtr的实例。

更新:从

this_type& operator = (typename const_reference value) 

改变你的operator=声明

template<typename OtherContainer> 
this_type& operator = (OtherContainer const& value) 

几乎工作。除了VS 2010或GCC 4.6.1都不提供返回迭代器的vector::insert重载。如果你想让你的范围插入返回一个迭代器来代替iter,你可能需要一个更新的编译器。

更改执行在年底operator=始终插入,即

container->insert(container->end(), std::begin(value), std::end(value)); 

编译罚款与GCC 4.6.1(当然,你可以删除对iter所有引用您的迭代器类)。

+0

我尝试创建自己的接受'ElementArray'的迭代器,它仍然不起作用。 – Sent1nel 2012-03-21 03:23:59

+0

使用'template this_type&operator =(OtherContainer const&value)'比我在下面发布的版本更通用,因为它允许任何STL容器类型,所以我会接受你的答案。 – Sent1nel 2012-03-21 16:01:42

0

我找到了答案,该问题与我的自定义迭代器。正确的一个工程是

template < class Container > 
class container_back_insert_interator 
{ 
public: 
    typedef container_back_insert_interator<Container> this_type; 
    typedef Container container_type; 
    typedef typename container_type::const_reference const_reference; 
    typedef typename container_type::value_type valty; 

    explicit container_back_insert_interator (container_type& cont) 
    : container(&cont) 
    { } 

    this_type& operator = (const_reference value) 
    { 
     container->insert(container->end(), std::begin(value), std::end(value)); 
     return *this; 
    } 

    this_type& operator*() 
    { 
     return *this; 
    } 

    this_type& operator++() 
    { 
     return *this; 
    } 

    this_type operator++ (int) 
    { 
     return *this; 
    } 

protected: 
    container_type* container; // pointer to container 
}; 

template < class Container > 
inline container_back_insert_interator<Container> container_back_inserter(Container& cont) 
{ 
    return container_back_insert_interator<Container>(cont); 
} 

不过,我必须提醒的是,如果你在Visual Studio 2010中使用此,你将必须实现一个std SGI形式::变换。由于某种原因,VS2010随附的版本会引发大量错误,所有错误均在<xutility>标题内。

This std::transform works just fine。

1

您可以考虑使用一个仿函数,可以添加元素,您的目标阵列std::for_each一起散步地图:

struct ElementArray_appender 
{ 
    ElementArray_appender(ElementArray& dest_) : dest(dest_) { 
    }; 

    void operator()(ElementsMap::value_type const& map_item) { 
     ElementArray const& vec(map_item.second); 

     dest.insert(dest.end(), vec.begin(), vec.end()); 
    }; 

private: 
    ElementArray& dest; 
}; 



// in whatever function: 

std::for_each(m_elementsMap.begin(), m_elementsMap.end(), ElementArray_appender(allElements)); 
相关问题