Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The C++ container class

The C++ container class

In C++, is part of the Standard Template Library (STL); it provides an associative container for storing key-value pairs.

map Elements in the container are automatically sorted by key, making it well suited for scenarios requiring fast lookup and ordered data.

Definition and characteristics

Basic Syntax

Include the header file:



#include 

Declare a map container:



std::map myMap;

Insert elements:



myMap[key] = value;

Access elements:



value = myMap[key];

Traverse the map:



for (std::map::iterator it = myMap.begin(); it != myMap.end(); ++it) {

    std::cout << it->first << " => " << it->second << std::endl;

}

For C++11 and later standards, the traversal part can be simplified with a range-based for loop, making the code cleaner:

for (auto &p : m) {

    std::cout << p.first << " : " << p.second << std::endl;

}

Example

Below is an example using map simple example, we will create a map to store employees' names and their ages, and traverse this map to print each employee's name and age.

Example

#include #include

#include

int

main

(

)

{

   

// Create a map container storing employees' names and ages

    std

::

map

<

std

::

string

,

int

employees

;

   

// Insert employee information

    employees

[

"Alice"

]

=

30

;

    employees

[

"Bob"

]

=

25

;

    employees

[

"Charlie"

]

=

35

;

   

// Traverse the map and print employee information

   

for

(

std

::

map

<

std

::

string

,

int

::

iterator

it

=

employees.

begin

(

)

;

it

!

=

employees.

end

(

)

;

++

it

)

{

        std

::

cout

<<

it

first

<<

" is "

<<

it

second

<<

" years old."

<<

std

::

endl

;

   

}

   

return

0

;

}

Output result:

Alice is 30 years old.

Bob is 25 years old.

Charlie is 35 years old.

Advanced usage

Check whether a key exists:



if (myMap.find(key) != myMap.end()) {

    // 键存在

}

Delete elements:



myMap.erase(key);

Clear the map:



myMap.clear();

Get the size of the map:



size_t size = myMap.size();

Other methods:



myMap.empty();      // 是否为空

myMap.count("Bob"); // key 是否存在(返回 0 或 1)

Custom sorting: by default ascending; you can use std::greater or a custom comparison function:



std::map> m;  // 降序

Using a custom comparison function:

Example

#include

#include

#include

bool

myCompare

(

const

std

::

string

&

a,

const

std

::

string

&

b

)

{

   

return

a

<

b

;

}

int

main

(

)

{

    std

::

map

<

std

::

string

,

int

, std

::

function

<

bool

(

const

std

::

string

&

,

const

std

::

string

&

)

myMap

(

myCompare

)

;

   

// Other operations...

   

return

0

;

}

map is a very useful container in the C++ STL, especially suited for scenarios requiring fast lookup and ordered data.

Example

#include

#include

#include

int

main

(

)

{

    std

::

map

<

std

::

string

,

int

scores

;

   

// Insert

    scores

[

"Alice"

]

=

90

;

    scores

[

"Bob"

]

=

85

;

    scores.

insert

(

{

"Charlie"

,

92

}

)

;

   

// Traverse

   

for

(

auto

&

p

:

scores

)

{

        std

::

cout

<<

p.

first

<<

" => "

<<

p.

second

<<

std

::

endl

;

   

}

   

// Find

   

auto

it

=

scores.

find

(

"Bob"

)

;

   

if

(

it

!

=

scores.

end

(

)

)

{

        std

::

cout

<<

"Bob's score: "

<<

it

second

<<

std

::

endl

;

   

}

   

// Delete

    scores.

erase

(

"Alice"

)

;

    std

::

cout

<<

"Size: "

<<

scores.

size

(

)

<<

std

::

endl

;

   

return

0

;

}

Output (automatically sorted by key):

Alice => 90

Bob => 85

Charlie => 92

Bob's score: 85

Size: 2