C ++ map. insert has different parameter types, and the number of constructor and destructor is different. map. insert

Source: Internet
Author: User

C ++ map. insert has different parameter types, and the number of constructor and destructor is different. map. insert
1. parameter passing Method

When insert is used as map interpolation, the insert parameter may include the following:

  • Make_pair generation object
  • Pair (key_type, value_type) generation object
  • Pair (const key_type, value_type) generation object
  • Map <key_type, value_type>: value_type generate object

Different parameter passing results in different constructor/destructor calls.

2. Test code and result/* different insert passing parameter types in map, resulting in different number of constructor/destructor calls. */# Include <iostream> # include <vector> # include <map> # include <string> using namespace std; class Data {public: Data (void) {cout <"Constructor" <endl;} Data (const Data & other) {cout <"Copy Constructor" <endl ;} data & operator = (const Data & other) {cout <"Assignment operator" <endl; return * this ;}~ Data (void) {cout <"Destructor" <endl ;}}; class Tracker {public: Tracker (string name) {_ name = name; cout <"### Testing" <name <endl ;}~ Tracker (void) {cout <"### Done" <endl;} private: string _ name ;}; int main () {Tracker tracker ("insert data using make_pair"); map <int, Data> c; Data d = Data (); cout <"---- Begin ----" <endl; c. insert (make_pair (1, d); cout <"----- End -----" <endl ;}{ Tracker tracker ("insert data using pair <key_type, value_type> "); map <int, Data> c; Data d = Data (); cout <"---- Begin ----" <endl; c. insert (pair <int, Data> (1, d); cout <"----- End -----" <endl ;} {Tracker tracker ("insert data using pair <const key_type, value_type>"); map <int, Data> c; Data d = Data (); cout <"---- Begin ----" <endl; c. insert (pair <const int, Data> (1, d); cout <"----- End -----" <endl ;} {Tracker tracker ("insert data using map <key_type, value_type >:: value_type"); map <int, Data> c; Data d = Data (); cout <"---- Begin ----" <endl; c. insert (map <int, Data >:: value_type (1, d); cout <"----- End -----" <endl;} return 0 ;}View Code

3. Call Process Analysis 3.1 value_type Copy Constructor twice to generate a Temporary Variable

Detailed comparison of value_type after decomposition

3.2 pair (const key_type, value_type) is the same as value_type.

Cause: value_type is defined as: pair <const key_type, mapped_type>

See http://www.cplusplus.com/reference/map/map/

3.3 pair (key_type, value_type) 3 times Copy Constructor generates 2 temporary variables

According to the process disassembly, we can see that the non-const key_type causes the insert Process to add a Copy Constructor.

3.4 make_pair Copy Constructor 4 times to generate 3 temporary variables

The new Copy Constructor comes from the make_pair process.

The make_pair process is Constructs a pair object with its first element setXAnd its second element setY.

The pseudocode is as follows:

template <class T1,class T2>  pair<T1,T2> make_pair (T1 x, T2 y)  {    return ( pair<T1,T2>(x,y) );  }

See: http://www.cplusplus.com/reference/utility/make_pair? Kw = make_pair

The key in make_pair uses the const variable, and the Copy Constructor In the insert phase is not reduced.

const int k = 1;c.insert(make_pair(k, d));
4. Result Analysis
Class Design, constructor and destructor c/c ++

Class is a C ++ mechanism for programmers to express custom data types. Similar to the structure in C, C ++ class

Supports data abstraction and object-oriented programming. In a sense, it is also a set of data types.

And implementation.

I. Design of Classes
1. Class Declaration

Class Name
{
Private: // private
...
Public: // public
...
};

2. class member
In C ++ classes, all Defined variables and functions are members of the class. If it is a variable, we call it

If the data member is a function, we call it a member function.
3. Visibility of Class Members
Private and public access controllers determine the visibility of members. Set by an Access Controller

The access status will continue until the next Access Controller appears, or the end of the class declaration. Private member

Only member functions in the same class can be accessed. Public members can be accessed by member functions in the same class.

Can also be accessed by functions in other instantiated classes. Of course, there are exceptions.

It is a friend function to be discussed later.
The default data type in the class is private, and the default type in the structure is public. Generally

Both the number and function are private members.
Class also has an access controller protected, which is called a protection member.
4. Initialization
When declaring an object of a class, you can use parentheses () to include an initialization table.

Let's look at the following example:

# Include iostream. h

Class Box
{
Private:
Int height, width, depth; // three private data members
Public:
Box (int, int, int );
~ Box ();
Int volume (); // member function
};

Box: Box (int ht, int wd, int dp)
{
Height = ht;
Width = wd;
Depth = dp;
}

Box ::~ Box ()
{
// Nothing
}

Int Box: volume ()
{
Return height * width * depth;
}

Int main ()
{
Box thisbox (3, 4, 5); // declare a Class Object and initialize
Cout <return 0;
}

When a class does not have private or protected members, there is no virtual function, and

If it is derived from other classes, you can use {} for initialization. (Later)
5. inline functions
The difference between an inline function and a common function is that an inline function is expanded during compilation. Usually inline letter

The number must be brief. There are two ways to define inline functions of a class: one is the same as that of C. when defining a function

Use the keyword inline. For example:

Inline int Box: volume ()
{
Return height * width * depth;
}

Another way is to define the function body directly in the class declaration, instead of simply providing a function

Prototype. Let's simplify the above functions:

# Include iostream. h

Class Box
{
Private:
Int height, width, depth;
Public:
Box (int ht, int wd, int dp)
{
Height = ht;
Width = wd;
Depth = dp;
}
~ Box ();
Int volume ()
{
Return height * width * depth;
}
};

Int main ()
{
Box thisbox (3, 4, 5); // declare a Class Object and initialize
Cout <return 0;
}

In this way, both functions are inline functions by default .... Remaining full text>

When std: map <key, Type> is used in c ++, an exception is found in the map container. If the Type is not constructed, it is analyzed multiple times.

The constructor also has a copy constructor. A (A &) {}// the copy construction is called twice.
Mapa [1] = A (); // This can be compiled. Pair <key, value>.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.