gpt4 book ai didi

c++如何保存 vector 项和 double

转载 作者:行者123 更新时间:2023-11-28 07:01:17 35 4
gpt4 key购买 nike

我正在尝试填充对象 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com