[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/container/map [Back]  [Original]

std::map cppreference.com
cppreference.com

std::map

cppreference.com
<tbody> </tbody>
template< class Key, class T, class Compare = std::less<Key>, class Allocator = std::allocator<std::pair<const Key, T>> > class map;
(1)
namespace pmr { template< class Key, class T, class Compare = std::less<Key> > using map = std::map<Key, T, Compare, std::pmr::polymorphic_allocator<std::pair<const Key, T>>>; }
(2) ( C++17)

std::map  , - . Compare. , . - .

, Compare, . , a b ( ), , : !comp(a, b) && !comp(b, a).

std::map Container, AllocatorAwareContainer, AssociativeContainer ReversibleContainer.

key_type Key []
mapped_type T []
value_type std::pair<const Key, T> []
size_type ( std::size_t) []
difference_type ( std::ptrdiff_t) []
key_compare Compare []
allocator_type Allocator []
reference value_type& []
const_reference const value_type& []
pointer
Allocator::pointer ( C++11)
std::allocator_traits<Allocator>::pointer ( C++11)
[]
const_pointer
Allocator::const_pointer ( C++11)
std::allocator_traits<Allocator>::const_pointer ( C++11)
[]
iterator LegacyBidirectionalIterator value_type []
const_iterator LegacyBidirectionalIterator const value_type []
reverse_iterator std::reverse_iterator<iterator>[]
const_reverse_iterator std::reverse_iterator<const_iterator>[]
node_type ( C++17) , []
insert_return_type ( C++17) , node_type,

template <class Iter, class NodeType> struct /**/ { Iter position; bool inserted; NodeType node; };
iterator node_type. []

value_type
() []

map
(public -) []
map
(public -) []

(public -) []

(public -) []

(public -) []

(public -) []

(public -) []
(C++11)

(public -) []

(public -) []

(public -) []
,
(public -) []

(public -) []

(public -) []

(public -) []
( C++17)
(public -) []

(public -) []
,
(public -) []
(C++11)

(public -) []
,
(public -) []
" ", , ,
(public -) []

(public -) []

(public -) []
(C++17)

(public -) []
(C++17)

(public -) []
,
(public -) []

(public -) []
(C++20)
,
(public -) []
,
(public -) []
,
(public -) []
,
(public -) []
,
(public -) []
, value_type
(public -) []

,

( C++20)( C++20)( C++20)( C++20)( C++20)(C++20)
map
( ) []
std::swap
( ) []
,
( ) []

( C++17)

__cpp_lib_containers_ranges 202202L (C++23)

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

void print_map(std::string_view comment, const std::map<std::string, int>& m)
{
    std::cout << comment;
    //     C++17
    for (const auto& [key, value] : m) {
        std::cout << '[' << key << "] = " << value << "; ";
    }
//  C++11:
//  for (const auto& n : m) {
//      std::cout << '[' << n.first << "] = " << n.second << "; ";
//  }
//  C++98:
//  for (std::map<std::string, int>::const_iterator it = m.begin(); it != m.end(); it++) {
//      std::cout << '[' << it->first << "] = " << it->second << "; ";
//  }
    std::cout << "\n";
}

int main()
{
    //      (    )
    std::map<std::string, int> m { {"CPU", 10}, {"GPU", 15}, {"RAM", 20}, };

    print_map("1)  : ", m);

    m["CPU"] = 25;  //   
    m["SSD"] = 30;  //   

    print_map("2)  : ", m);

    //  operator[]      
    std::cout << "3) m[UPS] = " << m["UPS"] << '\n';
    print_map("4)  : ", m);

    m.erase("GPU");
    print_map("5)  : ", m);

    std::erase_if(m, [](const auto& pair){ return pair.second > 25; });
    print_map("6)  : ", m);
    std::cout << "7) m.size() = " << m.size() << '\n';

    m.clear();
    std::cout << std::boolalpha << "8)  : " << m.empty() << '\n';
}

:

1)  : [CPU] = 10; [GPU] = 15; [RAM] = 20; 
2)  : [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30;
3) m[UPS] = 0
4)  : [CPU] = 25; [GPU] = 15; [RAM] = 20; [SSD] = 30; [UPS] = 0; 
5)  : [CPU] = 25; [RAM] = 20; [SSD] = 30; [UPS] = 0; 
6)  : [CPU] = 25; [RAM] = 20; [UPS] = 0; 
7) m.size() = 3
8)  : true

C++:

LWG 464 C++98 const map at

Web Proxy Viewer  |  New URL  |  Original Page