Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6477299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:55:54+00:00 2026-05-25T06:55:54+00:00

How do I correctly and efficiently return a pair from a newly inserted pair

  • 0

How do I correctly and efficiently return a pair from a newly inserted pair to a map?

inline pair<unsigned int, T> *createObj(unsigned int UID){
    static pair<unsigned int, T> ret;

    objList.insert(pair<unsigned int, T>(UID, T()));

    if (UID_Counter <= UID) 
        UID_Counter = UID+1; 
    ret = make_pair(UID, objList.find(UID)->second);
    return &ret;
}

The above returns a object to use, but whatever I change in ret does not change in the “real pair” in the map…

Basically what i want to achieve is:

  1. Insert new object Z into map A
  2. (“Child”-class) Change the stuff in the returned object Z
  3. In every update cycle, iterate through the objects in map A and use the actual data loaded into object “A-Z” when updating…
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T06:55:54+00:00Added an answer on May 25, 2026 at 6:55 am

    The particular overload of insert that you’re using returns an std::pair<iterator, bool>. In particular the first member of that pair is an iterator to either the element that was newly inserted or the element that was already present. Thus:

    pair<const unsigned int, T>&
    createObj(unsigned int UID)
    {
        auto inserted = objList.insert(pair<unsigned int, T>(UID, T()));
    
        if (UID_Counter <= UID) 
            UID_Counter = UID+1; 
    
        return *inserted.first;
    }
    

    Notice that I’m returning a reference where you were returning a pointer and that the key type is const unsigned int, not unsigned int. You can use map_type::value_type, too (where map_type is the type of your container).

    If you’re wondering why your code wasn’t working, it’s because you were storing a copy of the mapped object inside ret, so any modification through the pointer you returned would only affect that copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I efficiently catch and handle segmentation faults from C in an OSX
I'd like to say int x = magic(), y = moremagic(); return i =>
If I understand correctly, ClickOnce only checks for prerequisites with the first install of
I cannot correctly position the div form in my layout. By looking at my
My application correctly handles different kind of character sets, but only internally - when
I can correctly setup up a windows hook, but I get confused by the
The following code compiles correctly under VC++ 8 on XPSP3, but running it causes
If I understand correctly the .net runtime will always clean up after me. So
Edit--@Uri correctly pointed out that this was an abuse of annotations; trying to actually
i have a scroll view which represents a map, it has pins on it,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.