c++ - 如何使用 std::transform 算法将 hash_map<string, string> 转换为 hash_map<wstring, wstring>?

标签 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。

我做错了什么?

谢谢。

最佳答案

映射中的键类型是const,你不能给它赋值,这是std::transform做的;循环看起来像这样:

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

请注意,还有一个更大的问题:您的 res map 甚至没有空间容纳 field_type_map 中的所有内容!您只是在结尾处写入,从而在此处调用未定义的行为。

你最好的选择是用 for_each 手动插入每个新对:

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

关于c++ - 如何使用 std::transform 算法将 hash_map<string, string> 转换为 hash_map<wstring, wstring>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9017915/

相关文章:

mysql 中的 c++ 函数 make_scrambled_pa​​ssword 到 mariadb

c++ - 关于c++命名约定的问题

c++ - 如何将文件读入数组,然后对数据进行排序?

c++ - 鼠标钩子(Hook)代码内部的调用方法导致访问冲突

c++ - 元素数量未知的内存分配

c++ - 警告 : 'virtual void wxWindowBase::SetInitialBaseSize(const wxSize&)' is deprecated

c++ - 添加 Gtk::Entry 到 Gtk::Dialog

c++ - 如何在另一个 C++ 程序中使用预处理的 .i 文件?

c++ - 这是缩小数组的有效方法吗?

c++ - 用户定义的容器不适用于 std::ranges