不知道错哪儿了,Vector的操作不会

didoleo 2005-04-17 10:44:15
//片段:有个Hotel类,
class Hotel {
public:
int hotel_code;
char hotel_name[50];
char hotel_city[50];

int total_regular_rooms;
double tariff_regular_room;
int available_regular_rooms;

int total_deluxe_rooms;
double tariff_deluxe_room;
int available_deluxe_rooms;
}
//.....
//省略
//.....
vector<Hotel> Vh(1);
Hotel h;
ofstream OutputFile;
ifstream InputFile;

//....前面从cin读进了几条Hotel的记录然后准备望文件里写.(不知道Vh.size()这个有没问题,
我明明只输入了一条记录,但Vh.size()返回的却是2,总是成倍的???????
)
for(int n=0;n<Vh.size();n++){
OutputFile.write((char*)&Vh[n],sizeof(Vh[n]));
}

InputFile.read((char*)&h,sizeof(h));
while(!InputFile.eof())
{
Vh.push_back(h);
InputFile.read((char*)&h,sizeof(h));

}
InputFile.close();
InputFile.clear();

//下面我输出从文件里读出来的记录,下面的内容是对的,但是老是会在前面先打出一大堆乱码来,
//why??
for(int n=0;n<Vh.size();n++){
cout<<Vh[n].hotel_code<<endl;
cout<<Vh[n].hotel_name<<endl;
cout<<Vh[n].hotel_city<<endl;

}

谢谢各位老大.
...全文
321 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
zengwujun 2005-04-18
  • 打赏
  • 举报
回复

//楼主看来对vector的capacity和size混淆不清
//capacity:容量
//size:已站容量
//vector<int> coll(2);//<--分配两个元素,如果capacity不够,倍增
//coll.reserve()//<--改变容量
//coll.resize(3);//<--改变已站容量
//coll.empty();//<--依据size()进行判断,size()为0返回true;

看看这个例子,体会体会
#include <vector>
#include <stdio.h>
using namespace std;

void main()
{
vector<int> coll(2);
printf("capacity=%d,size=%d\n",coll.capacity(),coll.size());
coll.push_back(1);
printf("capacity=%d,size=%d\n",coll.capacity(),coll.size());
coll.push_back(2);
coll.reserve(10);
printf("capacity=%d,size=%d\n",coll.capacity(),coll.size());
coll.push_back(3);
printf("capacity=%d,size=%d\n",coll.capacity(),coll.size());
coll.push_back(4);
coll.resize(3);
printf("capacity=%d,size=%d\n",coll.capacity(),coll.size());
coll.push_back(5);
printf("capacity=%d,size=%d\n",coll.capacity(),coll.size());
coll.push_back(6);
}
ghostsG 2005-04-18
  • 打赏
  • 举报
回复
up
WuYL7812 2005-04-18
  • 打赏
  • 举报
回复
你用vector<Hotel> Vh(1);时Vh里面已经有一个元素了,此元素是用Hotel得默认构造函数构造形成的,当然是乱码。以后条用push_back是不会把添加的元素放到已经有的内容里的。
WuYL7812 2005-04-18
  • 打赏
  • 举报
回复
vector<Hotel> Vh(1);
这条语句错了,应该是 vector<Hotel> Vh;
用默认参数
didoleo 2005-04-18
  • 打赏
  • 举报
回复
自己up一下,下午来看.
didoleo 2005-04-18
  • 打赏
  • 举报
回复
to:1982pc()
谢谢1982pc() 老兄

我们老师太变态了,一定要用Vector,上课又只会读课本,又没例子.

vector<Hotel> Vh(1);这样是定义了一个只有能容纳一个元素的数组,但这个元素是空的.
vector的好处就是他会随着记录的增加自动扩大容量.我定义vector<Hotel> Vh(100)也可以
他只表明了他的容量,就象 int intArr[100],这个intArr里面没东西的,除非我给他们都赋值.


你能不能给我一个完整一点的代码让我学一下呢?

功能就是录入hotel的信息,每个hotel要给他编个码,就用int型好了,每个hotel信息当成一条记录,存放到文件hotel.dat里,要用vector.

谢谢.分不够再加

