从向量移动初始化priority_queue?

时间:2017-11-08 22:16:22

标签: c++ move-semantics

我看到了这个leetcode question,并希望用优先级队列而不是向量来解决它(因此O(nlogk)而不是O(nk))。如何使用给定向量作为基础容器初始化priority_queue?这是我尝试的但我明显误解了docs,因为它不会编译。

var express = require('express');
var bodyParser = require('body-parser');
var router = express.Router();
var AWS = require('aws-sdk');

router.use(bodyParser.json());
router.use(bodyParser.urlencoded({ extended: false }));

AWS.config.update({
  region: "us-east-1"
});

AWS.config.apiVersions = {
  sns: '2010-03-31'
};

var sns = new AWS.SNS();

router.get('/', function(req, res) {
  console.log('The GET is hitting');
  res.send(req.body);
});

router.post('/', function(req, res) {
  console.log('THE BODY IS ', req);
  console.log('THE REQUEST BODY IS ', req.body);
  if(req.body.Type === 'SubscriptionConfirmation') {
    console.log("Subscription Confirmation Message--->"+req.body);
    sns.confirmSubscription({
      Token: req.body.Token,
      TopicArn: req.body.TopicArn,
      Type: req.body.Type
    });
      console.log('MESSAGE DATA IS ', req.body.Token);
    } else if (req.body.Type === 'Notification') {
      console.log('Notification has arrived')
    } else {
      console.log('Unexpected message ' + req.body)
    }
});

1 个答案:

答案 0 :(得分:1)

你的意思是

priority_queue<ListNode*, vector<ListNode*>, cmp> pq{ cmp{}, std::move(lists) };

您的代码失败,因为默认情况下比较器为std::less<typename Container::value_type>(因此您必须在模板args中明确地写cmp)并且因为参数必须是cmp的实例(不是在类中,实际上类不是C ++中的一等公民,你不能将它们作为参数传递。)