Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ find function for vector<unsigned char>

I want to find empty space char " " in my vector<unsigned char> message

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

And I get an error:

/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
like image 240
M.K. Avatar asked Dec 06 '22 20:12

M.K.


2 Answers

You should use ' ' instead of " ":

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

Note that " " is string literal, while ' ' is character literal. What you need to provide as third argument is character literal, because message is a vector of chars, not of strings.

like image 103
Nawaz Avatar answered Dec 20 '22 22:12

Nawaz


You are searching for a string, rather than an unsigned char.

Try this. Notice the single quotes.

pos = find(message.begin(), message.end(), ' ');
like image 35
Peter Alexander Avatar answered Dec 20 '22 22:12

Peter Alexander