Skip to main content

std::map operator[]

// (1) Non const version only
T& operator[]( const Key& key );
// (2) Non const version only
T& operator[]( Key&& key );

Returns a reference

to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

(1) Inserts a value_type object constructed in-place from std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() if the key does not exist.

Since C++17 this function is equivalent to return this->try_emplace(key).first->second.

When the default allocator is used, this results in the key being copy constructed from key and the mapped value being value-initialized.

Type requirements

(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.

Since C++17 this function is equivalent to return this->try_emplace(key).first->second.

When the default allocator is used, this results in the key being move constructed from key and the mapped value being value-initialized.

Type requirements

Parameters

  • key - the key of the element to find

Return value

Reference

to the mapped value of the new element if no element with key key existed. Otherwise a reference to the mapped value of the existing element whose key is equivalent to key.

Exceptions

If an exception is thrown by any operation, the insertion has no effect.

Complexity

Logarithmic in the size of the container.

Example

Main.cpp
#include <iostream>
#include <map>

auto main() -> int {
std::map<std::string, int> playersScores = {
{ "player1", 50 },
{ "super_coder", 100 },
{ "andrew111", 41 }
};

std::cout << "player1 zdobył " << playersScores["player1"] << " punktów.\n";

std::cout << "super_cader zdobył " << playersScores["super_cader"] << " punktów.\n";
}
Output
player1 zdobył 50 punktów.
super_cader zdobył 0 punktów.
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.

std::map operator[]

// (1) Non const version only
T& operator[]( const Key& key );
// (2) Non const version only
T& operator[]( Key&& key );

Returns a reference

to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

(1) Inserts a value_type object constructed in-place from std::piecewise_construct, std::forward_as_tuple(key), std::tuple<>() if the key does not exist.

Since C++17 this function is equivalent to return this->try_emplace(key).first->second.

When the default allocator is used, this results in the key being copy constructed from key and the mapped value being value-initialized.

Type requirements

(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.

Since C++17 this function is equivalent to return this->try_emplace(key).first->second.

When the default allocator is used, this results in the key being move constructed from key and the mapped value being value-initialized.

Type requirements

Parameters

  • key - the key of the element to find

Return value

Reference

to the mapped value of the new element if no element with key key existed. Otherwise a reference to the mapped value of the existing element whose key is equivalent to key.

Exceptions

If an exception is thrown by any operation, the insertion has no effect.

Complexity

Logarithmic in the size of the container.

Example

Main.cpp
#include <iostream>
#include <map>

auto main() -> int {
std::map<std::string, int> playersScores = {
{ "player1", 50 },
{ "super_coder", 100 },
{ "andrew111", 41 }
};

std::cout << "player1 zdobył " << playersScores["player1"] << " punktów.\n";

std::cout << "super_cader zdobył " << playersScores["super_cader"] << " punktów.\n";
}
Output
player1 zdobył 50 punktów.
super_cader zdobył 0 punktów.
This article originates from this CppReference page. It was likely altered for improvements or editors' preference. Click "Edit this page" to see all changes made to this document.
Hover to see the original license.