Solved

Searching through strings

Posted on 2004-10-13
5
191 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 400 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 100 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now