Sign in to follow this  
andreib

Searching for a value in an array... key is string

Recommended Posts

andreib    147
Hi everyone! I need to up to thousands of items in an array/vector. Here is what one item looks like:
struct Patient {
    std::string m_StudyOid;
    std::string m_Filename;
}
So somthing like
vector<Patient>
would work, however I need to _quickly_ find all items in the list that have a specific m_StudyOid value. Basically, I need to search the list. I haven't done this before. I know it's called hash map or somthing... what is the fastest way to do this? thanks! (Yes, I'm doing DICOM work if that's what you guessed)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster

std::map< std::string, std::string > PatientMap;
std::map< std::string, std::string >::const_iterator iter;

...
PatientMap["id1"] = "file1";
PatientMap["id2"] = "file2";
...

if ((iter = PatientMap.find("id") != PatientMap.end())
{
// Found
std::string sFileName = iter->second;
...
}
else
{
// Not found
}


Note: If you have an implementation, replace with hash_map.

Share this post


Link to post
Share on other sites
Conner McCloud    1135
std::map has the possible disadvantage of being an ordered container. Keep that in mind if you require a specific ordering. If the order doesn't matter, then std::map is definately the easiest solution, espcially if m_StudyOid is the primary method of accessing elements.

CM

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this