emplace and unordered_map<?, std::array<?, N>>
emplace and unordered_map<?, std::array<?, N>>
我有一个std::unordered_map<string, std::array<int, 2>>
。emplace
将值输入到映射中的语法是什么?
unordered_map<string, array<int, 2>> contig_sizes;
string key{"key"};
array<int, 2> value{1, 2};
// OK ---1
contig_sizes.emplace(key, value);
// OK --- 2
contig_sizes.emplace(key, std::array<int, 2>{1, 2});
// compile error --3
//contig_sizes.emplace(key, {{1,2}});
// OK --4 (Nathan Oliver)
// Very inefficient results in two!!! extra copy c'tor
contig_sizes.insert({key, {1,2}});
// OK --5
// One extra move c'tor followed by one extra copy c'tor
contig_sizes.insert({key, std::array<int, 2>{1,2}});
// OK --6
// Two extra move constructors
contig_sizes.insert(pair<const string, array<int, 2>>{key, array<int, 2>{1, 2}});
我正在使用clang++ -c -x c++ -std=c++14
和clang 3.6.0
我在中测试了代码http://ideone.com/pp72yR
附录:(4( 由Nathan Oliver在下方的答案中提出
来自cppreference std::unordered_map::emplace
被声明为
template< class... Args >
std::pair<iterator,bool> emplace( Args&&... args );
因此,它试图推导传递给它的类型
--一种函数参数,其相关参数为初始值设定项列表(8.5.4(,但该参数没有指定从初始值设定值列表中推导的类型(14.8.1.1(。[示例:
template<class T> void g(T); g({1,2,3}); // error: no argument deduced for T
--结束示例]
因此,没有任何类型能够被推导出来。
如果你想在飞行中创建对象,那么你可以使用以下形式:
contig_sizes.emplace(key, std::array<int, 2>{1, 2});
或者我们可以创建一个typedef
typedef pair<const string, array<my_class, 2>> pair_type;
然后我们可以有
contig_sizes.emplace(pair_type{key, {1, 2}});
您也可以使用std::unordered_map::insert
,它接受键/值的pair
,该键/值可以从支撑的初始值设定项列表中构造。
contig_sizes.insert({key, {1, 2}});
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中