gpt4 book ai didi

c++ - 使用过多内存的文件解析 (C++)

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:57:13 24 4
gpt4 key购买 nike

我有一个程序可以从 .raw 文件中渲染内容,这是一个示例:

1.000000 1.000000 -1.000000 1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 -1.000000 1.000000 -1.000000 
1.000000 0.999999 1.000000 -1.000000 1.000000 1.000000 -1.000000 -1.000000 1.000000 0.999999 -1.000001 1.000000
1.000000 1.000000 -1.000000 1.000000 0.999999 1.000000 0.999999 -1.000001 1.000000 1.000000 -1.000000 -1.000000
1.000000 -1.000000 -1.000000 0.999999 -1.000001 1.000000 -1.000000 -1.000000 1.000000 - 1.000000 -1.000000 -1.000000
-1.000000 -1.000000 -1.000000 -1.000000 -1.000000 1.000000 -1.000000 1.000000 1.000000 - 1.000000 1.000000 -1.000000
1.000000 0.999999 1.000000 1.000000 1.000000 -1.000000 -1.000000 1.000000 -1.000000 -1.000000 1.000000 1.000000

这些只是立方体的面,我的问题是,这个文件是 650 字节,现在当我加载程序时,程序占用了 20mb。这不是什么大问题,但是当加载更大的文件时,它可能会成为一个大问题。

这是我的文件短语器的代码(抱歉,如果它很乱,我是 C++ 的新手):

/*
* PhraseObj.cpp
*
* Created on: Nov 5, 2011
* Author: tom
*/

#include "PhraseObj.h"

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "obj.h"

PhraseObj::PhraseObj(string file)
{
FILE.open(file.c_str());
cout << FILE.is_open() << "\n";
}

obj PhraseObj::getObj()
{
string line;
Polygon P;
vertex V;
obj O;
int place;
int NextPos;
float x;
float y;
float z;
getline(FILE, line);
while (!FILE.eof()) {

cout << "new line" << "\n";

place = 0;
while (place != string::npos) {
if (place == (line.length() - 1)) {
break;
}

NextPos = line.find(" ", place + 1);
line.substr(place, (NextPos - place) - 1);
cout << line.substr(place, (NextPos - place) - 1).c_str() << " ";
x =::atof(line.substr(place, (NextPos - place) - 1).c_str());

V.x(x);
cout << x << "\n";

place = NextPos;
NextPos = line.find(" ", place + 1);
line.substr(place, NextPos - place);
cout << line.substr(place + 1, (NextPos - place) - 1).c_str() << " ";
y =::atof(line.substr(place + 1, (NextPos - place) - 1).c_str());
V.y(y);
cout << y << "\n";

place = NextPos;
NextPos = line.find(" ", place + 1);
line.substr(place + 1, NextPos - place);
cout << line.substr(place + 1, (NextPos - place) - 1).c_str() << " ";
z =::atof(line.substr(place + 1, (NextPos - place) - 1).c_str());
V.z(z);
cout << z << "\n";

P.addPoint(V);
place = line.find(" ", place + 1);
cout << "place: " << place << " " << "length:" << line.length() << "\n";
}
getline(FILE, line);
O.AddPolygon(P);
}
cout << "returning" << "\n";
try {
return O;
}
catch(bad_alloc & ba) {
cout << "bad_alloc caught: " << ba.what() << endl;
}
}

void PhraseObj::closeFile()
{
FILE.close();
}

bool PhraseObj::isEnd()
{
return FILE.eof();
}

一些旁注,一个顶点是一个 x、y 和 z float 。
多边形是顶点的 vector 。
obj 是多边形的 vector 。

谢谢,汤姆。

和vertex.h:

/*
* vertex.h
*
* Created on: Oct 26, 2011
* Author: tom
*/

#ifndef VERTEX_H_
#define VERTEX_H_


class vertex {
float x_;
float y_;
float z_;
public:
//here are some getters
float x() {return x_;} ;
float y() {return y_;} ;
float z() {return z_;} ;
// and now for some setters
void x(float _x) {x_ = _x;} ;
void y(float _y) {y_ = _y;} ;
void z(float _z) {z_ = _z;} ;
};


#endif /* VERTEX_H_ */

和多边形.cpp:

/*
* Polygon.cpp
*
* Created on: Oct 26, 2011
* Author: tom
*/

#include "Polygon.h"


using namespace std;

void Polygon::addPoint(vertex V) {
point.push_back(V);
}

int Polygon::getNumOfPoints() {
return point.size();
}

vertex Polygon::getPoint(int I) {
return point.at(I);
}

最佳答案

我不知道为什么你会看到如此过多的内存使用,所以这并不是你问题的确切答案,但我想指出你可以通过使用 PhraseObj::getObj()std::istringstream 个对象:

obj PhraseObj::getObj()
{
obj O;
string line;
while ( getline( FILE, line ) ) {
istringstream vertices( line );
istream_iterator<double> it( vertices ), end;

Polygon p;
for ( int i = 0; i < 4; ++i ) {
Vertex v;
v.x( *it++ );
v.y( *it++ );
v.z( *it++ );
p.addPoint( v );
}

O.AddPolygon( p );
}
}

您可以将字符串粘贴到 std::istream_iterator 对象中,然后使用 std::string 对象迭代单独的顶点,而不是使用 istringstream 方法从一个空格字符转到下一个。如果您知道每行始终有 12 个坐标顶点(每个顶点三个,每个多边形四个顶点),那么您可以使用一个简单的循环来读取它们。

关于c++ - 使用过多内存的文件解析 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8062533/

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