1982pc 2005-04-18
  • 打赏
  • 举报
回复
void addNewHotel(char *filename)
{
system("cls");
ofstream OutputFile;
ifstream InputFile;
vector<Hotel> Vh(1);
Hotel h;
int maxhotelcode=1;
int hotelcode;
InputFile.open(filename,ios::binary|ios::in);

if(!InputFile){

hotelcode=1;
}else
{
//int o=0;
InputFile.read((char*)&h,sizeof(h));

while(!InputFile.eof())
{
Vh.push_back(h);
cout<<maxhotelcode;
maxhotelcode++;
InputFile.read((char*)&h,sizeof(h));

}
InputFile.close();
InputFile.clear();


}


cout << " ADD NEW HOTEL DETAILS" << endl;
cout << "New Hotel Code is:"<<maxhotelcode<<endl;



OutputFile.open(filename,ios::binary|ios::out);


char reply='Y';



h.hotel_code=maxhotelcode;
maxhotelcode++;
cout << "Enter Hotel/Resort name:" << endl;
cin >>h.hotel_name;
cout << "Enter City name:" << endl;
cin >> h.hotel_city;
cout << "Enter the number of Regular rooms:" << endl;
cin >> h.available_regular_rooms;
cout << "Enter the tariff for Regular rooms:" << endl;
cin >> h.tariff_regular_room;
cout << "Enter the number of Deluxe rooms:" << endl;
cin >> h.available_deluxe_rooms;
cout << "Enter the tariff for Deluxe rooms:" << endl;
cin >> h.tariff_deluxe_room;

Vh.push_back(h);



for(int n=0;n<Vh.size();n++){
//cout<<Vh[n].hotel_code<<endl;
//cout<<Vh[n].hotel_name<<endl;
//cout<<Vh[n].hotel_city<<endl;
OutputFile.write((char*)&Vh[n],sizeof(Vh[n]));//这里当你第一次运行的时候其实只有一条纪录,可是你写如两次!!!为什么呢?那是因为你定义的时候Vh(1),已经有一纪录了,你再push_back就是二了,只需要把这里改成Vh!!!!!!!!
}


OutputFile.close();
OutputFile.clear();
dispMenu();

}
1982pc 2005-04-18
  • 打赏
  • 举报
回复
对不起啊.按你的代码得的结果确实是这样.
void addNewHotel(char *filename)
{
system("cls");
ofstream OutputFile;
ifstream InputFile;
vector<Hotel> Vh(1);
Hotel h;
int maxhotelcode=1;
int hotelcode;
InputFile.open(filename,ios::binary|ios::in);

if(!InputFile){

hotelcode=1;
}else
{
//int o=0;
InputFile.read((char*)&h,sizeof(h));

while(!InputFile.eof())
{
Vh.push_back(h);
cout<<maxhotelcode;
maxhotelcode++;
InputFile.read((char*)&h,sizeof(h));

}
InputFile.close();
InputFile.clear();


}


cout << " ADD NEW HOTEL DETAILS" << endl;
cout << "New Hotel Code is:"<<maxhotelcode<<endl;



OutputFile.open(filename,ios::binary|ios::out);


char reply='Y';



h.hotel_code=maxhotelcode;
maxhotelcode++;
cout << "Enter Hotel/Resort name:" << endl;
cin >>h.hotel_name;
cout << "Enter City name:" << endl;
cin >> h.hotel_city;
cout << "Enter the number of Regular rooms:" << endl;
cin >> h.available_regular_rooms;
cout << "Enter the tariff for Regular rooms:" << endl;
cin >> h.tariff_regular_room;
cout << "Enter the number of Deluxe rooms:" << endl;
cin >> h.available_deluxe_rooms;
cout << "Enter the tariff for Deluxe rooms:" << endl;
cin >> h.tariff_deluxe_room;

Vh.push_back(h);



for(int n=0;n<Vh.size();n++){
//cout<<Vh[n].hotel_code<<endl;
//cout<<Vh[n].hotel_name<<endl;
//cout<<Vh[n].hotel_city<<endl;
OutputFile.write((char*)&Vh[n],sizeof(Vh[n]));//这里当你第一次运行的时候其实只有一条纪录,可是你写如两次!!!为什么呢?那是因为你定义的时候Vh(1),已经有一纪录了,你再push_back就是二了,只需要把这里改成Vh!!!!!!!!
}


OutputFile.close();
OutputFile.clear();
dispMenu();

}
didoleo 2005-04-18
  • 打赏
  • 举报
