【问题标题】:Why did my program call the copy constructor several times?为什么我的程序多次调用复制构造函数?
【发布时间】:2014-01-07 12:55:37
【问题描述】:

我的代码只从文件中读取Points,然后按自然顺序对它们进行排序(先比较y坐标,然后比较x坐标),然后按照点到第二点的斜率对它们进行排序。

对于第一次排序,我重载了< 运算符并调用了sort();

对于第二次排序,我创建了一个由第二点初始化的函数对象。

我重写了Point 的复制构造函数以找出任何不必要的复制,发现我复制了第二个Point 很多次,但我不明白为什么。谁能给我一个线索?

输出:

C:\Users\lenovo\Desktop>test.exe < input10.txt
During initialization : 0
Input reading has complete!
Sort by natural order : (28000,1000) (28000,5000) (28000,13500) (23000,16000) (1000,
18000) (13000,21000) (2000,22000) (3000,26000) (3500,28000) (4000,30000)
During soring : 0
Sort by slope : copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
copying28000,13500
(28000,13500) (4000,30000) (3500,28000) (23000,16000) (13000,21000) (3000,26000) (20
00,22000) (1000,18000) (28000,1000) (28000,5000)
During soring : 12

代码:

#include <iostream>
#include <iterator>
#include <vector>
#include <map>
#include <list>
#include <string>
#include <algorithm>
using namespace std;
int times;
class Point
{
    private:
        int x,y;
    public:
        Point() : x(0),y(0){}
        Point(int x,int y):x(x),y(y){}
        Point(const Point& p) : x(p.x),y(p.y) {  cout << "copying" <<x<<","<<y<<endl;times++;}
        double slopeTo(const Point& that) const
        {
            if (x == that.x && y == that.y) return - numeric_limits<double>::infinity();
            if (x == that.x) return numeric_limits<double>::infinity();
            if (y == that.y) return 0;
            return (that.y - y) / (double)(that.x - x);
        }
        bool operator< (const Point& that)const
        {

            if (y < that.y) return true;
            if (y == that.y && x < that.x) return true;6
            return false;
        }
    friend ostream& operator<< (ostream&, const Point& p);
};

class cmpBySlope
{
    private:
        Point origin;
    public:
        cmpBySlope(Point& a) : origin(a){}
        bool operator() (const Point* left,const Point* right)const
        {
                return origin.slopeTo(*left) < origin.slopeTo(*right);
        }

};
ostream& operator<< (ostream& out, const Point& p)
{
    cout << "(" << p.x << "," << p.y << ")" ;
    return out;
}


int N;
vector<Point*> v;
void create()
{

    cin >> N;
    for (int i = 0 ; i < N; i++)
    {
        int x,y;
        cin >> x >> y;
        Point* p = new Point(x,y);

    }
    cout << "During initialization : " << times << endl;

    cout << "Input reading has complete!" << endl;
}
bool cmp(const Point* p,const Point* q)
{
    return (*p)<(*q);
}
int main(void)        
{
    create();

    int before = times;
    cout << "Sort by natural order : ";
    sort(v.begin(),v.end(),cmp);
    for (Point* p : v)
        cout << *p << " ";
    cout << endl;
    cout << "During soring : " << (times - before) << endl;

    cout << "Sort by slope : ";
    before = times;
    sort(v.begin(),v.end(),cmpBySlope(*v[2]));
    for (Point* p : v)
    {
        cout << *p << " ";
    }
    cout << endl;
    cout << "During soring : " << (times - before) << endl;
}            

【问题讨论】:

  • 标准没有规定sort复制其谓词的次数。由于谓词是cmpBySlope 的一个实例,并且由于复制cmpBySlope 复制了它的Point 数据成员,这可能是原因。
  • 谢谢,我不明白为什么 sort 会调用 cmpBySlope 这么多次,因为递归?
  • 你可以观察到cmpBySlope被复制here
  • ideone.com/UGpLy4 通过将成员 Point origin 替换为引用 Point&amp; origin 来显示没有复制点。

标签: c++ stl copy-constructor


【解决方案1】:

如 cmets 所示,Point 副本来自 cmpBySlope 对象,该对象包含完整的 Point 实例。 cmpBySlope 被多次复制的事实是 sort 函数的一个实现细节。

看看这个answer,看看排序函数可能使用什么算法。在这两种情况下,都使用递归,并且由于比较对象是按值传递的,因此您必须期望创建副本。

【讨论】:

  • 非常感谢,很有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2020-06-29
相关资源
最近更新 更多