简体   繁体   English

在std :: pair中使用`std :: make_pair`:C ++ STL

[英]Use of `std::make_pair` in std::pair : C++ STL

I noticed many a times that whenever one needs to assign values to a (new) std::pair , std::make_pair is used. 我注意到很多次,只要需要为(新) std::pair赋值,就会使用std::make_pair But I did not find any use of the make_pair function, since we can directly input values to a pair, and modify them as we like. 但是我没有找到make_pair函数的任何用法,因为我们可以直接将值输入到一对,并根据需要修改它们。 For example: 例如:

std::pair<int,int> newp;
std::cin>>newp.first>>newp.second;
newp.first = -1;

Then what exactly is the use of this function? 那究竟是什么功能的使用?

There is, its advantage is called Template argument deduction . 有,它的优点是模板参数推导 It saves a bit of typing and lets you use auto . 它节省了一些打字,让你使用auto class-template arguments must be explicitly specified, functions , mustn't. 必须明确指定类模板参数, 函数必须不能。


But it becomes redundant with C++17 , because we will have Template Argument Deduction For class-templates 但它在C ++ 17中变得多余,因为我们将为类模板提供模板参数演绎

std::make_pair is used to create a std::pair object with the specified values. std::make_pair用于创建具有指定值的std::pair对象。

Creates a std::pair object, deducing the target type from the types of arguments. 创建一个std :: pair对象,从参数类型中推导出目标类型。

As a template function which supports automatic template argument type deducing, it allows you omit specifying the target template argument type. 作为支持自动模板参数类型推导的模板函数,它允许您省略指定目标模板参数类型。 eg, 例如,

auto p1 = std::make_pair(1, 2);  // p1 is std::pair<int, int> with value {1, 2}

we can directly input values to a pair, and modify them as we like. 我们可以直接输入一对值,并根据需要修改它们。 For example: 例如:

 std::pair<int,int> newp; std::cin>>newp.first>>newp.second; newp.first = -1; 

Some problems I can think of: 我能想到的一些问题:

  1. You don't always have a stream object ready. 您并不总是准备好流对象。 std::cin is a very special case, and std::make_pair is a very generic function. std::cin是一个非常特殊的情况, std::make_pair是一个非常通用的函数。

  2. Who says that both types in the pair support operator>> ? 谁说这两种类型都支持operator>>

  3. Const correctness. Const正确性。 You may want to have a const pair. 你可能想要一个const对。

Let's put these three things together to create a non-compiling example: 让我们把这三件事放在一起创建一个非编译示例:

#include <utility>
#include <iostream>

struct Foo
{
    int i;
};

struct Bar
{
    double d;
};

void printPair(std::pair<Foo, Bar> const& pair)
{
    std::cout << pair.first.i << " " << pair.second.d << "\n";
}

void createAndPrintPairTwice(Foo const& foo, Bar const& bar)
{
    // error 1: no std::cin, need to use foo and bar
    // error 2: no operator>> for Foo or Bar
    // error 3: cannot change pair values after initialisation
    std::pair<Foo, Bar> const newp;
    std::cin >> newp.first >> newp.second;
    printPair(newp);
    printPair(newp);
}

int main()
{
    Foo foo;
    foo.i = 1;
    Bar bar;
    bar.d = 1.5;
    createAndPrintPairTwice(foo, bar);
}

std::make_pair solves all three problems and makes the code much nicer to read. std::make_pair解决了所有三个问题,并使代码更好阅读。 Note that you don't have to repeat the pair's template arguments: 请注意,您不必重复该对的模板参数:

void createAndPrintPairTwice(Foo const& foo, Bar const& bar)
{
    std::pair<Foo, Bar> const pair = std::make_pair(foo, bar);
    printPair(pair);
    printPair(pair);
}

What's true is that C++11 has rendered std::make_pair much less useful than before, because you can now also write: 真正的是,C ++ 11使得std::make_pair比以前更有用,因为你现在也可以写:

void createAndPrintPairTwice(Foo const& foo, Bar const& bar)
{
    auto const pair = std::pair<Foo, Bar> { foo, bar };
    printPair(pair);
    printPair(pair);
}

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

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