vector<string> and find
-
I have a vector of strings which I want to search for a particular string. I am using: vector::iterator it = find( vecStrings.begin(), vecStrings.end(), sMax ); sMax is a std::string When I compile this (and it is specifically this line causing the problem) I get the following: c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::vector<_Ty,_A> &,const class std::vector<_Ty,_A> &)' : could not deduce template argument for 'const class std::vector <_Ty,_A> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' C:\test\test.cpp(6846) : see reference to function template instantiation 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *__cdecl std::find(class std::basic_string<char,struct std::char_trai ts<char>,class std::allocator<char> > *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocat or<_Ty> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' C:\test\test.cpp(6846) : see reference to function template instantiation 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *__cdecl std::find(class std::basic_string<char,struct std::char_trai ts<char>,class std::allocator<char> > *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::istream_iterator<_U,_E,_Tr> &,const class std::istream_iterator<_U,_E,_Tr> &)' : could not deduce template argument fo r 'const class std::istream_iterator<_U,_E,_Tr
-
I have a vector of strings which I want to search for a particular string. I am using: vector::iterator it = find( vecStrings.begin(), vecStrings.end(), sMax ); sMax is a std::string When I compile this (and it is specifically this line causing the problem) I get the following: c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::vector<_Ty,_A> &,const class std::vector<_Ty,_A> &)' : could not deduce template argument for 'const class std::vector <_Ty,_A> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' C:\test\test.cpp(6846) : see reference to function template instantiation 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *__cdecl std::find(class std::basic_string<char,struct std::char_trai ts<char>,class std::allocator<char> > *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::allocator<_Ty> &,const class std::allocator<_U> &)' : could not deduce template argument for 'const class std::allocat or<_Ty> &' from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' C:\test\test.cpp(6846) : see reference to function template instantiation 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *__cdecl std::find(class std::basic_string<char,struct std::char_trai ts<char>,class std::allocator<char> > *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *,const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)' being compiled c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::istream_iterator<_U,_E,_Tr> &,const class std::istream_iterator<_U,_E,_Tr> &)' : could not deduce template argument fo r 'const class std::istream_iterator<_U,_E,_Tr
Hi, Your compiler's first message is explanative:
Jonnster wrote:
I get the following: c:\program files\microsoft visual studio\vc98\include\algorithm(43) : error C2784: 'bool __cdecl std::operator ==(const class std::vector<_Ty,_A> &,const class std::vector<_Ty,_A> &)' : could not deduce template argument for 'const class std::vector
Make it happy with:
std::vectorstd::string::iterator it = std::find(vecStrings.begin(), vecStrings.end(), sMax);
With C++0x (VS2010 or gcc 4.5) you can write:
auto it = std::find(vs.begin(), vs.end(), sMax);
which instructs the compiler to deduce the type of
it
. With previous compilers usetypedef
to get simpler code:typedef std::vectorstd::string VS;
VS vs;
// populate vs
VS::iterator it = std::find(vs.begin(), vs.end(), sMax);cheers, AR C++0x wording edited
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
modified on Friday, October 22, 2010 4:24 AM