Sign in to follow this  

Using Find() and Vectorstring

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I saw an example of how to use Find() from algorithm.h in conjunction with a vector in order to search for a given integer value. Here is the example: http://cplus.about.com/library/weekly/aa050102j.htm Is there a way to search a vector<string> in a similar manner? Right now I'm just looping through and doing a string.compare on each iteration, but I was wondering if there was a faster and/or more elegant way to do this?

Share this post


Link to post
Share on other sites
The Find() funtion takes 3 arguments, the start and end iterators, and the value to be searched for:

result = find( L.begin( ), L.end( ), 10 );

But when I try to enter a string or string.c_str() as the third (value) parameter, there is no overloaded option that accepts that.

Can you provide an example of how to do this with a std::string?

Share this post


Link to post
Share on other sites
Quote:
Original post by toddhd
The Find() funtion takes 3 arguments, the start and end iterators, and the value to be searched for:

result = find( L.begin( ), L.end( ), 10 );

But when I try to enter a string or string.c_str() as the third (value) parameter, there is no overloaded option that accepts that.

Can you provide an example of how to do this with a std::string?


You must be doing something wrong, can you post it (including variable declerations)?

All this compiled fine:

#include <iostream>
#include <string>
#include <vector>

char * source_names[] = { "Bob" , "Pete" , "David" , "Mike" , "Ernest" };
const unsigned int source_count = sizeof( source_names ) / sizeof( source_names[0] );

int main ( int argc , char ** argv )
{
std::vector< std::string > str_names( source_count , std::string() );
std::vector< char * > cptr_names( source_count , 0 );
for ( unsigned int i = 0 ; i < source_count ; ++i )
{
str_names[ i ] = source_names[ i ];
cptr_names[ i ] = source_names[ i ];
}

std::vector< std::string >::iterator result1 = std::find( str_names.begin() , str_names.end() , "Pete" );
std::cout << "result1: " << *result1 << std::endl;

std::vector< std::string >::iterator result2 = std::find( str_names.begin() , str_names.end() , std::string("Bob") );
std::cout << "result2: " << *result2 << std::endl;


std::vector< char * >::iterator result3 = std::find( cptr_names.begin() , cptr_names.end() , "David" );
std::cout << "result3: " << *result3 << std::endl;

std::vector< char * >::iterator result4 = std::find( cptr_names.begin() , cptr_names.end() , std::string("Mike") );
std::cout << "result4: " << *result4 << std::endl;

std::string Ernest = "Ernest";

std::vector< std::string >::iterator result5 = std::find( str_names.begin() , str_names.end() , Ernest );
std::vector< std::string >::iterator result6 = std::find( str_names.begin() , str_names.end() , Ernest.c_str() );
std::vector< char * >::iterator result7 = std::find( cptr_names.begin() , cptr_names.end() , Ernest );
std::vector< char * >::iterator result8 = std::find( cptr_names.begin() , cptr_names.end() , Ernest.c_str() );

std::cin.get();
return 0;
}

Share this post


Link to post
Share on other sites

This topic is 4836 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this