What is the purpose of std::make_pair vs the constructor of std::pair?

后端 未结 7 1272
借酒劲吻你
借酒劲吻你 2020-12-04 06:04

What is the purpose of std::make_pair?

Why not just do std::pair(0, \'a\')?

Is there any difference between the tw

相关标签:
7条回答
  • 2020-12-04 06:43

    There is no difference between using make_pair and explicitly calling the pair constructor with specified type arguments. std::make_pair is more convenient when the types are verbose because a template method has type deduction based on its given parameters. For example,

    std::vector< std::pair< std::vector<int>, std::vector<int> > > vecOfPair;
    std::vector<int> emptyV;
    
    // shorter
    vecOfPair.push_back(std::make_pair(emptyV, emptyV));
    
     // longer
    vecOfPair.push_back(std::pair< std::vector<int>, std::vector<int> >(emptyV, emptyV));
    
    0 讨论(0)
提交回复
热议问题