C ++中的make_pair() [英] make_pair() in C++

查看:84
本文介绍了C ++中的make_pair()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过leetcode执行问题337.这是我实现的代码.

I was doing the problem 337 from leetcode. This is the code I implemented.

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int rob(TreeNode* root) {

        unordered_map<TreeNode*, int> memo;

        return robSub(root, memo);
    }

private:
    int robSub(TreeNode* root, unordered_map<TreeNode*, int>& memo) {

        if (root == nullptr) {
            return 0;
        }

        if (memo.find(root) != memo.end()) {
            return memo.find(root)->second;
        }

        int leftGrand = 0;
        int rightGrand = 0;

        if (root->left != nullptr) {
            leftGrand = robSub(root->left->left, memo) + robSub(root->left->right, memo);
        }
        if (root->right != nullptr) {
            rightGrand = robSub(root->right->left, memo) + robSub(root->right->right, memo);
        }

        int result = max(root->val + leftGrand + rightGrand, robSub(root->left, memo) + robSub(root->right, memo));

        memo.insert(make_pair<TreeNode*, int>(root, result));
        return result;

    }
};

然后它报告了一个错误:

Then it reported an error:

Line 42: Char 59: error: no matching function for call to `make_pair<TreeNode*, int>(TreeNode*&, int&))`

为什么会发生错误,并且 make_pair<>()中的参数成为引用?有人可以帮忙吗?

Why the error happened and the arguments in make_pair<>() become references? Can someone help?

我将 make_pair< TreeNode *,int>(根,结果)修改为 make_pair(根,结果),然后它起作用了.它们之间有什么区别?

I modified make_pair<TreeNode*, int>(root, result) to make_pair(root, result), then it worked. What's the differences between them?

推荐答案

TL; DR:不要指定 std :: make_pair 的模板参数.让编译器来推断它们.

TL;DR: Don't specify template arguments of std::make_pair. Let the compiler deduce them.

std :: make_pair 在C ++ 11中已更改.

The meaning of template parameters of std::make_pair was changed in C++11.

它曾经是:

template <class T1, class T2> /*...*/ make_pair( T1 t, T2 u );

但是现在是:

template <class T1, class T2> /*...*/ make_pair(T1 &&t, T2 &&u);
// `t` and `u` are forwarding references.

该代码在C ++ 11之前是有效的,但现在不再有效.

The code was valid pre-C++11, but it no longer is.

您可以相应地更改模板参数: make_pair< TreeNode *& ;, int&>(root,result),但是没有理由手动指定它们.编译器可以推断出它们很好.

You could change the template arguments accordingly: make_pair<TreeNode*&, int&>(root, result), but there is no reason to specify them manually. The compiler can deduce them just fine.

如果您不明白为什么模板参数必须是引用,请阅读转发引用.

If you don't understand why the template arguments have to be references, read about forwarding references.

为什么... make_pair<>()中的参数成为引用?

您的编译器可能会将参数类型显示为引用,以指示您正在将左值传递给 make_pair .

Probably your compiler displayed argument types as references to indicate that you're passing lvalues into make_pair.

这篇关于C ++中的make_pair()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