c++如何保存 vector 项和 double

标签 c++ vector precision

我正在尝试填充对象 Point 3D 的 vector 。我的应用程序读取一个 csv 文件以通过三个坐标 x、y、z 加载 vector 。我使用 float 类型。 这是我的代码。

main.cpp

int main(int argc, char** argv) {
    char *theFileName = "file.csv"; //[100];
    vector<Point> v = getPointCloud(theFileName);
    for (int i = 0; i < v.size(); ++i) {
        v.at(i).print(cout);
    }

}

getPointCloud

vector<Point> getPointCloud(char *fileName) {
    string line;
    string token;
    vector<Point> v;
    double tab[3];
    ifstream file(fileName);
    if (file.is_open()) {
        while (getline(file, line)) {
            int cpt = 0;
            stringstream stream(line);
            while (getline(stream, token, ',')) {
                tab[cpt] = ::atof(token.c_str());
                cpt++;
            }
            Point p(tab[0], tab[1], tab[2]);
            p.print(cout);            <-- the display works 
            p.setColor(255, 0, 0);
            v.push_back(p);
        }
        file.close();
    } else {
        cout << "Unable to open " << fileName << '\n';
        exit(0);
    }

    return v;
}

我有两个问题:

1 - 当我尝试在 main 方法中显示点时,我发现三个坐标为 null ( == 0) 但在 getPointCloud 方法中显示效果很好。

2 - 有人可以提供一个简单的方法来保存我的坐标而不损失数学运算后的精度。我在网上搜索过,但我不明白如何解决它。我是 C++ 新手。

点.h

#ifndef POINT_H
#define POINT_H

#include <math.h>
#include <iostream>

class Point {
protected:
    float x;
    float y;
    float z;
    // color RGB
    float r;
    float g;
    float b;
public:
    // Constructors
    Point();
    //  Point(const Point& orig);
    Point(std::ostream &strm);
    Point(float x, float y, float z);
    Point(const Point& orig);
    virtual ~Point();

    //getters

    float getX() const {
        return this->x;
    }

    float getY() const {
        return this->y;
    }

    float getZ() const {
        return this->z;
    }

    float getR() const {
        return this->r;
    }

    float getG() const {
        return this->g;
    }

    float getB() const {
        return this->b;
    }

    //setters

    void setX(float x) {
        this->x = x;
    }

    void setY(float y) {
        this->y = y;
    }

    void setZ(float z) {
        this->z = z;
    }

    void setR(float r) {
        this->r = r;
    }

    void setG(float g) {
        this->g = g;
    }

    void setB(float b) {
        this->b = b;
    }

    void setColor(float r, float g, float b) {
        this->r = r;
        this->g = g;
        this->b = b;
    }

    /**
     * Print the point
     * @param strm
     */
    void print(std::ostream &strm);

    //Other methods

    float dist2D(Point &other);

    float dist3D(Point &other);

    Point swap(Point p);

//    Point operator-(const Point &other) const;
};

#endif  /* POINT_H */

点.cpp

#include <iostream>
#include <math.h>
#include <ostream>
using namespace std;

#include "Point.h"

Point::Point(const Point& orig) {
}

Point::Point(ostream &strm) {
    strm << "Type the abscissa: ", cin >> this->x;
    strm << "Type the ordinate: ", cin >> this->y;
    strm << "Type the applicate: ", cin >> this->z;
}

Point::Point(float x, float y, float z) : x(x), y(y), z(z) {
    // The default point color is blue
    this->r = 0;
    this->g = 0;
    this->b = 255;
}

/**
 * Destructor
 */
Point::~Point() {

}

//Other methods

float Point::dist2D(Point &other) {
    float xd = x - other.x;
    float yd = y - other.y;
    return sqrt(xd * xd + yd * yd);
}

float Point::dist3D(Point &other) {
    float xd = x - other.x;
    float yd = y - other.y;
    float zd = z - other.z;
    return sqrt(xd * xd + yd * yd + zd * zd);
}

Point Point::swap(Point p) {
    Point aux(x, y, z);
    x = p.x;
    y = p.y;
    z = p.z;
    return aux;
}

//Point Point::operator-(const Point &other) const {
//    return Point(other.getX() - this->x, other.getY() - this->y, other.getZ() - this->z);
//}

void Point::print(ostream &strm) {
    strm << "Point(" << this->x << "," << y << "," << z << ")" << endl;
}

提前致谢。

最佳答案

Point::Point(const Point& orig) {
}

不正确。 它不会将数据从 orig 复制到 *this

请复制此构造函数中的每个成员。

这看起来像这样:

Point::Point(const Point& orig) {
    x = orig.x ;
    y = orig.y ;
    x = orig.z ;

    r = orig.r ;
    g = orig.g ;
    b = orig.b ;
}

关于c++如何保存 vector 项和 double ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22431913/

相关文章:

c++ - 为什么不在一个线程中锁定互斥量不会阻止在不同线程中锁定相同的互斥量?

c++ - 访问 vector 对象的最佳方式

javascript - 对浮点相等性测试感到困惑?

C++ vector 指针/引用问题

c++ - 指向 Vector 的指针调用的方法无法修改对象

sql - Float 在数据库 Sql Server 中存储为 Real

python - json.encoder.FLOAT_REPR 已更改但没有效果

c++ - 在 C++ 中使用 UTF-8 字符串正确检查回文

c++ - 包含相邻顶点列表的自定义顶点类型 [图形] : incomplete type error

c++如何正确声明另一个类的友元类方法