std::map<Key,T,Compare,Allocator>:: map
| (1) | ||
|
map
(
)
;
|
(C++11 이전) | |
|
map
(
)
:
map
(
Compare
(
)
)
{
}
|
(C++11 이후)
(constexpr C++26 이후) |
|
|
explicit
map
(
const
Compare
&
comp,
const Allocator & alloc = Allocator ( ) ) ; |
(2) | (C++26부터 constexpr) |
|
explicit
map
(
const
Allocator
&
alloc
)
;
|
(3) |
(C++11부터)
(C++26부터 constexpr) |
|
template
<
class
InputIt
>
map
(
InputIt first, InputIt last,
|
(4) | (C++26부터 constexpr) |
|
template
<
class
InputIt
>
map
(
InputIt first, InputIt last,
|
(5) |
(C++14부터)
(C++26부터 constexpr) |
|
map
(
const
map
&
other
)
;
|
(6) | (C++26부터 constexpr) |
|
map
(
const
map
&
other,
const
Allocator
&
alloc
)
;
|
(7) |
(C++11부터)
(C++26부터 constexpr) |
|
map
(
map
&&
other
)
;
|
(8) |
(C++11부터)
(C++26부터 constexpr) |
|
map
(
map
&&
other,
const
Allocator
&
alloc
)
;
|
(9) |
(C++11부터)
(C++26부터 constexpr) |
|
map
(
std::
initializer_list
<
value_type
>
init,
const
Compare
&
comp
=
Compare
(
)
,
|
(10) |
(C++11부터)
(C++26부터 constexpr) |
|
map
(
std::
initializer_list
<
value_type
>
init,
const
Allocator
&
alloc
)
|
(11) |
(C++14부터)
(C++26부터 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
map
(
std::
from_range_t
, R
&&
rg,
|
(12) |
(C++23부터)
(C++26부터 constexpr) |
|
template
<
container-compatible-range
<
value_type
>
R
>
map
(
std::
from_range_t
, R
&&
rg,
|
(13) |
(C++23부터)
(C++26부터 constexpr) |
다양한 데이터 소스로부터 새로운 컨테이너를 생성하며, 선택적으로 사용자가 제공한 할당자 alloc 또는 비교 함수 객체 comp 를 사용합니다.
[
first
,
last
)
.
[
first
,
last
)
가 유효한 범위가 아니라면, 동작은 정의되지 않습니다.
|
alloc
이 제공되지 않으면, allocator는
std::
allocator_traits
<
allocator_type
>
::
|
(C++11부터) |
|
클래스 템플릿 인수 추론
동안에는 첫 번째 인수만 컨테이너의
|
(C++23부터) |
|
클래스 템플릿 인수 추론
동안에는 첫 번째 인수만 컨테이너의
|
(C++23부터) |
내용매개변수
복잡도
1-3)
상수.
4,5)
N·log(N)
여기서
N
은
std::
distance
(
first, last
)
입니다. 일반적으로
N
에 선형적이며,
[
first
,
last
)
가 이미
value_comp
(
)
에 따라 정렬된 경우
N
에 선형입니다.
6,7)
other
의 크기에 선형적입니다.
8,9)
상수. 만약
alloc
이 주어지고
alloc
!
=
other.
get_allocator
(
)
인 경우, 선형 시간.
10,11)
N·log(N)
여기서
N
는
init.
size
(
)
입니다. 일반적으로
N
에 선형적이며,
init
가 이미
value_comp
(
)
로 정렬된 경우에는 선형 시간입니다.
12,13)
N·log(N)
여기서
N
는
ranges::
distance
(
rg
)
입니다. 일반적으로
N
에 선형적이며,
rg
가 이미
value_comp
(
)
에 의해 정렬된 경우입니다.
예외
참고컨테이너 이동 생성(오버로드 (8,9)) 후에, 원래 C++23까지 공식적으로 요구되지는 않지만, 일부 구현체는 이미 이전 모드에서 템플릿 매개변수
예제
이 코드 실행
#include <iomanip> #include <iostream> #include <map> #include <string> template<typename Key, typename Value, typename Cmp> std::ostream& operator<<(std::ostream& os, const std::map<Key, Value, Cmp>& map) { os << "{ "; for (auto comma{map.size()}; const auto& p : map) os << '\'' << p.first << "'는 " << p.second << (--comma ? ", " : " "); return os << "}\n"; } struct Point { double x, y; friend std::ostream& operator<<(std::ostream& os, Point pt) { return os << '(' << pt.x << ", " << pt.y << ')'; } }; struct PointCmp { bool operator()(const Point& lhs, const Point& rhs) const { return lhs.x < rhs.x; // NB: y는 의도적으로 무시됨 } }; int main() { // (1) 기본 생성자 std::map<std::string, int> map1; map1["something"] = 69; map1["anything"] = 199; map1["그것"] = 50; std::cout << "map1 = " << map1; // (4) 범위 생성자 std::map<std::string, int> iter(map1.find("anything"), map1.end()); std::cout << "\niter = " << iter; std::cout << "map1 = " << map1; // (6) 복사 생성자 std::map<std::string, int> copied(map1); std::cout << "\ncopied = " << copied; std::cout << "map1 = " << map1; // (8) 이동 생성자 std::map<std::string, int> moved{std::move(map1)}; std::cout << "\nmoved = " << moved; std::cout << "map1 = " << map1; // (10) 초기화 리스트 생성자 const std::map<std::string, int> init { {"this", 100}, {"할 수 있다", 100}, {"be", 100}, {"const", 100} }; std::cout << "\ninit = " << init; std::cout << "\nCustom Key 클래스 옵션 1:\n"; // 비교 구조체 사용 std::map<Point, double, PointCmp> mag = { {{5, -12}, 13}, {{3, 4}, 5}, {{-8, -15}, 17} }; std::cout << "mag = " << mag << '\n'; std::cout << "Custom Key 클래스 옵션 2:\n"; // 비교 람다 사용 // 이 람다는 점들을 그들의 크기에 따라 정렬하며, 여기서 // 이 크기 값들은 지역 변수 mag에서 가져온 것입니다. auto cmpLambda = [&mag](const Point& lhs, const Point& rhs) { return mag[lhs] < mag[rhs]; }; // 다음과 같이 지역 변수에 의존하지 않는 람다를 사용할 수도 있습니다: // auto cmpLambda = [](const Point& lhs, const Point& rhs){ return lhs.y < rhs.y; }; std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda); // 요소를 삽입하는 다양한 방법: magy.insert(std::pair<Point, double>({5, -12}, 13)); magy.insert({{3, 4}, 5}); magy.insert({Point{-8.0, -15.0}, 17}); std::cout << "magy = " << magy << '\n'; std::cout << "범위로부터의 생성:\n"; using PS = std::pair<const std::string, int>; const auto rg = {PS{"하나", 1}, {"하나", 101}, {"two", 2}, {"three", 3}}; #if __cpp_lib_containers_ranges std::map<std::string, int> nums(std::from_range, rg); // 오버로드 (12) #else std::map<std::string, int> nums(rg.begin(), rg.end()); // (4)로 폴백 #endif std::cout << "nums = " << nums << '\n'; } 출력: map1 = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
iter = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
map1 = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
copied = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
map1 = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
moved = { 'anything' is 199, 'something' is 69, 'that thing' is 50 }
map1 = { }
init = { 'be' is 100, 'can' is 100, 'const' is 100, 'this' is 100 }
커스텀 키 클래스 옵션 1:
mag = { '(-8, -15)' is 17, '(3, 4)' is 5, '(5, -12)' is 13 }
커스텀 키 클래스 옵션 2:
magy = { '(3, 4)' is 5, '(5, -12)' is 13, '(-8, -15)' is 17 }
범위로부터의 생성:
nums = { 'one' is 1, 'three' is 3, 'two' is 2 }
결함 보고서다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.
참고 항목
| ||||||||||||||||||||||||||||||||||||||||||||||||||||