C++ 如何将唯一的_ptr从优先级队列传输到队列

C++ 如何将唯一的_ptr从优先级队列传输到队列,c++,c++11,stl,smart-pointers,C++,C++11,Stl,Smart Pointers,简而言之,我有一个priority\u队列选择k无序集满足特定条件。我想将它们(散列集)作为队列返回 由于priority\u队列的创建和维护需要元素交换,因此我使用指针而不是unordered\u set作为priority\u队列的条目 因此,返回类型应该是队列 如果我使用shared\u ptr,代码运行良好,但我希望使用unique\u ptr,因为它更经济,客户承诺将其用作unique\u ptr 如何使用unique\u ptr实现以下代码 -------------------详细

简而言之,我有一个
priority\u队列
选择k
无序集
满足特定条件。我想将它们(散列集)作为
队列
返回

由于
priority\u队列的创建和维护需要元素交换,因此我使用指针而不是
unordered\u set
作为
priority\u队列的条目

因此,返回类型应该是
队列

如果我使用
shared\u ptr
,代码运行良好,但我希望使用
unique\u ptr
,因为它更经济,客户承诺将其用作
unique\u ptr

如何使用
unique\u ptr
实现以下代码

-------------------详细描述-------------------------------

我有一个函数可以从文件中读取保留大小与参考大小最接近的
k
行。例如,如果
k=2
,则参考大小为
5
,并且文件包含6行大小(此行中的整数数)
3,5,6,20,2,1
k-最近的
行是大小分别为
5
6
的两行

我使用大小为
k
priority\u队列
和定制的比较器来实现目标。我决定返回一个
队列
,其中包含所选的
k-最近的
行,因为客户端不想知道比较器是如何实现的(比较器是
优先级队列
模板的参数)

使用ptr\u type=shared\u ptr;
// ???????????????????????????????????????
//使用ptr_type=unique_ptr;//唯一\u ptr不起作用
// ???????????????????????????????????????
//是否可以将唯一的\u ptr条目从优先级\u队列传输到队列?
使用pair_comm_type=pair;
队列f(){
//myFile.txt是一个以空格分隔的整数文件。
//不同的行可能有不同的长度(整数数)
字符串inputFile=“myFile.txt”;
const int TOP_K_线=3;
//打开文件
ifstream fin(inputFile.c_str());
字符串读取缓冲区;
//文件打开了
//定义优先级队列的步骤
//定义自定义比较函数,以便保留的行具有大小
//最接近参考值。
双参考尺寸=log10(10.0);
自动合成=[&referenceSize](常数对通信类型和LHS,常数对通信类型和RHS)
{返回abs(log10(LHS.first)-referenceSize)
>bufIntValue)lineBufferPtr->insert(bufIntValue);
//一行读
//根据此行的长度决定是否保留
int arraySize=lineBufferPtr->size();
if(myHeap.size()如果(arraySize
STL
容器是为移动而设计的,当您这样做时,它与使用指针一样高效。事实上,它们在内部使用指针,所以您不必这样做

我会考虑只使用这样的值:

using pair_comm_type = pair<int, unordered_set<int>>;

queue<pair_comm_type> f() {

  string inputFile = "myFile.txt";
  const int TOP_K_LINE = 3;

  ifstream fin(inputFile.c_str());
  string readBuffer;

  double referenceSize = log10(10.0);
  auto comp = [&referenceSize](const pair_comm_type &LHS, const pair_comm_type &RHS)
      { return abs(log10(LHS.first)-referenceSize)
      < abs(log10(RHS.first)-referenceSize); };
  priority_queue<pair_comm_type, vector<pair_comm_type>, decltype(comp)> myHeap(comp);

  int bufIntValue = -1;
  int curMinArraySize = -1;

  while (getline(fin,readBuffer)) {
    istringstream S(readBuffer);

    // no need to use pointers here
    unordered_set<int> lineBufferPtr;
    while (S>>bufIntValue)
        lineBufferPtr.insert(bufIntValue);

    int arraySize = lineBufferPtr.size();
    if (myHeap.size() < TOP_K_LINE) {

      myHeap.emplace(arraySize,std::move(lineBufferPtr));
      curMinArraySize = myHeap.top().first;
      continue;
    }
    if (arraySize <= curMinArraySize) continue;
    myHeap.emplace(arraySize,std::move(lineBufferPtr));
    myHeap.pop();
    curMinArraySize = myHeap.top().first;
  }

  fin.close();

  // Use std::move to transfer the top() element which will be
  // just as efficient as using pointers

  queue<pair_comm_type> Q;

  while (!myHeap.empty()) {
    auto temp = std::move(myHeap.top()); // USE MOVES
    myHeap.pop();
    Q.push(std::move(temp));
  }

  return Q;
}
使用pair\u comm\u type=pair;
队列f(){
字符串inputFile=“myFile.txt”;
const int TOP_K_线=3;
ifstream fin(inputFile.c_str());
字符串读取缓冲区;
双参考尺寸=log10(10.0);
自动合成=[&referenceSize](常数对通信类型和LHS,常数对通信类型和RHS)
{返回abs(log10(LHS.first)-referenceSize)
>bufIntValue)
lineBufferPtr.insert(bufIntValue);
int arraySize=lineBufferPtr.size();
if(myHeap.size()如果(arraySize@Galik的解决方案有效

至于原来的问题,简单的答案是否定的。我们不能将唯一的ptr从优先级队列中转移出去


已删除参数为常量引用的
unique\u ptr
的返回类型。
priority\u queue::top()
的返回类型为。因此,我们无法使用返回值创建新的
unique\u ptr
对象

“由于优先级队列的创建和维护需要元素交换,因此我使用指针而不是无序集作为优先级队列的条目。”-这真的有必要吗?
无序集可以交换/移动。听起来您需要在移除优先级队列中的元素之前调用.release()(因此PQ不会删除指针)。您可以执行以下操作:unique_ptr tmp=std::move(PQ.front());PQ.pop();return tmp;但我还没有查看unique_ptr是否可移动。
using pair_comm_type = pair<int, unordered_set<int>>;

queue<pair_comm_type> f() {

  string inputFile = "myFile.txt";
  const int TOP_K_LINE = 3;

  ifstream fin(inputFile.c_str());
  string readBuffer;

  double referenceSize = log10(10.0);
  auto comp = [&referenceSize](const pair_comm_type &LHS, const pair_comm_type &RHS)
      { return abs(log10(LHS.first)-referenceSize)
      < abs(log10(RHS.first)-referenceSize); };
  priority_queue<pair_comm_type, vector<pair_comm_type>, decltype(comp)> myHeap(comp);

  int bufIntValue = -1;
  int curMinArraySize = -1;

  while (getline(fin,readBuffer)) {
    istringstream S(readBuffer);

    // no need to use pointers here
    unordered_set<int> lineBufferPtr;
    while (S>>bufIntValue)
        lineBufferPtr.insert(bufIntValue);

    int arraySize = lineBufferPtr.size();
    if (myHeap.size() < TOP_K_LINE) {

      myHeap.emplace(arraySize,std::move(lineBufferPtr));
      curMinArraySize = myHeap.top().first;
      continue;
    }
    if (arraySize <= curMinArraySize) continue;
    myHeap.emplace(arraySize,std::move(lineBufferPtr));
    myHeap.pop();
    curMinArraySize = myHeap.top().first;
  }

  fin.close();

  // Use std::move to transfer the top() element which will be
  // just as efficient as using pointers

  queue<pair_comm_type> Q;

  while (!myHeap.empty()) {
    auto temp = std::move(myHeap.top()); // USE MOVES
    myHeap.pop();
    Q.push(std::move(temp));
  }

  return Q;
}