gpt4 book ai didi

c++ - 使用 std::unique_copy 从 vector 中提取唯一的、不区分大小写的字符串

转载 作者:行者123 更新时间:2023-11-30 04:27:12 26 4
gpt4 key购买 nike

问题

考虑以下 vector :

std::vector<std::string> extensions;

extensions.push_back(".cpp");
extensions.push_back(".CPP");
extensions.push_back(".h");
extensions.push_back(".H");

std::vector<std::string> caselessUniqueExtensions;

将扩展的内容复制到 caselessUniqueExtensions 中,但丢弃所有唯一的扩展,忽略大小写。 (即结果应包含两个元素 - .cpp.CPP 之一,以及 .h 之一。 H.)

问题

显然有很多方法可以做到这一点,有些比其他的更有效,有些比其他的更易读。以下是我认为可行的一种方法:

#include <boost/algorithm/string/compare.hpp>

...

std::unique_copy(
extensions.begin(), extensions.end(),
caselessUniqueExts.begin(),
boost::is_iequal());

但是,它无法使用 MSVS2010 进行编译:

1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xlocale(626): error C2440: 'type cast' : cannot convert from 'unsigned char' to 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xlocale(2211) : see reference to function template instantiation '_Elem std::_Maklocchr<_Elem>(char,_Elem *,const std::_Locinfo::_Cvtvec &)' being compiled
1> with
1> [
1> _Elem=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xlocale(2210) : while compiling class template member function 'std::basic_string<_Elem,_Traits,_Ax> std::ctype<std::basic_string<_Elem,_Traits,_Ax>>::do_widen(char) const'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\locale(261) : see reference to class template instantiation 'std::ctype<_Elem>' being compiled
1> with
1> [
1> _Elem=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\locale(259) : see reference to function template instantiation '_Elem std::toupper<T1>(_Elem,const std::locale &)' being compiled
1> with
1> [
1> _Elem=std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> T1=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(2042) : see reference to function template instantiation 'bool boost::algorithm::is_iequal::operator ()<std::basic_string<_Elem,_Traits,_Ax>,std::basic_string<_Elem,_Traits,_Ax>>(const T1 &,const T2 &) const' being compiled
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>,
1> T1=std::basic_string<char,std::char_traits<char>,std::allocator<char>>,
1> T2=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(2070) : see reference to function template instantiation '_OutIt std::_Unique_copy<_InIt,_OutIt,_Pr>(_FwdIt,_FwdIt,_OutIt,_Pr,std::forward_iterator_tag)' being compiled
1> with
1> [
1> _OutIt=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
1> _InIt=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,
1> _Pr=boost::algorithm::is_iequal,
1> _FwdIt=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm(2094) : see reference to function template instantiation '_OutIt std::_Unique_copy1<std::basic_string<_Elem,_Traits,_Ax>*,_OutIt,_Pr>(_InIt,_InIt,_OutIt,_Pr,std::tr1::true_type)' being compiled
1> with
1> [
1> _OutIt=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>,
1> _Pr=boost::algorithm::is_iequal,
1> _InIt=std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
1> ]
1> c:\users\stv04463\documents\visual studio 2010\projects\pgp sandbox\pgp sandbox\main.cpp(64) : see reference to function template instantiation '_OutIt std::unique_copy<std::_Vector_iterator<_Myvec>,std::_Vector_iterator<_Myvec>,boost::algorithm::is_iequal>(_InIt,_InIt,_OutIt,_Pr)' being compiled
1> with
1> [
1> _OutIt=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
1> _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
1> _Pr=boost::algorithm::is_iequal
1> ]
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\xlocale(593): error C2440: 'type cast' : cannot convert from 'std::basic_string<_Elem,_Traits,_Ax>' to 'unsigned char'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xlocale(2229) : see reference to function template instantiation 'char std::_Maklocbyte<std::basic_string<_Elem,_Traits,_Ax>>(std::basic_string<_Elem,_Traits,_Ax>,const std::_Locinfo::_Cvtvec &)' being compiled
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\xlocale(2225) : while compiling class template member function 'char std::ctype<_Elem>::_Donarrow(_Elem,char) const'
1> with
1> [
1> _Elem=std::basic_string<char,std::char_traits<char>,std::allocator<char>>
1> ]

我是不是错过了一些基本的理解,还是不能将 std::unique_copyboost::is_iequal 结合起来?

我知道替代方案 - 我想知道为什么我会收到此编译器错误。

最佳答案

boost::is_iequal 谓词旨在用于单个字符,而不是像您正在做的那样用于整个字符串。

不幸的是,Boost String Algorithms 文档不是最好的,但您可以看到这个指定的 here : "定义元素比较谓词",其中"元素"指的是字符串字符。

在内部,boost::is_iequal 在其参数上调用 std::toupper,这仅适用于字符。

关于c++ - 使用 std::unique_copy 从 vector 中提取唯一的、不区分大小写的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11115973/

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