#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<map>

using namespace std;
struct PoiDetails
{
   int x;
   std::string s; 
};
int main()
{

   struct PoiDetails oBj;
   std::map<std::string, PoiDetails> vName;

   oBj.x = 1; oBj.s = "str1";
   vName["Jhon"] = oBj;
   oBj.x = 2; oBj.s = "str2";
   vName["Ben F"] = oBj;
   oBj.x = 3; oBj.s = "str3";
   vName["Shown"] = oBj;
   oBj.x = 4; oBj.s = "str4";
   vName["peter"] = oBj;
   oBj.x = 5; oBj.s = "str5";
   vName["juk"] = oBj;
   oBj.x = 6; oBj.s = "str6";
   vName["peter"] = oBj;


   //std::set<std::string>::iterator it;
   std::map<std::string, PoiDetails>::iterator it;

   int i;
   for(i=0, it=vName.begin(); (it!= vName.end()) && (i<6); i++, it++)
   {
      cout<<"Detail " << i << " ";
      cout << it->first << "\t";
      cout<<"x = "<< it->second.x <<"\t";
      cout<<"s = "<< it->second.s <<"\n";

   }
     return 0;

}

I want to use multimap instead of "std::map<std::string, PoiDetails> vName;"
this is becoz, the key object "vName["peter"] = oBj;" is missing as the key has the same name occured twice.

Could you please let me know how I can use multimap to get rid of this.
Multimap for the given example worth helps a lot.
thanks well in advance

Recommended Answers

All 2 Replies

#include <map>
#include <string>
#include <iostream>

using namespace std;

struct PoiDetails
{
   int x;
   string s;
};

int main()
{
    PoiDetails oBj;
    multimap<string, PoiDetails> vName;

    // Add the contents.
    oBj.x = 1; oBj.s = "str1";
    vName.insert(pair<string, PoiDetails>("Jhon", oBj));

    oBj.x = 2; oBj.s = "str2";
    vName.insert(pair<string, PoiDetails>("Ben F", oBj));

    oBj.x = 3; oBj.s = "str3";
    vName.insert(pair<string, PoiDetails>("Shown", oBj));

    oBj.x = 4; oBj.s = "str4";
    vName.insert(pair<string, PoiDetails>("peter", oBj));

    oBj.x = 5; oBj.s = "str5";
    vName.insert(pair<string, PoiDetails>("juk", oBj));

    oBj.x = 6; oBj.s = "str6";
    vName.insert(pair<string, PoiDetails>("peter", oBj));

    // Show the contents.
    for (multimap<string, PoiDetails>::iterator it = vName.begin(); it != vName.end(); it++)
    {
        cout<<"Detail " << distance(vName.begin(), it) << " ";
        cout << it->first << "\t";
        cout <<"x = "<< it->second.x <<"\t";
        cout <<"s = "<< it->second.s <<"\n";
    }

    return 0;
}

What is the hangup? Change line 17 to std::multimap<std::string, PoiDetails> vName; and change line 34 to std::multimap<std::string, PoiDetails>::iterator it;. That should be all you need to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.