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

  • Home
  • SEARCH
  • 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 8958505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:13:42+00:00 2026-06-15T15:13:42+00:00

I have a std::map of objects whose instances are very expensive to construct. (In

  • 0

I have a std::map of objects whose instances are very expensive to construct. (In real life they require several accesses to a database.)

I want to access an element of the map, or create it if it doesn’t exist. This sounds like a job for std::map::insert, except that the expensive object is constructed unnecessarily and then thrown away if the element exists. To illustrate:

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

struct CexpensiveObject
{    
    CexpensiveObject(const char* args="default"):args_(args)
    {
        std::cout << "Constructor: CexpensiveObject(" << args << ")" << std::endl;
    }
    CexpensiveObject( const CexpensiveObject& other )
    {
        std::cout << "Copy Constructor: CexpensiveObject other.args_ = " << other.args_ << "." << std::endl;
        args_ = other.args_;
    }
    ~CexpensiveObject()
    {
        std::cout << "Destructor: CexpensiveObject args_ = " << args_ << "." << std::endl;
    }
    const char* args_;
};

// entry point
int main() 
{
    typedef std::map<std::string, CexpensiveObject> mymaptype;   
    mymaptype mymap;
    std::pair<mymaptype::iterator, bool> insertionResult;

    std::cout << "First insertion" << std::endl;
    insertionResult = mymap.insert( mymaptype::value_type( "foobar", CexpensiveObject("first") ) );
    std::cout << "Was it inserted? " << (insertionResult.second?"yes":"no") << std::endl;

    std::cout << "Second insertion" << std::endl;
    insertionResult = mymap.insert( mymaptype::value_type("foobar", CexpensiveObject("second") ) );
    std::cout << "Was it inserted? " << (insertionResult.second?"yes":"no") << std::endl;
}

Results:

First insertion
Constructor: CexpensiveObject(first)
Copy Constructor: CexpensiveObject other.args_ = first.
Copy Constructor: CexpensiveObject other.args_ = first.
Destructor: CexpensiveObject args_ = first.
Destructor: CexpensiveObject args_ = first.
Was it inserted? yes
Second insertion
Constructor: CexpensiveObject(second)
Copy Constructor: CexpensiveObject other.args_ = second.
Destructor: CexpensiveObject args_ = second.
Destructor: CexpensiveObject args_ = second.
Was it inserted? no
Destructor: CexpensiveObject args_ = first.

There’s more copying and destroying than I expected, but critically an instance CexpensiveObject is constructed and then thrown away if an element with the same key exists in the ma.

Am I misusing std::map::insert, or do I have to use std::map::find to check whether an element with the same key exists before I instantiate a CexpensiveObject instance?

  • 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-06-15T15:13:43+00:00Added an answer on June 15, 2026 at 3:13 pm

    It’s constructed before you even get to insert, when you call CexpensiveObject("second"). You’re passing in the extraneous object! (And then it’s copied as the value_type is passed to insert.)

    Instead of insert, use find. If you find the item at the desired key, then you’re finished. If not, then insert it.

    auto it = mymap.find("foobar");
    if (it == mymap.end())
      mymap.insert(mymaptype::value_type("foobar", CexpensiveObject("second")));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a global object holding several c++ std::map objects. This object is supposed
I have a std::map<std::string, HANDLE> SampleMap which stores HANDLE objects as value. After the
So I have a container(any kind, probably std::map or std::vector) which contains objects of
Suppose, I have a very large std::map< unsigned int, Foo > FooDB , which
I have a std::set container whose elements are objects of the following class: class
I have a std::map of HANDLE objects, std::map<int, HANDLE> MyMap; I have to wait
let's say I have std::map< std::string, std::string > m_someMap as a private member variable
I have a std::map<std::string, myClass*> myMap then I am inserting like follow: if(!myKey.empty()) {
I have a std::map that I'm using to store values for x and y
i have a std::map and i am using iterator to find a certain key,value

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.