Java到C++;转换代码

Java到C++;转换代码,java,c++,Java,C++,我有一个Java代码;这是我的Poin Java代码: import java.util.Scanner; public class Poin { private int X; private int Y; public Poin() { X = 0; Y = 0; } public Poin(int X, int Y) { this.X = X; this.Y = Y; }

我有一个Java代码;这是我的Poin Java代码:

import java.util.Scanner;

public class Poin {
    private int X;
    private int Y;

    public Poin() {
        X = 0;
        Y = 0;
    }

    public Poin(int X, int Y) {
        this.X = X;
        this.Y = Y;
    }

    public int getX() {
        return X;
    }
    public int getY() {
        return Y;
    }
    public boolean InRect(Poin TopLeft, Poin BottomRight) {
        if (this.X < BottomRight.getX() && this.X > TopLeft.getX()
                && this.Y < BottomRight.getY() && this.Y > TopLeft.getY()) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String [] args) {
        int N;
        int i;
        int x,y;
        int count = 0;
        Poin TopLeft, BottomRight;

        Scanner sc = new Scanner(System.in);
        N = sc.nextInt();

        Poin[] a = new Poin[N];
        int x_top = sc.nextInt();
        int y_top = sc.nextInt();
        int x_bot = sc.nextInt();
        int y_bot = sc.nextInt();
        TopLeft = new Poin(x_top, y_top);
        BottomRight = new Poin(x_bot, y_bot);
        for (i = 0; i < N; i++) {
            x = sc.nextInt();
            y = sc.nextInt();
            Poin p = new Poin(x, y);
            a[i] = p;
            if (p.InRect(TopLeft, BottomRight)) {
                count += 1;
            }
        }
        System.out.println(count);
        for (i = 0; i < N; i++) {
            System.out.println(a[N-1-i].getX()+","+a[N-1-i].getY());
        }
    }
}

<是否有任何解决问题的方法?C++中的

< p>不需要使用<代码>新< /C> >创建新对象,只需声明变量就足够了:

Poin TopLeft, BottomRight;
现在,两个对象被声明、定义和创建

如果要指定构造函数参数,可以在声明中执行此操作:

Poin TopLeft(x_top, y_top), BottomRight(x_bot, y_bot);


在您开始学习指针之前,您不需要
new
,如果您想编写任何严肃的程序,您可能很快就应该学习指针。还请记住,在C++中,动态分配的对象(分配给<代码>新< /代码>)不会自动释放,必须手动完成,然后使用代码< >删除>代码>对象。

< p>如果您有特殊的原因使用<代码>新< /COD> < /P>
Poin* TopLeft;
Poin* BottomRight;
TopLeft = new Poin(x_top, y_top);
BottomRight = new Poin(x_bot, y_bot);
否则

Poin TopLeft(x_top, y_top);
Poin BottomRight(x_bot, y_bot);

如果你来自java后台,不理解C++指针是如何工作的,那么第二个选项对你来说会更好。p> 来自Java时,您必须打破使用

new
关键字声明所有变量的坏习惯。在C++中,大多数时候你不会使用那个关键字。
Poin TopLeft, BottomRight;
...
TopLeft = new Poin(x_top, y_top); // TopLeft of is type Poin, new returns a type Poin*
BottomRight = new Poin(x_bot, y_bot); // same here
你真正想要的是:

cin>>x_top;
cin>>y_top;
cin>>x_bot;
cin>>y_bot;
Poin TopLeft(x_top, y_top);
Poin BottomRight(x_bot, y_bot);

虽然你已经对你所见过的表面问题提出了一些建议,但是遵循这个建议(本身)的结果仍然是我认为的糟糕的C++代码。到目前为止,基本上是从java到C++的音译,在使C++编译器接受基本上是java代码的语法中,需要做的语法变化最小。 我建议,C++编写的是真正的C++。这将与Java代码大不相同,因为这两种语言的本质上是完全不同的语言,而编写用于C++的代码与java中的java代码相同。

让我们考虑一下你的代码真正的原理,并编写一些C++来做。

  • 从文件中读入点(每个点是一对
    int
    s)
  • 计算矩形内的点
  • 把计数打印出来
  • 按相反顺序打印出各点
  • 我们希望编写代码以尽可能清晰地支持这一点,因此让我们从为作业正确定义的
    poin
    类开始。必要的能力包括:

  • 从int构造
  • 从流中读取
  • 写入流
  • 检查是否在矩形中
  • 这应该就是它所需要的,所以让我们实现它:

    class Poin {
        int x;
        int y;
    public:
        // construct a point from two ints. Note that we prefer to use the member 
        // initialization list over assigning inside the body of the ctor
        Poin(int x=0, int y=0) : x(x), y(y) {}
    
        bool operator<(Poin const &other) const {
            return x < other.x && y < other.y;
        }
    
        bool in_rect(Poin const &TL, Poin const &BR) const {
            return (*this < BR) && (TL < *this);
        }
    
        // read a Poin from a stream:    
        friend std::istream &operator>>(std::istream &is, Poin &p) {
            return is >> p.x >> p.y;
        }
    
        // display a Poin on a stream. Note: operator>> won't read what this writes
        friend std::ostream &operator<<(std::ostream &os, Poin const &p) {
            return os << p.x << "," << p.y;
        }
    };
    
    我不得不努力工作来抵制诱惑,不去阐述这段代码的优越性和简单性,以及与Java这样的低级语言相比的优越性,但我想我会让这段代码在这个问题上独树一帜

    不过,我要补充最后一点:这段代码是用C++11编写的。如果您使用的是较旧的编译器,您可能会遇到一些问题。两个明显的问题是
    点的初始化
    ——对于较旧的编译器,您可能需要稍微更改语法,以:

    std::vector<Poin> points( 
        (std::istream_iterator<Poin>(std::cin)),
        std::istream_iterator<Poin>());
    

    这也可以解决,但如果您在这两个方面都有问题,我建议您更新编译器,而不是大量使用代码。这些都是在C++11中引入的,(许多)编译器甚至在标准最终确定之前就支持它们。要扭转旧的广告口号,这是一个比切换更好的地方。

    你熟悉C++和声明对象的不同方式吗?在这种情况下,您可能希望
    点位于左上角(x_-top,y_-top);Poin BottomRightPoin(x\u bot,y\u bot)
    new
    将返回指向
    Poin
    对象的指针。具体问题是什么?
    class Poin {
        int x;
        int y;
    public:
        // construct a point from two ints. Note that we prefer to use the member 
        // initialization list over assigning inside the body of the ctor
        Poin(int x=0, int y=0) : x(x), y(y) {}
    
        bool operator<(Poin const &other) const {
            return x < other.x && y < other.y;
        }
    
        bool in_rect(Poin const &TL, Poin const &BR) const {
            return (*this < BR) && (TL < *this);
        }
    
        // read a Poin from a stream:    
        friend std::istream &operator>>(std::istream &is, Poin &p) {
            return is >> p.x >> p.y;
        }
    
        // display a Poin on a stream. Note: operator>> won't read what this writes
        friend std::ostream &operator<<(std::ostream &os, Poin const &p) {
            return os << p.x << "," << p.y;
        }
    };
    
    int main() {
        // read points defining the rectangle from standard input:
        Poin TL, BR;
        std::cin >> TL >> BR;
    
        // read points from standard input to initialize collection:    
        std::vector<Poin> points{ 
            std::istream_iterator<Poin>(std::cin),
            std::istream_iterator<Poin>() };
    
        // display count of points inside rectangle:
        std::cout << "Count: "
                  << std::count_if(points.begin(), points.end(), 
                                   [&](Poin const &p) { return p.in_rect(TL, BR); })
                  << "\n";
    
        // display all points in reverse order:
        std::copy(points.rbegin(), points.rend(),
            std::ostream_iterator<Poin>(std::cout, "\n"));
    }
    
    std::vector<Poin> points( 
        (std::istream_iterator<Poin>(std::cin)),
        std::istream_iterator<Poin>());
    
    [&](Poin const &p) { return p.in_rect(TL, BR); }