回复
因为有新的记录从键盘上录入,push_back两次,第一次是push_bak的文件里的老的记录,第二次是push_back的刚录入的新的记录,新老记录重新覆盖文件,是这样的流程.两次push_back的不一样的内容.

下面是我写进hotel.dat的内容:我录入了3条记录,的确没重复,但读出来的时候Vh.size()不是3,比3大?!


烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫 aaaaa 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫bbbbb 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫 ? 烫烫 ? 烫烫 cccccc 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘢ddddd 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫 ? 烫烫 ? 烫烫 eeeeee 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘤fffff 烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫 ? 烫烫 ? 烫烫
1982pc 2005-04-18
  • 打赏
  • 举报
回复
Vh.size()代表当前vector的大小,虽然不太懂你说的,但你说的大小问题确实是由于你push_back两次造成的!
didoleo 2005-04-18
  • 打赏
  • 举报
回复
to: useresu(俗人) 的确Vh.push_back(h)两次
第一次:
if(!InputFile){

hotelcode=1;
}else
{
//int o=0;
InputFile.read((char*)&h,sizeof(h));

while(!InputFile.eof())
{
Vh.push_back(h);
cout<<maxhotelcode;
maxhotelcode++;
InputFile.read((char*)&h,sizeof(h));

}
InputFile.close();
InputFile.clear();


}
目的是从文件中读出记录数,push_back一次,我认为就是读了一条记录,这个概念不知道对不对,用maxhotelcode++来累计记录总数,目的要得到一个当前可用的最大hotel的编号.文件读完了,Vh里面因该是已有的记录.

第二次push_back是在这
h.hotel_code=maxhotelcode;
maxhotelcode++;
cout << "Enter Hotel/Resort name:" << endl;
cin >>h.hotel_name;
cout << "Enter City name:" << endl;
cin >> h.hotel_city;
cout << "Enter the number of Regular rooms:" << endl;
cin >> h.available_regular_rooms;
cout << "Enter the tariff for Regular rooms:" << endl;
cin >> h.tariff_regular_room;
cout << "Enter the number of Deluxe rooms:" << endl;
cin >> h.available_deluxe_rooms;
cout << "Enter the tariff for Deluxe rooms:" << endl;
cin >> h.tariff_deluxe_room;

Vh.push_back(h);//第二次

这是从键盘输入的新的记录被push_back进来,由于我是覆盖文件的,把整个Vh重新写回文件,覆盖掉文件中老的记录(老师说一定要覆盖掉,不要追加).
现在的问题是我每次得到的hotel的编号总是2,4,6,8.....这样下去的,好象就是说文件里的记录被写进了两次,我在循环里面用的Vh.size(),我不确定这个size()到底代表什么,他为什么不能准确反映Vh数组大小呢?

谢谢

owe 2005-04-18
  • 打赏
  • 举报
回复
借人气问一下,在类的属性里面定义Vector怎么定义啊?
比如用上面的为例:
///////////////////////////
#include <iostream>
#include <vector>
using namespace std;

class Hotel {
private:
vector<int> v(10); // 在这里出错!!
public:
//vector<int> v(10);
int hotel_code;
Hotel()
{
hotel_code = 100;
//v[0] = 20;

}
void print()
{
vector<int> Vh(1);
Vh[0] = 12;
cout<<endl<<"旅馆代码: "<<hotel_code << " Vector " <<Vh[0];
}
};

int main()
{
Hotel h;
h.print();
system("PAUSE");
return 0;
}
//////////////////
谢谢啊
useresu 2005-04-18
  • 打赏
  • 举报
回复
你push_back(h);两次的原因吧

