C++ 查找矢量<无符号字符的函数>

c++ find function for vector<unsigned char>

本文关键字:字符 gt 函数 无符号 lt 查找 C++      更新时间:2023-10-16

我想在我的vector<unsigned char> message 中找到空白字符" "

vector<unsigned char>::iterator pos;
pos = find(message.begin(), message.end(), " ");

我得到一个错误:

/usr/include/c++/4.5/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char> >, _Tp = char [2]]’:
/usr/include/c++/4.5/bits/stl_algo.h:4209:45:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char> >, _Tp = char [2]]’
../source/InveritasServer.cpp:107:49:   instantiated from here
/usr/include/c++/4.5/bits/stl_algo.h:158:4: error: ISO C++ forbids comparison between pointer and integer
/usr/include/c++/4.5/bits/stl_algo.h:4209:45:   instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char> >, _Tp = char [2]]’
../source/InveritasServer.cpp:107:49:   instantiated from here
/usr/include/c++/4.5/bits/stl_algo.h:162:4: error: ISO C++ forbids comparison between pointer and integer
/usr/include/c++/4.5/bits/stl_algo.h:166:4: error: ISO C++ forbids comparison between pointer and integer
/usr/include/c++/4.5/bits/stl_algo.h:170:4: error: ISO C++ forbids comparison between pointer and integer
/usr/include/c++/4.5/bits/stl_algo.h:178:4: error: ISO C++ forbids comparison between pointer and integer
/usr/include/c++/4.5/bits/stl_algo.h:182:4: error: ISO C++ forbids comparison between pointer and integer
/usr/include/c++/4.5/bits/stl_algo.h:186:4: error: ISO C++ forbids comparison between pointer and integer

您应该使用' '而不是" ":

pos = find(message.begin(), message.end(), ' ');

注意," "是字符串文字,而' '是字符文字。作为第三个参数,您需要提供的是字符文字,因为message是字符的矢量,而不是字符串的矢量。

您正在搜索字符串,而不是unsigned char

试试这个。注意单引号。

pos = find(message.begin(), message.end(), ' ');

它是一个"unsigned char"的向量,您正试图在其中找到一个字符串。发送一个无符号字符类型。