std::map::map
| (1) | ||
explicit map( const Compare& comp = Compare(), {{#pad:|12}} const Allocator& alloc = Allocator() ); |
||
explicit map( const Allocator& alloc ); |
(1) | (od C++11) |
| (2) | ||
template< class InputIterator > map( InputIterator first, InputIterator last, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
||
map( const map& other ); |
(3) | |
map( const map& other, const Allocator& alloc ); |
(3) | (od C++11) |
map( map&& other ); |
(4) | (od C++11) |
map( map&& other, const Allocator& alloc ); |
(4) | (od C++11) |
| (5) | ||
map( std::initializer_list<value_type> init, {{#pad:|3}} const Compare& comp = Compare(), {{#pad:|3}} const Allocator& alloc = Allocator() ); |
(od C++11) | |
Konstruuje nowy kontener z różnych źródeł danych, opcjonalnie wykorzystując dostarczony przez użytkownika alokator alloc i/lub obiekt funkcji porównującej comp.
std::allocator_traits<allocator_type>::select_on_container_copy_construction(other.get_allocator()).Parametry
| alloc | - | alokator używany do wszystkich alokacji pamięci wykonywanych przez ten kontener |
| comp | - | obiekt funkcji porównującej, wykorzystywany przy wszystkich porównaniach kluczy |
| first, last | - | przedział, z którego zostają skopiowane elementy |
| other | - | inny kontener, wykorzystywany jako źródło, z którego inicjalizowane są elementy kontenera |
| init | - | lista inicjalizacyjna, do zainicjowania wartości elementów kontenera |
| Wymagania względem typów | ||
| -InputIterator musi spełniać wymagania InputIteratorerator. | ||
| -Compare musi spełniać wymagania Compare. | ||
| -Allocator musi spełniać wymagania Allocator. | ||
Złożoność
1) Stała
2) N log(N), gdzie N = std::distance(first, last) ogólnie, liniowa względem N jeśli przedział jest już posortowany zgodnie z value_comp().
3) Liniowa względem rozmiaru other
4) Stała. Jeśli alloc jest podany i alloc != other.get_allocator(), liniowa.
5) N log(N), gdzie N = init.size()) ogólnie, liniowa względem N jeśli init jest już posortowana zgodnie z value_comp().
Wyjątki
Wywołania Allocator::allocate mogą wyrzucić wyjątki.
Notka
Po skonstruowaniu kontenera przez przeniesienie (przeciążenie (4)), referencje, wskaźniki i iteratory (inne niż "past-the-end") do other pozostają prawidłowe, ale wskazują na elementy znajdujące się teraz w *this. Obecny standard gwarantuje to przez oświadczenie zbiorcze w §23.2.1[container.requirements.general]/12, i bardziej bezpośrednia gwarancja jest wzięta pod uwagę: LWG 2321.
Przykład
#include <iostream>
#include <string>
#include <iomanip>
#include <map>
template<typename Map>
void print_map(Map& m)
{
std::cout << '{';
for(auto& p: m)
std::cout << p.first << ':' << p.second << ' ';
std::cout << "}\n";
}
struct Point { double x, y; };
struct PointCmp {
bool operator()(const Point& lhs, const Point& rhs) const {
return lhs.x < rhs.x; // sortuje tylko po pierwszej współrzędnej
}
};
int main()
{
// (1) Domyślny konstruktor
std::map<std::string, int> map1;
map1["something"] = 69;
map1["anything"] = 199;
map1["that thing"] = 50;
std::cout << "map1 = "; print_map(map1);
// (2) Konstruktor przedziałowy
std::map<std::string, int> iter(map1.find("anything"), map1.end());
std::cout << "\niter = "; print_map(iter);
std::cout << "map1 = "; print_map(map1);
// (3) Konstruktor kopiujący
std::map<std::string, int> copied(map1);
std::cout << "\ncopied = "; print_map(copied);
std::cout << "map1 = "; print_map(map1);
// (4) Konstruktor przenoszący
std::map<std::string, int> moved(std::move(map1));
std::cout << "\nmoved = "; print_map(moved);
std::cout << "map1 = "; print_map(map1);
// (5) Konstruktor z listy inicjalizacyjnej
const std::map<std::string, int> init {
{"this", 100},
{"can", 100},
{"be", 100},
{"const", 100},
};
std::cout << "\ninit = "; print_map(init);
// Własna metoda porównywania kluczy, opcja 1:
// Użyj funktora
std::map<Point, double, PointCmp> mag = {
{ {5, -12}, 13 },
{ {3, 4}, 5 },
{ {-8, -15}, 17 }
};
for(auto p : mag)
std::cout << "The magnitude of (" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << '\n';
// Własna metoda porównywania kluczy, opcja 2:
// Użyj wyrażeń lambda
// Ta lambda sortuje punkty po ich module, można zwrócić uwagę,
// że wartość tego modułu pobiera ze zmiennej lokalnej mag
auto cmpLambda = [&mag](const Point &lhs, const Point &rhs) { return mag[lhs] < mag[rhs]; };
//Można również użyć lambd, które nie są zależne od zmiennych lokalnych, np.:
//auto cmpLambda = [](const Point &lhs, const Point &rhs) { return lhs.y < rhs.y; };
//Taka lambda posortowałaby po drugiej współrzędnej
std::map<Point, double, decltype(cmpLambda)> magy(cmpLambda);
//Różne metody wstawiania elementów:
magy.insert(std::pair<Point, double>({5, -12}, 13));
magy.insert({ {3, 4}, 5});
magy.insert({Point{-8.0, -15.0}, 17});
std::cout << '\n';
for(auto p : magy)
std::cout << "The magnitude of (" << p.first.x
<< ", " << p.first.y << ") is "
<< p.second << '\n';
}
Wynik:
map1 = {anything:199 something:69 that thing:50 }
iter = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
copied = {anything:199 something:69 that thing:50 }
map1 = {anything:199 something:69 that thing:50 }
moved = {anything:199 something:69 that thing:50 }
map1 = {}
init = {be:100 can:100 const:100 this:100 }
The magnitude of (-8, -15) is 17
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (3, 4) is 5
The magnitude of (5, -12) is 13
The magnitude of (-8, -15) is 17
Zobacz także
| przypisuje wartości do kontenera (publiczna metoda) |