?
Solved

Searching through strings

Posted on 2004-10-13
5
Medium Priority
?
242 Views
Last Modified: 2010-04-01

I have 8 different scenarios.  If the user selects the first option, I hvae to search through the names of the 5 group1 files...if the 2nd option is selected, I have to search through the names of group2 files...etc

I currently have a switch statement to handle which option is selected.  But I am trying to determine, rather than having a separate search handler for each one....how would I handle the searching....?  I have 8 separate char* arrays (which I could change to something else...)

I was thinking about, for example, if option 1 is selected, then store the char* array into a generic char* array which can then be used for searching after the switch statement to avoid having 8 different search loops.  
Then I was told that copying arrays is not very efficient and it may be best to use vectors??
0
Comment
Question by:jewee
  • 3
5 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12300499
You could arrange your arrays in an array and use the option as an index into that array

char* search_in_array ( char* pszToSearch, char* arr[]) {

//...
}

int nOption;

char* apsz1 [] = { ....};
char* apsz2 [] = { ....};
char* apsz3 [] = { ....};
//...
char* apsz8 [] = { ....};

char** appsz[8] = { apsz1, apsz2, .... apsz8};

char* res = search_in_array ( "somestring", appsz[nOption]);
0
 
LVL 86

Expert Comment

by:jkr
ID: 12300546
>>Then I was told that copying arrays is not very efficient and it may be best to use vectors??

I'd recommend to move to STL containers in general.
0
 

Author Comment

by:jewee
ID: 12300554
Would it be best, rather than having 8 arrays for each file group, to do something else, like use vectors?  Which would be the most efficient??
0
 
LVL 86

Accepted Solution

by:
jkr earned 1600 total points
ID: 12300599
>> Would it be best, rather than having 8 arrays for each file group, to do something else, like use vectors?

As I wrote, you can re-group the 8 arrays in a single array of arrays without copying them. The same applies to vectors, you'd have to use an array of 8 vectors or a vector of vectors. STL containers are more convenient to handle in terms of coding but efficiency in terms of "speed" will hardly differ for both approaches.
0
 
LVL 12

Assisted Solution

by:OnegaZhang
OnegaZhang earned 400 total points
ID: 12316316
use std::string instead of char* to avoid memory leakage,
use referrence instead of value to avoid copy overload

bool mysearch( std::vector<std::string> &array, std::vector<std::string> &filelist>
{
...
}

welcome to www.fruitfruit.com
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question