[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Searching through strings

Posted on 2004-10-13
5
Medium Priority
?
240 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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 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.
Suggested Courses

656 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