void addNewHotel(char *filename)
{
system("cls");
ofstream OutputFile;
ifstream InputFile;
vector<Hotel> Vh(1);
Hotel h;
int maxhotelcode=1;
int hotelcode;
InputFile.open(filename,ios::binary|ios::in);

if(!InputFile){

hotelcode=1;
}else
{
//int o=0;
InputFile.read((char*)&h,sizeof(h));

while(!InputFile.eof())
{
Vh.push_back(h);//第一次
cout<<maxhotelcode;
maxhotelcode++;
InputFile.read((char*)&h,sizeof(h));

}
InputFile.close();
InputFile.clear();


}


cout << " ADD NEW HOTEL DETAILS" << endl;
cout << "New Hotel Code is:"<<maxhotelcode<<endl;



OutputFile.open(filename,ios::binary|ios::out);


char reply='Y';



h.hotel_code=maxhotelcode;
maxhotelcode++;
cout << "Enter Hotel/Resort name:" << endl;
cin >>h.hotel_name;
cout << "Enter City name:" << endl;
cin >> h.hotel_city;
cout << "Enter the number of Regular rooms:" << endl;
cin >> h.available_regular_rooms;
cout << "Enter the tariff for Regular rooms:" << endl;
cin >> h.tariff_regular_room;
cout << "Enter the number of Deluxe rooms:" << endl;
cin >> h.available_deluxe_rooms;
cout << "Enter the tariff for Deluxe rooms:" << endl;
cin >> h.tariff_deluxe_room;

Vh.push_back(h);//第二次



for(int n=0;n<Vh.size();n++){
//cout<<Vh[n].hotel_code<<endl;
//cout<<Vh[n].hotel_name<<endl;
//cout<<Vh[n].hotel_city<<endl;
OutputFile.write((char*)&Vh[n],sizeof(Vh[n]));
}


OutputFile.close();
OutputFile.clear();
dispMenu();

}
didoleo 2005-04-18
  • 打赏
  • 举报
回复
to: WuYL7812(龙哥)
1982pc()
zengwujun(月之海)

太谢谢你们了,现在对了!
下午借了本书看看,也知道了,呵呵.


1982pc 2005-04-18
  • 打赏
  • 举报
回复
楼主你说的是,可是你用push_back表示在队尾+如一个元素,这样vector就会自动增加1了!你只要把声明改成vector<Hotel> Vh;就可以了!
antter 2005-04-18
  • 打赏
  • 举报
回复
vector<Hotel> Vh(1);
你的vector初始不为空,size所以比原来多1,1+1=2 一次会存入两条.
displayMenu()递归调用,在极端可能下,比如操作了上万次,会发生堆栈溢出。
zengwujun 2005-04-18
  • 打赏
  • 举报
回复
vector<Hotel> Vh(1);这样是定义了一个只有能容纳一个元素的数组,但这个元素是空的.
vector的好处就是他会随着记录的增加自动扩大容量.我定义vector<Hotel> Vh(100)也可以
他只表明了他的容量,就象 int intArr[100],这个intArr里面没东西的,除非我给他们都赋值

------------------------------------------------------------------------------
vector<Hotel> Vh(100)表明要给Vh分配100个元素,元素内容是空的,如果capacity不够,
vector会自动增加已满足能分配100元素的空间,这时Vh.size()=100;capacity可能为100,
也可能>100.
didoleo 2005-04-17
  • 打赏
  • 举报
回复
to: defyer007(挑战者---在学MFC呢)

Vh.size()我的理解应该是有多少记录有应该是多少,比如文件里两条记录,那么读到Vh里面,
Vh.size()就应该是2吧,但实际上我跟踪了一写Vh.size()是4

晕倒
didoleo 2005-04-17
  • 打赏
  • 举报
回复
不好意思,我学c++才一个星期,这是作业题目,我写的不规范,不要笑我.
didoleo 2005-04-17
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>

#include <fstream>
#include <stdlib.h>

#include <vector>
#include <algorithm>
using namespace std;

