当前位置:网站首页>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
边栏推荐
- 17.6 unique_lock详解
- Luogu p1036 number selection
- What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
- Sort (internal sort) + external sort
- 使用EasyDarwin+FFmpeg实现rtsp推流
- QT using layout manager is invalid or abnormal
- Section 2 - branch and loop statements
- Force deduction 121 questions
- The games that you've tasted
- C language exercise 1
猜你喜欢
C # get all callable methods of WebService interface [webmethod]
Shell built-in string substitution
Bm1z002fj-evk-001 startup evaluation
BM1Z002FJ-EVK-001开机测评
Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
About mission planning and improving execution
Conception d'un système basé sur MVC avec javaswing JDBC
The games that you've tasted
利用Javeswingjdbc基於mvc設計系統
随机推荐
2021tami/ image processing: exploiting deep generative priority for versatile image restoration and manipulation
OpenCV中的saturate操作(饱和操作)究竟是怎么回事
RuoYi-Cloud启动教程(手把手图文)
Opencv image storage and reading
Advantages of win8.1 and win10
Optocoupler working principle function electric parameter application circuit
C language learning log 1.24
C language learning log 1.17
Binary search and binary answer
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
Robot pose description and coordinate transformation
Win8.1和Win10各自的優勢
Must know must know -c language keywords
Analysis of the principle of V-model and its application in user defined components
C language learning log 1.19
Mind mapping series - Database
Gradient descent, learning rate
Draw a hammer
Flex布局自适应失效的问题
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases