Solved

Searching through strings

Posted on 2004-10-13
5
223 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

737 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