STL Map Value Constructors

I have an X class that I would like to put in an STL map like std :: map. An STL card must have an X stored in memory somewhere, so I'm looking for an efficient way (runtime and memory) to create an X and save it on the map.

I noticed that the following code, where x is an object of type X and stlMap, is a map of type std :: map:

stlMap["test"] = x;

The following results are called up:

  • Default constructor X
  • X Copy Constructor
  • X Copy Constructor
  • X destructor
  • X destructor
  • Assignment Constructor X
  • X destructor

Why are so many X objects being created?

Is it an inefficient use of time and memory?

Is there a better way to put an object on a map? Maybe changing the map will be displaying rows in x *?

+3
source share
5

stlMap.insert( map<string, X>::value_type("test", x) ):

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

using namespace std;

class X
{
public:
X() { cout << "X default constructor" << endl; }
~X() { cout << "X destructor" << endl; }
X( const X& other ) { cout << "X copy constructor" << endl; }
X& operator=( const X& other ) { cout << "X copy-assignment operator" << endl; }
int x;
};


int main()
{
X x;
map< string, X > stlMap;

cout << "INSERT BEGIN" << endl;
stlMap.insert( map< string, X >::value_type( "test", x ) );
cout << "INSERT END" << endl;
stlMap.clear();
cout << "ASSIGN BEGIN" << endl;
stlMap["test"] = x;
cout << "ASSIGN END" << endl;

return 0;
}

g++, :

  • X
  • X
  • X destructor

: ArunSaha . insert() , :

  • X
  • X
  • X
  • X destructor
  • X destructor
  • X
+5

STL , , , .

, ( ).

+3

:

#include <iostream>
#include <map>

class X
{
    public:
     X()                    { std::cout << "Default Construct\n";}
    ~X()                    { std::cout << "Destroy\n";}
     X(X const&)            { std::cout << "Copy Construct\n";}
     X& operator=(X const&) { std::cout << "Assignment\n";}
};


int main()
{
    std::map<int,X>     store;
    X                   x;
    X                   y;

    std::cout << "Inserting x\n";
    store[1]    = x;
    std::cout << "Finished Insert\n";
    std::cout << "Inserting y\n";
    store[1]    = y;
    std::cout << "Finished Insert\n";
}

:

Default Construct                    Building X
Default Construct                    Building Y
Inserting x                          ---- Start of an insert
Default Construct                    -------- Work to insert an item that is not in the map
Copy Construct                        
Copy Construct                       
Destroy                              
Destroy                             -------- Finished work to insert a new item
Assignment                          Assign x into the internal object
Finished Insert                     ---- Done
Inserting y                         ---- Start of an insert
Assignment                          Assign y onto the internal object.
Finished Insert                     ---- Done
Destroy                             Destroy y
Destroy                             Destroy x
Destroy                             Destroy map containing one X
+1

STL . . , , - X .

std::map X. X*, . , .

0

? , . , .

++ 0x . X ( X x(X&& m) { ... }, stlMap["test"] = std::move(x);. X, . ++ 0x , , .

0

Source: https://habr.com/ru/post/1766037/


All Articles