• Advertisement
Sign in to follow this  

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

This topic is 4476 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
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
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
Sign in to follow this  

  • Advertisement