gpt4 book ai didi

c++ - 从 .in 文件读取对象

转载 作者:行者123 更新时间:2023-11-28 03:35:53 25 4
gpt4 key购买 nike

你好,我有一个由 2 个空格间隔的 3 个整数组成的列表,我想读取它们并创建一个对象。我将从数据结构、类和前/后数据中粘贴我的代码。我找不到错误的原因。欢迎任何帮助:

类.h:

class RAngle{
private:
int x,y,l,b;
int solution,prec;
RAngle(){
x = y = solution = prec = b = l = 0;
}

RAngle(int i,int j,int k){
x = i;
y = j;
l = k;
solution = 0; prec=0; b=0;
}

friend ostream& operator << (ostream& out, const RAngle& ra){
out << ra.x << " " << ra.y<<" " << ra.l <<endl;
return out;
}

friend istream& operator >>( istream& is, RAngle& ra){
is >> ra.x;
is >> ra.y;
is >> ra.l;

return is ;
}

};

数据结构.h:

template <class T>
class List
{
private:
struct Elem
{
T data;
Elem* next;
};

Elem* first;

void push_back(T data){
Elem *n = new Elem;
n->data = data;
n->next = NULL;
if (first == NULL)
{
first = n;
return ;
}
Elem *current;
for(current=first;current->next != NULL;current=current->next);
current->next = n;
}

主要.cpp:

void readData(List <RAngle> &l){
*RAngle r;
int N;
ifstream f_in;
ofstream f_out;

f_in.open("ex.in",ios::in);
f_out.open("ex.out",ios::out);

f_in >> N;
for(int i=0;i<13;++i){
f_in >> r;
cout << r;
l.push_back(r);
}

f_in.close();
f_out.close();
}*

输入数据:

3 1 2
1 1 3
3 1 1

输出(阅读时打印):

1 2 1
1 3 3
1 1 3

正如您所看到的,它以某种方式从第二位开始阅读并以第一位结束。

最佳答案

As you can see somehow it starts reading with 2-nd position and finishes with first.

那是因为你提取了第一个值。

readData函数中:

f_in >> N;    //  This line will extract the first value from the stream.

// the loop starts at the second value
for(int i=0;i<13;++i){
f_in >> r;
cout << r;
l.push_back(r);
}

关于c++ - 从 .in 文件读取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10887512/

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