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

Started by
2 comments, last by njpaul 18 years, 5 months ago
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)
Advertisement
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.
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
If you have it, I would use stdext::hash_map

This topic is closed to new replies.

Advertisement