编辑2:添加#include <algorithm>
修复了代码。
我正在尝试匹配字符串,但是我收到的错误表明我没有使用std::find
函数的正确数据类型。就我的目的而言,矢量并不一定必须是常数。
bool string_in_vector(const std::string &str,std::vector<std::string> &arr)
{
return std::find(std::begin(arr),std::end(arr),str) != std::end(arr);
}
我也尝试过这种常量矢量类型:
bool string_in_vector(const std::string &str,const std::vector<std::string> &arr)
{
return std::find(std::begin(arr),std::end(arr),str) != std::end(arr);
}
和
bool string_in_vector(const std::string &str,std::vector<std::string>::const_iterator arr)
{
return std::find(std::begin(arr),std::end(arr),str) != std::end(arr);
}
但是我没有获得正确的数据类型。对于上面的第一个和第二个函数,std::find
会引发错误,当我跟std::begin
<std::string>
(第三个函数)时,::const_iterator
会引发错误。所有这些错误都是&#34;没有匹配的功能&#34;。
应该采取什么措施来解决这个问题?
编辑(错误消息): || === Build:在thtag中调试(编译器:GNU GCC编译器)=== |
/home/user/workspace/thtag/main.cpp||In function ‘bool string_in_vector(const string&, std::vector<std::basic_string<char> >&)’:|
/home/user/workspace/thtag/main.cpp|22|error: no matching function for call to ‘find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, const string&)’|
/home/user/workspace/thtag/main.cpp|22|note: candidate is:|
/usr/include/c++/4.8/bits/streambuf_iterator.h|369|note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)|
/usr/include/c++/4.8/bits/streambuf_iterator.h|369|note: template argument deduction/substitution failed:|
/home/user/workspace/thtag/main.cpp|22|note: ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >’ is not derived from ‘std::istreambuf_iterator<_CharT>’|
/home/user/workspace/thtag/main.cpp||In function ‘void read_pdf(const char*)’:|
/home/user/workspace/thtag/main.cpp|74|warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]|
/home/user/workspace/thtag/main.cpp|79|warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]|
/home/user/workspace/thtag/main.cpp||In function ‘bool string_in_vector(const string&, std::vector<std::basic_string<char> >&)’:|
/home/user/workspace/thtag/main.cpp|23|warning: control reaches end of non-void function [-Wreturn-type]|
||=== Build failed: 1 error(s), 3 warning(s) (0 minute(s), 1 second(s)) ===|