gpt4 book ai didi

c++ - 如何使用 std::transform 算法将 hash_map 转换为 hash_map

转载 作者:行者123 更新时间:2023-11-28 01:00:29 25 4
gpt4 key购买 nike

我知道这应该很容易,但我就是没能消除编译错误。这是我的代码:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com