将std::vector复制到boost::无序映射<;字符串,std::vector<;foo>&燃气轮机;

将std::vector复制到boost::无序映射<;字符串,std::vector<;foo>&燃气轮机;,boost,c++11,vector,unordered-map,Boost,C++11,Vector,Unordered Map,将记录类型映射到字段值向量: unordered_map<string, vector<string>> input_records; string rec_type {"name"}; vector<string> fields { "field 1", "field 2", "field 3" }; 然而,无序映射的boost文档包含以下内容: template<typename InputIterator> void insert(Inp

将记录类型映射到字段值向量:

unordered_map<string, vector<string>> input_records;
string rec_type {"name"};
vector<string> fields { "field 1", "field 2", "field 3" };
然而,无序映射的boost文档包含以下内容:

template<typename InputIterator> 
void insert(InputIterator first, InputIterator last);

Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent key.

Throws:
When inserting a single element, if an exception is thrown by an operation other than a call to hasher the function has no effect.
Notes:
Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.
Pointers and references to elements are never invalidated.
模板
无效插入(先输入,后输入);
将一系列元素插入容器中。当且仅当容器中没有具有等效键的元素时,才会插入元素。
抛出:
插入单个元素时,如果异常是由对哈希器的调用以外的操作引发的,则该函数无效。
笔记:
可以使迭代器无效,但仅当插入导致负载因子大于或等于最大负载因子时。
指向元素的指针和引用永远不会失效。
(写这篇文章时,我意识到可能InputIterator指向一系列键/值对,每个键/值对都要插入到映射中。因此,方法是错误的。)


如何最好地实例化和填充
输入记录[rec\u type]
处的向量?一旦填充,向量将不会被修改。它是否像
输入记录[rec\u type]=字段那样简单?“移动语义”在这种情况下是否适用?

在标准关联容器中,推入的是std::pair,大多数情况下使用std::make_pair()

#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
无序的地图输入记录;
字符串记录类型{“名称”};
向量场{“场1”、“场2”、“场3”};
输入记录。插入(创建记录对(记录类型,字段));
for(常量自动和文本:输入记录[记录类型])

cout“它是否像
input\u record[rec\u type]=fields
那么简单?”您试过了吗?-o对于移动语义,将
fields
替换为临时值(在C++11中更容易)。“放置”新值比移动它稍微好一些,但鉴于您的数据类型,它不太可能有意义。
template<typename InputIterator> 
void insert(InputIterator first, InputIterator last);

Inserts a range of elements into the container. Elements are inserted if and only if there is no element in the container with an equivalent key.

Throws:
When inserting a single element, if an exception is thrown by an operation other than a call to hasher the function has no effect.
Notes:
Can invalidate iterators, but only if the insert causes the load factor to be greater to or equal to the maximum load factor.
Pointers and references to elements are never invalidated.
#include <vector>
#include <string>
#include <unordered_map>
#include <iostream>

using namespace std;

int main()
{
    unordered_map<string, vector<string>> input_records;
    string rec_type {"name"};
    vector<string> fields { "field 1", "field 2", "field 3" };   

    input_records.insert( make_pair( rec_type, fields ) );

    for( const auto& text : input_records[rec_type] )
        cout << text << '\n';
}
#include <vector>
#include <string>
#include <unordered_map>
#include <iostream>

using namespace std;

int main()
{
    unordered_map<string, vector<string>> input_records;
    string rec_type {"name"};
    vector<string> fields { "field 1", "field 2", "field 3" };   

    input_records.emplace( rec_type, fields );

    for( const auto& text : input_records[rec_type] )
        cout << text << '\n';
}