如何使用std :: transform算法将hash_map <string,string =“”>转换为hash_map <wstring,wstring =“”>?</wstring,> </string,>

时间:2012-01-26 12:10:37

标签: c++

我知道它应该很容易,但我只是无法摆脱编译错误。这是我的代码:

template<class C>
struct basic_field_type_map : public hash_map<basic_string<C>, basic_string<C>>
{
};

typedef basic_field_type_map<char> field_type_map;
typedef basic_field_type_map<wchar_t> wfield_type_map;

wfield_type_map::value_type to_wfield_type_map_value(field_type_map::const_reference src)
{
  return wfield_type_map::value_type(to_wstring(src.first), to_wstring(src.second));
}

wfield_type_map to_wfield_type_map(const field_type_map& m)
{
  wfield_type_map res;
  transform(m.begin(), m.end(), res.begin(), to_wfield_type_map_value);
  return res;
}

to_wstring具有以下签名:

wstring to_wstring(const string& s)

编译代码会给我以下错误:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(260): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Ax=std::allocator<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(707): could be 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(std::basic_string<_Elem,_Traits,_Ax> &&)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Ax=std::allocator<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(761): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const std::basic_string<_Elem,_Traits,_Ax> &)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Ax=std::allocator<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(766): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(const _Elem *)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Ax=std::allocator<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(771): or       'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::operator =(_Elem)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Ax=std::allocator<wchar_t>
1>          ]
1>          while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::basic_string<_Elem,_Traits,_Ax>)'
1>          with
1>          [
1>              _Elem=wchar_t,
1>              _Traits=std::char_traits<wchar_t>,
1>              _Ax=std::allocator<wchar_t>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(259) : while compiling class template member function 'std::pair<_Ty1,_Ty2> &std::pair<_Ty1,_Ty2>::operator =(std::pair<_Ty1,_Ty2> &&)'
1>          with
1>          [
1>              _Ty1=const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>,
1>              _Ty2=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
1>          ]
1>          z:\dev\internal\vedtool\defs.h(34) : see reference to class template instantiation 'std::pair<_Ty1,_Ty2>' being compiled
1>          with
1>          [
1>              _Ty1=const std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>,
1>              _Ty2=std::basic_string<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t>>
1>          ]
Build has been canceled.

我正在使用VS2010。

我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:2)

地图中的关键字类型为const,您无法分配给它,这是std::transform的作用;循环看起来像这样:

for(; first != last; ++first, ++dest)
  *dest = func(*first);  // assignment of key-value pair here

请注意,还有一个更大的问题:您res地图甚至没有空间来容纳field_type_map中的所有内容!你只需要在结尾处写一下就可以在这里调用未定义的行为。

您最好的选择是使用insert手动for_each每个新对:

std::for_each(m.begin(), m.end(),
    [&](field_type_map::value_type const& p){
      res.insert(to_wfield_type_map_value(p));
    });

答案 1 :(得分:1)

您不能将res.begin()用作transform的输出参数,原因有两个:

  • 地图为空,因此begin()不是有效的迭代器,而
  • 无法重新分配地图元素。

一种可能性是用hash_map替换过时的STL unordered_map;然后您可以使用std::insert_iterator插入值:

transform(m.begin(), m.end(), 
          inserter(res, res.end()), 
          to_wfield_type_map_value);

如果你真的想因某些原因使用hash_map,那么这将不起作用,因为hash_map没有insert的{​​{1}}函数insert_iterator使用。据我所知,STL没有合适的插件,但你可以自己提供:

template <typename Container>
class hash_insert_iterator {
public:
    hash_insert_iterator(Container & c) : container(&c) {}

    hash_insert_iterator & 
    operator=(typename Container::value_type const & value) {
        container->insert(value);
        return *this;
    }
    hash_insert_iterator & operator*() {return *this;}
    hash_insert_iterator & operator++() {return *this;}
    hash_insert_iterator & operator++(int) {return *this;}

private:
    Container * container;
};

template <typename Container>
hash_insert_iterator<Container> hash_inserter(Container & c) {
    return hash_insert_iterator<Container>(c);
}

或者,直接插入可能更简单,而不是使用transform

for (auto const & value : m) {
    res.insert(to_wfield_type_map_value(m));
}

或某些等效的forfor_each构造,如果您的编译器不支持基于范围的for循环。