C++ std::make_pair 与 std::pair 的构造函数的目的是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9270563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:34:17  来源:igfitidea点击:

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

c++stlstd-pair

提问by

What is the purpose of std::make_pair?

的目的是std::make_pair什么?

Why not just do std::pair<int, char>(0, 'a')?

为什么不直接做std::pair<int, char>(0, 'a')

Is there any difference between the two methods?

这两种方法有什么区别吗?

采纳答案by Tor Valamo

The difference is that with std::pairyou need to specify the types of both elements, whereas std::make_pairwill create a pair with the type of the elements that are passed to it, without you needing to tell it. That's what I could gather from various docs anyways.

不同之处在于 withstd::pair您需要指定两个元素的类型,而std::make_pair将创建一对传递给它的元素的类型,而无需告诉它。无论如何,这就是我可以从各种文档中收集到的信息。

See this example from http://www.cplusplus.com/reference/std/utility/make_pair/

请参阅http://www.cplusplus.com/reference/std/utility/make_pair/ 中的此示例

pair <int,int> one;
pair <int,int> two;

one = make_pair (10,20);
two = make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>

Aside from the implicit conversion bonus of it, if you didn't use make_pair you'd have to do

除了它的隐式转换奖励,如果你没有使用 make_pair 你必须这样做

one = pair<int,int>(10,20)

every time you assigned to one, which would be annoying over time...

每次你分配给一个,随着时间的推移会很烦人......

回答by PlagueHammer

As @MSalters replied above, you can now use curly braces to do this in C++11 (just verified this with a C++11 compiler):

正如@MSalters 在上面回答的那样,您现在可以使用花括号在 C++11 中执行此操作(刚刚使用 C++11 编译器验证了这一点):

pair<int, int> p = {1, 2};

回答by devil

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

使用make_pair和显式调用pair具有指定类型参数的构造函数之间没有区别。std::make_pair当类型冗长时更方便,因为模板方法具有基于其给定参数的类型推导。例如,

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));

回答by mkm

It's worth noting that this is a common idiom in C++ template programming. It's known as the Object Generator idiom, you can find more information and a nice example here.

值得注意的是,这是 C++ 模板编程中的常见习语。它被称为对象生成器习语,您可以在此处找到更多信息和一个很好的示例。

EditAs someone suggested in the comments (since removed) the following is a slightly modified extract from the link in case it breaks.

编辑正如有人在评论中建议的那样(自删除以来),以下是对链接的稍微修改的摘录,以防万一。

An Object Generator allows creation of objects without explicitly specifying their types. It is based on a useful property of function templates which class templates don't have: The type parameters of a function template are deduced automatically from its actual parameters. std::make_pairis a simple example that returns an instance of the std::pairtemplate depending on the actual parameters of the std::make_pairfunction.

对象生成器允许在不明确指定对象类型的情况下创建对象。它基于类模板所没有的函数模板的一个有用属性:函数模板的类型参数是从其实际参数中自动推导出来的。std::make_pair是一个简单的例子,它std::pair根据std::make_pair函数的实际参数返回模板的一个实例。

template <class T, class U>
std::pair <T, U> 
make_pair(T t, U u)
{
  return std::pair <T, U> (t,u);
}

回答by EmpZoooli

make_pair creates an extra copy over the direct constructor. I always typedef my pairs to provide simple syntax.
This shows the difference (example by Rampal Chaudhary):

make_pair 在直接构造函数上创建一个额外的副本。我总是 typedef 我的对以提供简单的语法。
这显示了差异(Rampal Chaudhary 的示例):

class Sample
{
    static int _noOfObjects;

    int _objectNo;
public:
    Sample() :
        _objectNo( _noOfObjects++ )
    {
        std::cout<<"Inside default constructor of object "<<_objectNo<<std::endl;
    }

    Sample( const Sample& sample) :
    _objectNo( _noOfObjects++ )
    {
        std::cout<<"Inside copy constructor of object "<<_objectNo<<std::endl;
    }

    ~Sample()
    {
        std::cout<<"Destroying object "<<_objectNo<<std::endl;
    }
};
int Sample::_noOfObjects = 0;


int main(int argc, char* argv[])
{
    Sample sample;
    std::map<int,Sample> map;

    map.insert( std::make_pair( 1, sample) );
    //map.insert( std::pair<int,Sample>( 1, sample) );
    return 0;
}

回答by Mahmoud Badri

starting from c++11 just use uniform initialization for pairs. So instead of:

从 c++11 开始,只需对对使用统一初始化。所以而不是:

std::make_pair(1, 2);

or

或者

std::pair<int, int>(1, 2);

just use

只是使用

{1, 2};