When I declare std::vector<std::string> v
, i can easily check if it contains given char sequence
if ( std::find( v.begin(), v.end(), "abc" ) != v.end() )
{
// some logic if contains
}
but if i use std::vector<const char*> v
and try to apply the same logic to find given char sequence my code does not work properly.
What causes that issue? How can I solve that problem?
EDIT
The fastest solution (I care about performance, thats why i said no to std::string
)
bool FindStringInArray( const DynamicArray<const char*>& array, const char* pStr )
{
return ( std::find_if( array.cbegin(), array.cend(), [&]( const char* p )
{ return strcmp( p, pStr ) == 0; } ) != array.cend() ) ? true : false;
}
std::find
calls operator==
, which for const char *
is defined to compare pointer values, not contents. (After all, who says that the pointed-to thing is a string? It could be a single char or even a one-past-the-end pointer to an array.)
You'll have to use strcmp
with std::find_if
or iterate over the vector manually.