当前位置:网站首页>Std:: Map empty example
Std:: Map empty example
2022-06-13 05:01:00 【Snow * sleet * snow】
emplace
emplace The operation is from C++11 Start introducing new features ,emplace The operation is to construct elements directly through parameters instead of copying elements into containers, which can reduce copying and improve performance . about map It's not emplace_front、emplace_after、emplace_back These operations are .
std::map<Key,T,Compare,Allocator>::emplace
template< class... Args > | (since C++11) | |
Inserts a new element into the container constructed in-place with the given args if there is no element with the key in the container.
Note that the return value is also a std::pair
#include <iostream>
#include <utility>
#include <string>
#include <map>
int main()
{
std::map<std::string, std::string> m;
// uses pair's move constructor
// Use mobile construction
m.emplace(std::make_pair(std::string("a"), std::string("a")));
// uses pair's converting move constructor
// Use implicit conversion to move constructors , The difference from the previous one is the “a” It will be implicitly converted to std::string
m.emplace(std::make_pair("b", "abcd"));
// uses pair's template constructor
// Use template construction
m.emplace("d", "ddd");
// uses pair's piecewise constructor
m.emplace(std::piecewise_construct,
std::forward_as_tuple("c"),
std::forward_as_tuple(10, 'c'));
// as of C++17, m.try_emplace("c", 10, 'c'); can be used
for (const auto &p : m) {
std::cout << p.first << " => " << p.second << '\n';
}
}Output :
a => a b => abcd c => cccccccccc d => ddd
边栏推荐
猜你喜欢

Kaggle 时间序列教程
![[LeetCode]-二分查找](/img/7f/7d1f616c491c6fb0be93f591da6df1.png)
[LeetCode]-二分查找

PostgreSQL Guide: Insider exploration (Chapter 7 heap tuples and index only scanning) - Notes

Configuration used by automatic teaching evaluation script

Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions

Logical point

Win8.1和Win10各自的优势

Recursion and recursion

Section 6 - pointers

Spice story
随机推荐
Must know must know -c language keywords
Shell variable learning notes
Section 4 - arrays
RMQ、LCA
Kaggle time series tutorial
2021TPAMI/图像处理:Exploiting Deep Generative Prior for Versatile Image Restoration and Manipulation
C language learning log 10.5
【多线程编程】Future接口获取线程执行结果数据
Construction problem of D Xiaohong
String()和toString()方法得区别
Analysis of the principle of V-model and its application in user defined components
约瑟夫问题
C language learning log 1.22
The games that you've tasted
Cesium:cesiumlab makes image slices and loads slices
Article 49: understanding the behavior of new handler
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
MySQL8.0.13安装教程(有图)
All blog navigation
语音信号分帧的理解