A unique pointer can not be copied, only moved. To have multiple shared pointers pointing at the same object, we would write:
#include
#include
int main()
{
std::shared_ptr p1(new int{ 123 });
std::shared_ptr p2 = p1;
std::shared_ptr p3 = p1;
} // when the last shared pointer goes out of scope, the memory gets // deallocated39
Shared pointers can be copied. It is said they share ownership of the object. When the last shared pointer gets out of scope, the pointed-to object gets destroyed, and the memory gets deallocated.
11.9 std::unordered_set
std::set使用红黑树为底层数据结构,std::unordered_set以哈希映射实现物理存储。
The std::unordered_set is a container that allows for constant time insertion, searching, and removal of elements. This container is implemented as an array of buckets of linked lists. The hash value of each element is calculated (hashed), and the object is placed into an appropriate bucket based on the hash value. The object themselves are not sorted in any particular order. To define an unordered set, we need to include the header. Example:
#include
#include
int main()
{
std::unordered_set myunorderedset = { 1, 2, 5, -4, 7, 10 };
for (auto el : myunorderedset)
{
std::cout << el << '
';
}
}
The values are not sorted but are unique. To insert single or multiple values into an unordered_set, we use the .insert() member function:
#include
#include
int main()
{
std::unordered_set myunorderedset = { 1, 2, 5, -4, 7, 10 };
myunorderedset.insert(6); // insert a single value
myunorderedset.insert({ 8, 15, 20 }); // insert multiple values
for (auto el : myunorderedset)
{
std::cout << el << '
';
}
}
To delete a value from an unordered set, we use the .erase() member function:
#include
#include
int main()
{
std::unordered_set myunorderedset = { 1, 2, 5, -4, 7, 10 };
myunorderedset.erase(-4); // erase a single value
for (auto el : myunorderedset)
{
std::cout << el << '
';
}
}
11.10 std::unordered_map
std::map使用红黑树为底层数据结构,std::unordered_map以哈希映射实现物理存储。
Similar to std::unordered_set, there is also an std::unordered_map , an unordered container of key-value pairs with unique keys. This container also allows for fast insertion, searching, and removal of elements. The container is also data is also implemented as buckets. What element goes into what bucket depends on the element’s key hash value. To define an unordered map, we include the header. Example:
#include
#include
int main()
{
std::unordered_map myunorderedmap = { {'a', 1}, {'b', 2}, {'c', 5} };
for (auto el : myunorderedmap)
{
std::cout << el.first << ' '<< el.second << '
';
}
}
Here we initialize an unordered map with key-value pairs. In the range-based for loop, we print both the key and the value. Map elements are pairs. Pairs have member functions .first for accessing a key and .second for accessing a value. To insert an element into a map we can use the member function .insert() member function:
#include
#include
int main()
{
std::unordered_map myunorderedmap = { {'a', 1}, {'b', 2}, {'c', 5} };
myunorderedmap.insert({ 'd', 10 });
for (auto el : myunorderedmap)
{
std::cout << el.first << ' '<< el.second << '
';
}
}
We can also use the map’s operator [] to insert an element. Normally, this operator is used to access an element value by key. However, if the key does not exist, the operator inserts a new element into the map: