在C++优先队列中自定义比较器类的拷贝构造函数被过度调用。

huangapple go评论69阅读模式
英文:

Excessive Invocation of Copy Constructor for Custom Comparator Class in C++ Priority Queue

问题

I had declared a priority_queue with custom comparator having a vector property in it. Below is the full code for it:

CODE

#include <bits/stdc++.h>

using namespace std;

class Compare
{
private:
    vector<int> vec;

public:
    Compare(const vector<int> &vec) { this->vec = vec; }

    Compare(const Compare &obj)
    {
        this->vec = obj.vec;
        cout << "Copy Constructor Called!\n";
    }

    bool operator()(const int &left, const int &right)
    {
        return vec[left] > vec[right];
    }
};

int main(void)
{
    vector<int> vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    priority_queue<int, vector<int>, Compare> pq{Compare(vec)};

    cout << "Pushing 1\n";
    pq.push(1);
    cout << "Pushed 1\n";
    cout << "Pushing 2\n";
    pq.push(2);
    cout << "Pushed 2\n";

    while (!pq.empty())
    {
        cout << "Popped = " << pq.top() << '\n';
        pq.pop();
    }
    return 0;
}

But the output is strange!!

OUTPUT

Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Pushing 1
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Pushed 1
Pushing 2
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Pushed 2
Popped = 1
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Popped = 2
Copy Constructor Called!

Why the copy constructor is called so many times, for every push and pop operation?

Also without any push or pop operation, when I just declared the priority_queue, the copy constructor is called 4 times

Why is this occurring? Isn't it correct that the priority_queue stores the comparator class object and utilizes it whenever comparisons are needed?

英文:

I had declared a priority_queue with custom comparator having a vector property in it. Below is the full code for it:

CODE

#include &lt;bits/stdc++.h&gt;

using namespace std;

class Compare
{
private:
    vector&lt;int&gt; vec;

public:
    Compare(const vector&lt;int&gt; &amp;vec) { this-&gt;vec = vec; }

    Compare(const Compare &amp;obj)
    {
        this-&gt;vec = obj.vec;
        cout &lt;&lt; &quot;Copy Constructor Called!\n&quot;;
    }

    bool operator()(const int &amp;left, const int &amp;right)
    {
        return vec[left] &gt; vec[right];
    }
};

int main(void)
{
    vector&lt;int&gt; vec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    priority_queue&lt;int, vector&lt;int&gt;, Compare&gt; pq{Compare(vec)};

    cout &lt;&lt; &quot;Pushing 1\n&quot;;
    pq.push(1);
    cout &lt;&lt; &quot;Pushed 1\n&quot;;
    cout &lt;&lt; &quot;Pushing 2\n&quot;;
    pq.push(2);
    cout &lt;&lt; &quot;Pushed 2\n&quot;;

    while (!pq.empty())
    {
        cout &lt;&lt; &quot;Popped = &quot; &lt;&lt; pq.top() &lt;&lt; &#39;\n&#39;;
        pq.pop();
    }
    return 0;
}

But the output is strange!!

OUTPUT

Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Pushing 1
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Pushed 1
Pushing 2
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Pushed 2
Popped = 1
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Copy Constructor Called!
Popped = 2
Copy Constructor Called!

Why the copy constructor is called so many times, for every push and pop operation?

Also without any push or pop operation, when I just declared the priority_queue, the copy constructor is called 4 times

Why is this occurring? Isn't it correct that the priority_queue stores the comparator class object and utilizes it whenever comparisons are needed?

答案1

得分: 3

std::make_heap按值传递比较器libstdc++ 实现 make_heap 的许多内部函数也是如此。libc++ 传递方式较少按值传递,但也有一些按值传递。

比较器应该便宜复制。如果它们需要外部数据,请让它们存储指向外部数据的指针。

英文:

std::make_heap takes the comparator by value see, and so do numerous internal functions in the libstdc++ implementation of make_heap. libc++ does less passing by value, but it does some.

Comparators are supposed to be cheap to copy. If they need external data, make them store a pointer to external data.

huangapple
  • 本文由 发表于 2023年6月29日 22:12:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76581879.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定