std::map::operator[]
T& operator[]( const Key& key ); |
(1) | |
T& operator[]( Key&& key ); |
(2) | (od C++11) |
Zwraca referencję do wartości skojarzonej z kluczem równym key, wykonując wstawienie, jeśli taki element wcześniej nie istniał.
|
1) Wstawia value_type(key, T()) jeśli klucz nie występował w kontenerze. Funkcja jest równoważna z return insert(std::make_pair(key, T())).first->second;
|
(do C++11) | ||
|
1) Wstawia obiekt value_type konstruowany "w miejscu" (ang) z
std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>(), jeśli klucz nie występuje w kontenerze. Kiedy używany jest domyślny alokator, sprowadza się to do skonstruowania klucza przez skopiowanie key and the mapped value being value-initialized.(wymaga tłumaczenia)
2) Inserts a value_type object constructed in-place from std::piecewise_construct, std::forward_as_tuple(std::move(key)), std::tuple<>() if the key does not exist. Kiedy używany jest domyślny alokator, sprowadza się to do skonstruowania klucza przez przeniesienie keyand the mapped value being value-initialized.(wymaga tłumaczenia)
|
(od C++11) |
Żadne iteratory ani referencje nie zostają unieważnione.
Parametry
| key | - | klucz, z którym element będzie szukany / wstawiony |
Zwracana wartość
Referencja do mapowanej wartości nowego elementu, jeśli wcześniej nie istniał żaden element z kluczem key. W przeciwnym razie referencja do mapowanej wartości istniejącego wcześniej elementu z kluczem równym key.
Wyjątki
Jeśli zostanie wyrzucony wyjątek przez jakąkolwiek operację, funkcja nie ma żadnego efektu.
Złożoność
Logarytmicza względem rozmiaru kontenera.
Notka
In the published C++11 and C++14 standards, this function was specified to require mapped_type to be DefaultInsertable and key_type to be CopyInsertable or MoveInsertable into *this. This specification was defective and was fixed by LWG issue 2469, and the description above incorporates the resolution of that issue.
However, one implementation (libc++) is known to construct the key_type and mapped_type objects via two separate allocator construct() calls, as arguably required by the standards as published, rather than emplacing a value_type object.
operator[] is non-const because it inserts the key if it doesn't exist. If this behavior is undesirable or if the container is const, at() may be used.
| Ta sekcja jest niedokończona.
Powód: wymaga tłumaczenia |
Przykład
#include <iostream>
#include <string>
#include <vector>
#include <map>
int main()
{
std::map<char, int> letter_counts {{'a', 27}, {'b', 3}, {'c', 1}};
std::cout << "initially:\n";
for (const auto &pair : letter_counts) {
std::cout << pair.first << ": " << pair.second << '\n';
}
letter_counts['b'] = 42; // uaktualnienie istniejącej wartości
letter_counts['x'] = 9; // wstawienie nowej wartości
std::cout << "after modifications:\n";
for (const auto &pair : letter_counts) {
std::cout << pair.first << ": " << pair.second << '\n';
}
// zlicza liczbę wystąpień każdego słowa
// (pierwsze wywołanie operatora[] inicjalizuje liczniki zerem)
std::map<std::string, size_t> word_map;
for (const auto &w : { "this", "sentence", "is", "not", "a", "sentence",
"this", "sentence", "is", "a", "hoax"}) {
++word_map[w];
}
for (const auto &pair : word_map) {
std::cout << pair.second << " occurrences of word '" << pair.first << "'\n";
}
}
Wynik:
initially:
a: 27
b: 3
c: 1
after modifications:
a: 27
b: 42
c: 1
x: 9
2 occurrences of word 'a'
1 occurrences of word 'hoax'
2 occurrences of word 'is'
1 occurrences of word 'not'
3 occurrences of word 'sentence'
2 occurrences of word 'this'
Zobacz także
(C++11) |
dostęp do wskazanego elementu, ze sprawdzeniem zakresów (publiczna metoda) |