Jump to content
  • Advertisement
Sign in to follow this  
toddhd

Using Find() and Vectorstring

This topic is 5055 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!