CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: stl nested maps

  1. #1
    Join Date
    May 2015
    Posts
    450

    stl nested maps

    I have put some kind of pseudocode for the issue I am facing

    I have a hybrid map, I want to insert a particular element from this map to another map.

    typedef std::map<int,int> MapOfInts;
    typedef std::map<int,MapOfInts> HybridMapOfInts;

    Code:
    int main ()
    {
      MapOfInts mymap;
      MapOfInts mylatmap;
      HybridMapOfInts myNewmap;
      MapOfInts::iterator it; 
      HybridMapOfInts::iterator itNew;
    
      mymap[5]=50;
    
      mylatmap[1]=10;
      mylatmap[2]=20;
    
      myNewmap.insert(std::make_pair(1, mylatmap) );  
    
      itNew = myNewmap.find(1);
    
    //Following gives error, is there any efficient simple right way to do this ??
      mymap.insert(itNew->second);
    }
    Last line gives error , is there any efficient simple right way to do this ??
    Last edited by pdk5; November 26th, 2015 at 12:12 PM.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: stl nested maps

    You have a type issue here.

    - itNew is an iterator to a pair<int, MapOfInts>.
    - myMap IS a MapOfInts.

    What exactly do you want "mymap.insert(itNew);" to do?
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    May 2015
    Posts
    450

    Re: stl nested maps

    Thanks a lot Monarch for the inputs.

    Yes sorry, but my last statemaent should have been something like:
    mymap.insert(itNew->second);

    I want to add (append) the element itNew from the mylatmap to the the mymap.

    #####
    Basically scenario is, I have final "mymap". I am sending some info in the Request message ( with some Identity) and storing the "data" which is similar to my map in temp storage. Once response is received, I retrive the data from temp storage and populate in final "mymap". So i use the "Identity" as key and get the value which is populated into the final map.
    Last edited by pdk5; November 26th, 2015 at 12:13 PM.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: stl nested maps

    Please try to provide actual code that you have written for real. These little inaccuracies make helping you difficult.

    Anyways, map has an insert variant that takes an iterator range, so you can simply do:
    Code:
    mymap.insert((itNew->second).begin(), (itNew->second).end());
    It's as efficient as inserting a container (non-destructivelly) into another can get, which is to say: o(n).
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: stl nested maps

    Quote Originally Posted by monarch_dodra
    It's as efficient as inserting a container (non-destructivelly) into another can get, which is to say: o(n).
    Not quite: there may be as many insertions as there are objects in the range, but each insertion would normnally take logarithmic time.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: stl nested maps

    Quote Originally Posted by laserlight View Post
    Not quite: there may be as many insertions as there are objects in the range, but each insertion would normnally take logarithmic time.
    Yes, very true. An oversight on my part. I just meant to say that as a general rule, you shouldn't hope for better than o(N). (At least, in a non-destructive fashion, and containers that don't share their internals, AFAIK).

    That said, I was looking at the standards.
    C++98 has this to say: "Nlog(size()+N) (N is the distance from i to j) in general; linear if [i, j) is sorted according to value_comp()"
    but C++11 has this relaxed to simply: "N log(a.size() + N) (N has the value distance(i, j)"

    Although cplusplus.com does state: "Implementations may optimize if the range is already sorted."

    The more you know I guess. It's interesting to discover this little changes from 98 to 11.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  7. #7
    Join Date
    May 2015
    Posts
    450

    Re: stl nested maps

    @monarch: Thanks a lot for all the inputs. Sorry for the delay.

    Btw now i have provided some parts of my actual code
    Code:
    CPCRF.h:
    struct PolicyRuleInfo{
    	BearerQoSInfo stBearerQoS;
    	TFTInfo stTFTInfo;
    
    	PolicyRuleInfo(){};
    	PolicyRuleInfo( BearerQoSInfo const& qos, TFTInfo const& tft)
           : stBearerQoS(qos), stTFTInfo(tft)
        { }
    };
    
    typedef map<string, PolicyRuleInfo> listOfPolicyRuleInfo;
    
    struct IPAddressPolicyRulesInfo{
    	CIPAddress sIpAddress;
    	listOfPolicyRuleInfo policyRules;
    	IPAddressPolicyRulesInfo(){};
    	IPAddressPolicyRulesInfo(CIPAddress ipaddr, string policyRuleName, PolicyRuleInfo policyRule): sIpAddress(ipaddr){policyRules[policyRuleName]=policyRule;};
    
    	void addPolicycyRule(string policyRuleName, PolicyRuleInfo policyRule) { policyRules[policyRuleName]=policyRule; }
    };
    
    // Apn to PolicyRules
    typedef map<string, IPAddressPolicyRulesInfo> APN2PolicyRules;
    
    // Imsi to APN2PolicyRules Info
    typedef map<string, APN2PolicyRules>	 IMSI2APNPolicyRules;
    
    // GxSessionId to IMSI2APNPolicyRules Info
    typedef map<string, IMSI2APNPolicyRules>	 GxId2IMSI2APNPolicyRules;
    
    class CPCRF : public CBaseClass
    {
    public:
    :
    :
    	CPCRF();
    
    	// list of PolicyRules configured for the User
        // ipAddress to User info
    	static IMSI2APNPolicyRules	 m_mIMSI2ApnPolicyRuleInfo;
    
    	static GxId2IMSI2APNPolicyRules m_mGxId2IMSI2APNPolicyRules;
    }
    
    CPCRF.cpp
    CompConf::Oam2::IMessagePtr CPCRF::OnAddRequest(CompConf::Oam2::IRequestPtr const& req)
    {
    	PolicyRuleInfo sPolicyRule(stQoSInfo, stTFT);
    
    	sIpAddPolicyRule.addPolicycyRule(sApn, sPolicyRule);
    
    	sApn2PolicyRule.insert(std::make_pair(sApn, sIpAddPolicyRule));
    
    	IMSI2APNPolicyRules sImsi2Apn;
    
    	sImsi2Apn.insert(std::make_pair(sIMSI, sApn2PolicyRule));
    
    	string sGxsessionId;
    
    	vector<string> stVectTFT;
    
    	stVectTFT.push_back(sTft);
    
    	string sUeIpAddress;
    
    	
    	GxRARequest(sGxsessionId, sImsi, sDedBearerId, sUeIpAddress, true, stVectTFT, stQoSInfo);
    
           // Add this info in the temp map
    	m_mGxId2IMSI2APNPolicyRules.insert(std::make_pair(sGxsessionId, sImsi2Apn));
    }
    
    CGxDia.cpp			
    DiaRes CGxDiaSvr::RAAnswer(CDiaMessage & sMsg)
    {
    ::
    	if(iTerGxId != CPCRF::m_mGxId2IMSI2APNPolicyRules.end())
    	{
    		//copy this to actual map
            CPCRF::m_mIMSI2ApnPolicyRuleInfo.insert();
    			
    	    //delete this info in the temp map
    		CPCRF::m_mGxId2IMSI2APNPolicyRules.erase(sSessionId);
    	}
    }

  8. #8
    Join Date
    Oct 2008
    Posts
    1,456

    Re: stl nested maps

    Quote Originally Posted by monarch_dodra View Post
    It's interesting to discover this little changes from 98 to 11.
    here is the corresponding library defect issue

    anyway, note that O(n) "sorted insertions" are still guaranteed at container construction...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured