Re: std::multimap with composite key?
On Jan 19, 2:28 pm, "Daniel T." <danie...@earthlink.net> wrote:
On Jan 19, 9:13 am, PeteOlcott <PeteOlc...@gmail.com> wrote:
I want to make a std::multimapand use two std::string data members as
the key to the data record, is there a standard C++ way to do this?
struct NameValuePair
{
std::string Name;
std::string Value;
std::string ParentName;
std::string ParentValue;
};
This is semantically what I want to do, what is the correct standard C+
+ syntax?
std::mulitmap<std::string Name + std::string Value, NameValuePair nvp>
data;
In addition to the other advice given. Note that if you use a map, the
name + value in the key won't be connected to the name + value in the
struct except by coincidence.
Is it your intention to be able to find all the objects with the same
name or same value? You couldn't do that using a map, you could only
(easily) find all the object with the same name + value pair.- Hide quote=
d text -
- Show quoted text -
// Here is my solution:
#include <stdio.h>
#include <map>
#include <string>
struct NameValuePair
{
std::string Name;
std::string Value;
};
int main()
{
NameValuePair nvp;
std::multimap<std::string, NameValuePair> nvp_multimap;
nvp.Name = "Name"; nvp.Value = "Pete";
nvp_multimap.insert(std::make_pair(nvp.Name + nvp.Value, nvp));
nvp.Name = "Name"; nvp.Value = "Pete";
nvp_multimap.insert(std::make_pair(nvp.Name + nvp.Value, nvp));
printf("nvp_map.size(%d)\n", nvp_multimap.size());
std::multimap<std::string, NameValuePair>::iterator result;
result = nvp_multimap.find("NamePete");
if (result != nvp_multimap.end())
printf("Found It!\n");
else
printf("Not Found!\n");
}