void dispMenu();
char* filename="hotel.dat";
class Hotel {
public:
int hotel_code;
char hotel_name[50];
char hotel_city[50];

int total_regular_rooms;
double tariff_regular_room;
int available_regular_rooms;

int total_deluxe_rooms;
double tariff_deluxe_room;
int available_deluxe_rooms;

void print()
{
cout<<endl<<"旅馆代码: "<<hotel_code;
cout<<endl<<"旅馆名称: "<<hotel_name;
cout<<endl<<"旅馆所在城市: "<<hotel_city;

cout<<endl<<"总共标准房间: "<<total_regular_rooms;
cout<<endl<<"标准房间价格: "<<tariff_regular_room;
cout<<endl<<"可用标准房: "<<available_regular_rooms;

cout<<endl<<"总共豪华房间: "<<total_deluxe_rooms;
cout<<endl<<"豪华房间价格: "<<tariff_deluxe_room;
cout<<endl<<"可用豪华房: "<<available_deluxe_rooms;

}



};


int getMaxCode(char *filename)
{
int maxhotelcode=0;
ifstream InputFile;

vector<Hotel> Vh(1);
Hotel h;
InputFile.open(filename,ios::in);


while(!InputFile.eof())
{
InputFile.read((char*)&h,sizeof(h));
//cout<<h.hotel_city<<" "<<h.hotel_code<<" "<<h.hotel_name<<endl;
Vh.push_back(h);
}
InputFile.close();
InputFile.clear();



for(int n=0;n<Vh.size();n++)
{
//maxhotelcode++;
//Vh[n].hotel_code=maxhotelcode;
maxhotelcode=Vh[n].hotel_code;
}
return maxhotelcode+1;

};



void addNewHotel(char *filename)
{
system("cls");
ofstream OutputFile;
ifstream InputFile;
vector<Hotel> Vh(1);
Hotel h;
int maxhotelcode=1;
int hotelcode;
InputFile.open(filename,ios::binary|ios::in);

if(!InputFile){

hotelcode=1;
}else
{
//int o=0;
InputFile.read((char*)&h,sizeof(h));

while(!InputFile.eof())
{
Vh.push_back(h);
cout<<maxhotelcode;
maxhotelcode++;
InputFile.read((char*)&h,sizeof(h));

}
InputFile.close();
InputFile.clear();


}


cout << " ADD NEW HOTEL DETAILS" << endl;
cout << "New Hotel Code is:"<<maxhotelcode<<endl;



OutputFile.open(filename,ios::binary|ios::out);


char reply='Y';



h.hotel_code=maxhotelcode;
maxhotelcode++;
cout << "Enter Hotel/Resort name:" << endl;
cin >>h.hotel_name;
cout << "Enter City name:" << endl;
cin >> h.hotel_city;
cout << "Enter the number of Regular rooms:" << endl;
cin >> h.available_regular_rooms;
cout << "Enter the tariff for Regular rooms:" << endl;
cin >> h.tariff_regular_room;
cout << "Enter the number of Deluxe rooms:" << endl;
cin >> h.available_deluxe_rooms;
cout << "Enter the tariff for Deluxe rooms:" << endl;
cin >> h.tariff_deluxe_room;

Vh.push_back(h);



for(int n=0;n<Vh.size();n++){
//cout<<Vh[n].hotel_code<<endl;
//cout<<Vh[n].hotel_name<<endl;
//cout<<Vh[n].hotel_city<<endl;
OutputFile.write((char*)&Vh[n],sizeof(Vh[n]));
}


OutputFile.close();
OutputFile.clear();
dispMenu();

}

void addNewResort()
{

}

void recBooking()
{

}

void recCancel()
{

}

void viewDetail()
{

}

void dispMenu()
{
system("cls");
int menuitem;
cout << " HOTEL BOOK SYSTEM" << endl;
cout << "1. Add new Hotel details" << endl;
cout << "2. Add new Resort details" << endl;
cout << "3. Record Booking details" << endl;
cout << "4. Record Cancellation details" << endl;
cout << "5. View details or Hetels and Resorts in a city" << endl;
cout << "6. Exit" << endl;
cout << " Enter choice:";
cin >> menuitem;
switch(menuitem){
case 1: addNewHotel(filename);
break;
case 2: addNewResort();
break;
case 3: recBooking();
break;
case 4: recCancel();
break;
case 5: viewDetail();
break;
case 6: exit(0);
break;
default: dispMenu();
break;
};

}

void main()
{


dispMenu();

}
加载更多回复(4)

64,644

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