?
Solved

Searching through strings

Posted on 2004-10-13
5
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

762 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