简体   繁体   English

将元素插入多图的语法 <string, vector<pair<string, int> &gt;&gt;

[英]Syntax to insert elements into multimap<string, vector<pair<string, int>>>

My question is how to insert some element into multimap of the form 我的问题是如何将某些元素插入表单的多图

multimap<string, vector<pair<string, int>>> someMap; //std skipped to simplify

I tried different syntaxes and i think the closest one may be this one 我尝试了不同的语法,我认为最接近的语法可能就是这个

someMap.insert(pair<string,vector<pair<string, int>>>(someString1, vector<pair<string, int>> { pair<string, int> (someString2, someInt) }));

Unfortunately it is not working. 不幸的是,它不起作用。 Any tips?? 有小费吗??

The type of the first pair is wrong 第一对的类型是错误的

pair<string,vector<string, int>>
                   ^^^^^^^^^^^

anyway I suggest: 无论如何,我建议:

multimap<string, vector<pair<string, int>>> someMap;
vector<pair<string,int>> obj;
someMap.insert(make_pair("hello", obj));

or if you insist with that syntax (verbose-mode): 或者,如果您坚持使用该语法(详细模式):

  multimap<string, vector<pair<string, int>>> someMap;
  string someString2 = "hello";
  string someString1 = "world";
  int someInt = 42;
  someMap.insert(pair<string,vector<pair<string, int>>>(someString1, vector<pair<string, int>> { pair<string, int> (someString2, someInt) }));

this requires C++11. 这需要C ++ 11。

Try the following 尝试以下

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>

int main() 
{
    typedef std::pair<std::string, int> value_type;
    std::multimap<std::string, std::vector<value_type>> m; 

    m.insert( { "A", std::vector<value_type>( 1, { "A", 'A' } ) } );

    return 0;
}

Or another example 或另一个例子

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <utility>

int main() 
{
    typedef std::pair<std::string, int> value_type;
    std::multimap<std::string, std::vector<value_type>> m; 

    auto it = m.insert( { "A", std::vector<value_type>( 1, { "A", 'A' } ) } );

    for ( char c = 'B'; c <= 'Z'; ++c )
    {
        const char s[] = { c, '\0' };

        it->second.push_back( { s, c } );
    }

    size_t i = 0;
    for ( const auto &p : it->second )
    {
        std::cout << "{" << p.first << ", " << p.second << "} ";
        if ( ++i % 7 == 0 ) std::cout << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

The output is 输出是

{A, 65} {B, 66} {C, 67} {D, 68} {E, 69} {F, 70} {G, 71} 
{H, 72} {I, 73} {J, 74} {K, 75} {L, 76} {M, 77} {N, 78} 
{O, 79} {P, 80} {Q, 81} {R, 82} {S, 83} {T, 84} {U, 85} 
{V, 86} {W, 87} {X, 88} {Y, 89} {Z, 90} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM