我不确定我在这里做错了什么:
vector<string> names;
...
string lh = "localhost";
vector<string>::iterator it = std::find(names.begin(), names.end(), lh);
if(it != names.end())
names.push_back(lh);
find
行会产生:
../source/ac-pki-4.cpp: In function ‘std::vector<std::basic_string<char> > GetServerAltNames()’:
../source/ac-pki-4.cpp:757:76: error: no matching function for call to ‘find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::basic_string<char>&)’
../source/ac-pki-4.cpp:757:76: note: candidate is:
In file included from /usr/include/c++/4.7/bits/locale_facets.h:50:0,
from /usr/include/c++/4.7/bits/basic_ios.h:39,
from /usr/include/c++/4.7/ios:45,
from /usr/include/c++/4.7/ostream:40,
from /usr/include/c++/4.7/iostream:40,
from /home/jwalton/ac/include/ac-common.h:33,
from ../source/ac-pki-4.cpp:1:
/usr/include/c++/4.7/bits/streambuf_iterator.h:371:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> > >::__type std::find(std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >, const _CharT2&)
/usr/include/c++/4.7/bits/streambuf_iterator.h:371:5: note: template argument deduction/substitution failed:
../source/ac-pki-4.cpp:757:76: note: ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >’ is not derived from ‘std::istreambuf_iterator<_CharT2, std::char_traits<_CharT> >’
make: *** [source/ac-pki-4.o] Error 1
我也尝试了下面的内容,但没有任何乐趣:
std::basic_string<char> lh = "localhost";
我尝试了一个const_iterator
没有快乐。
下面是我在Eclipse下看到的内容的截屏。
任何想法我做错了什么? (为什么在这种情况下使用istreambuf_iterator
?)。
答案 0 :(得分:7)
<iostream>
似乎包含一些重载std::find
的标题与<algorithm>
中的标题不同。要使用正确的,您必须包含<algorithm>
。取消注释此live example上的包含行,以查看我的意思(以及<iostream>
以便进一步调查。)请参阅libstdc++文档。