一个c++ stl相关的问题 ,可以说很简单,可以说很难

molinus 2006-05-15 12:35:01
这个题目是卡耐基 梅农大学的ssd 课程的一个题目
在main.cpp里面
有三个函数是的申明已经给出 ,代码实现要自己写
这个三个函数是 find_car , handle_arrival, handle_departure
下面是 main.cpp的 代码
代码有点长,真的不好意思,但是为了把问题说清楚,我只能把全部代码贴出来
#pragma warning(disable:4786)

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <stack>
#include <stdexcept>
#include <vector>
#include <algorithm>

#include "car.h"

using namespace std;

const int PARKING_SPOTS_PER_AISLE = 3;
const int NUMBER_OF_AISLES = 5;

void handle_arrival(vector<Car>&, vector<stack<string> >&, const string&);
void handle_departure(vector<Car>&, vector<stack<string> >&, const string&);
Car& find_car(vector<Car>&, string);

int main(int argc, char* argv[]) {

try {

if (argc != 2) {
cerr << "Usage:\n" << argv[0] << " data-file";
return EXIT_FAILURE;
}

ifstream inf(argv[1]);
if (! inf) {
cerr << "Could not open " << argv[1];
return EXIT_FAILURE;
}

vector<Car> cars;
vector< stack<string> > parking_lot(NUMBER_OF_AISLES);

while (! inf.eof()) {

string action, plate;
inf >> plate >> action;

if (action == "arrives") {
handle_arrival(cars, parking_lot, plate);
}
else if (action == "departs") {
handle_departure(cars, parking_lot, plate);
} else {
cerr << "Unknown action: " << action << endl;
}

}
inf.close();

cout << "\nHere are all the cars that visited the lot today:\n";
ostream_iterator<string> output(cout,"");
sort(cars.begin(),cars.end(),cars);
copy(cars.begin(),cars.end(),output);


return EXIT_SUCCESS;

}
catch (exception& e) {
cerr << e.what() << endl;
}
catch (...) {
cerr << "Unknown exception caught!" << endl;
}

return EXIT_FAILURE;
}

void handle_arrival(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {

for(int i=0;i<parking_lot.size();i++){
stack<string> s=parking_lot[i];
if(s.size()!=3){
parking_lot[i].push(plate);
Car c(plate,i+1);
cars.push_back(c);
cout<<plate<<"park";
return ;
}
}
cerr<<" all the parking_lot is full";

}

void handle_departure(vector<Car>& cars, vector< stack<string> >& parking_lot, const string& plate) {

Car departCar=find_car(cars,plate);
int i=departCar.getAisle();
stack<string> temp;
stack<string> leave =parking_lot[i-1];
string license=leave.top();
leave.pop();
while(license!=plate){
temp.push(license);
license=leave.top();
leave.pop();
}
while(temp.size()>0){
string l=temp.top();
temp.pop();
leave.push(l);
}
cout<<plate<<" departure";
// cout<<
}

Car &find_car(vector<Car>& cars, string plate) {
Car n(plate);
Car *p=&n;
Car *r;
*r=find(cars.begin(),cars.end(),p);
return *r;
}
另外car.cpp和car.h 是题目给的代码,不能改动的。
现在编译出现这个错误
C:\Documents and Settings\pineapple\桌面\main.cpp(118) : error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class Car *' (or there is no acceptable conversion)
而且在find-car 函数里面 ,各种各样的方法我都试验过
下面回帖的人 如果不能确定是100% 正确的,不要随便说
因为这个问题 我已经搞了 n天鸟
希望 是能够可以运行的程序
下面把car.h 和car.cpp也贴出来
...全文
366 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
jesse8013 2006-05-25
  • 打赏
  • 举报
回复
你前面的程序有问题
至于那个find_car
Car &find_car(vector<Car>& cars, string plate) {
Car n(plate);
Car *r;
*r=find(cars.begin(),cars.end(),p);
return *r;
}
就可以前提是你前面的要正确.
如果有问题 ,继续和我联系
molinus 2006-05-25
  • 打赏
  • 举报
回复
楼上的方法换汤不换药
molinus 2006-05-15
  • 打赏
  • 举报
回复
下面是题目的说明
其实贴出拉也没什么用Tasks
To complete this assessment, you will need to finish the implementation of the parking-lot simulation.

To begin, verify the files needed for this assessment.

Extract the archive to retrieve the files needed to complete this assessment.
Following is an ordered list of steps that serves as a guide to completing this assessment. Work and test incrementally. Save often

First, finish the implementation of function find_car. This function returns a reference to the Car object stored in the vector cars whose license plate equals the parameter plate. Use the STL find function to perform this task. To use this function correctly, you must supply it with three arguments. The first two arguments specify a range to search. The third argument is the value that the function attempts to find. This argument must be of type Car.
Next, finish the implementation of function handle_arrival. This function should iterate through the vector of stacks, looking for the first stack that does not contain three cars. If all five aisles (stacks) are full, output a message indicating such; otherwise place the license plate into the first non-full stack. This is essentially "parking" the car. For this arriving car, also add an entry of type Car to the vector cars. In this Car instance, make sure to record properly the index of the aisle where this car is parked.
Then, finish the implementation of function handle_departure. This function should locate the departing vehicle from the cars vector using function find_car. Then this function should remove the departing car's license plate from the appropriate aisle. Another stack must be used to move, temporarily, any cars that may be in front of the departing car. Record the number of times a car is moved when accommodating the departure of another car. For the departing car, display the number of times it was moved while it was parked in the lot.
molinus 2006-05-15
  • 打赏
  • 举报
回复
car.h
#include <iostream>
#include <string>
#ifndef _CAR_H_
#define _CAR_H_

using namespace std;

class Car {

private:
int moved;
string license_plate;
int aisle;

public:
Car(string);
Car(string, int);

int getTimesMoved(void) const;
void setTimesMoved(int);
string getPlate(void) const;
int getAisle(void) const;

bool operator < (const Car& rhs) const;
bool operator == (const Car& rhs) const;
};

#endif

car.cpp

#include "car.h"

Car::Car(string plate) :
license_plate(plate),
moved(0), aisle(-1) {}

Car::Car(string plate, int aisle) :
license_plate(plate),
moved(0), aisle(aisle) {}

int Car::getTimesMoved(void) const {

return moved;
}

void Car::setTimesMoved(int m) {

moved = m;
}

int Car::getAisle(void) const {

return aisle;
}

string Car::getPlate(void) const {

return license_plate;
}

bool Car::operator < (const Car& rhs) const {

return this->getPlate() < rhs.getPlate();
}

bool Car::operator == (const Car& rhs) const {

return this->getPlate() == rhs.getPlate();
}
roger_77 2006-05-15
  • 打赏
  • 举报
回复
偶看完了。

不知能否

Car &find_car(vector<Car>& cars, string plate) {
Car n(plate);
// Car *p=&n;
// Car *r;
vector<Car>::iterator itr=NULL;
itr = find(cars.begin(),cars.end(),n);

return *itr;
}
delphihero 2006-05-15
  • 打赏
  • 举报
回复
Car *r;
*r=find() Car 类没有定义operator = 操作符,即使定义了 *r 只是一个指针也没用空间去存放
xuch318 2006-05-15
  • 打赏
  • 举报
回复
晕,没看完就说了...
xuch318 2006-05-15
  • 打赏
  • 举报
回复
楼主要把程序补齐还是讨论题目的??
molinus 2006-05-15
  • 打赏
  • 举报
回复
up
aronzhou 2006-05-15
  • 打赏
  • 举报
回复
mark
当读者有一定c/c++基础 推荐的阅读顺序: level 1 从<>开始,短小精悍,可以对c++能进一步了解其特性 以<>作字典和课外读物,因为太厚不可能一口气看完 level 2 然后从<>开始转职,这是圣经,请遵守10诫,要经常看,没事就拿来翻翻 接着是<>,个人认为Herb Sutter主席大人的语言表达能力不及Scott Meyers总是在教育第一线的好 顺下来就是<>和<>,请熟读并牢记各条款 当你读到这里,应该会有一股升级的冲动了 level 3 <>看过后如一缕清风扫去一直以来你对语言的疑惑,你终于能明白compiler到底都背着你做了些什么了,这本书要细细回味,比较啃,最好反复看几遍,加深印象 看完上一本之后,这本<>会重演一次当年C++他爹在设计整个语言过程中的历程 level 4 <>是stl的字典,要什么都可以查得到 学c++不能不学stl,那么首先是<>,它和圣经一样是你日常行为的规范 <>让你从oo向gp转变 光用不行,我们还有必要了解stl的工作原理,那么<>会解决你所有的困惑 level 5 对于c++无非是oo和gp,想进一步提升oo,<>是一本主席这么多年的经验之谈,是很长esp的 一位stl高手是不能不去了解template的,<>是一本百科全书,足够你看完后对于gp游刃有余 <>是太过聪明的人写给明眼人看的 好书有很多,不能一一列举 以上我的读书经历,供各位参考。接下来的无非就是打怪练级,多听多写多看;boost、stl、loki这些都是利器,斩妖除魔,奉劝各位别再土法练钢了。 at last,无他,唯手熟尔。 忘了一本《thinking in C++》 也是经典系列之一 <>这本圣经的作者Scott Meyesr在给<>序言的时候高度的赞赏了Andrei同志的工作:C++社群对template的理解即将经历一次巨大的变化,我对它所的任何事情,也许很快就会被认为是陈旧的、肤浅的、甚至是完全错的。 就我所知,template的世界还在变化,速度之快就像我1995年回避写它的时候一样。从发展的速度来看,我可能永远不会写有关template的技术书籍。幸运的是一些人比我勇敢,Andrei就是这样一位先锋。我想你会从此书得到很多收获。我自己就得到了很多——Scott Meyers September2000。 并且,Scott Meyers 在最近的Top5系列文章中,评价C++历史里面最重要5本书中、把Modern C++ Design列入其中,另外四本是它自己的effective c++、以及C++ Programming Language、甚至包括《设计模式》和《C++标准文档》。 显然,Scott Meyers已经作为一个顶尖大师的角度承认了<>的价值。 并且调侃地,可以把是否使用其中模板方法定义为,现代C++使用者和非现代C++使用者,并且检讨了自己在早期版本Effective对模板的忽视,最后重申在新版本Effective第七章节加入大量对模板程序设计的段落,作为对这次失误的补偿。 并且,在这里要明确的是<>并不是一本泛型编成的书,也不是一本模板手册。其中提出了基于策略的设计方法,有计划和目的的使用了模板、面向对象和设计模式。虽然Andrei本人对模板的研究世界无人能敌,但对其他领域的作为也令人赞叹。 任何做游戏的人都不能忽视OpenAL把,你在开发者的名单里能看到Loki的名字:) 最近很忙,无时间写文章,小奉献一下书籍下载地址。虽然经过验证,但是不感肯定各位一定能下: 中文 http://www.itepub.net/html/ebookcn/2006/0523/40146.html 英文 http://dl.njfiw.gov.cn/books/C/Essential%20C
当读者有一定c/c++基础 推荐的阅读顺序: level 1 从<>开始,短小精悍,可以对c++能进一步了解其特性 以<>作字典和课外读物,因为太厚不可能一口气看完 level 2 然后从<>开始转职,这是圣经,请遵守10诫,要经常看,没事就拿来翻翻 接着是<>,个人认为Herb Sutter主席大人的语言表达能力不及Scott Meyers总是在教育第一线的好 顺下来就是<>和<>,请熟读并牢记各条款 当你读到这里,应该会有一股升级的冲动了 level 3 <>看过后如一缕清风扫去一直以来你对语言的疑惑,你终于能明白compiler到底都背着你做了些什么了,这本书要细细回味,比较啃,最好反复看几遍,加深印象 看完上一本之后,这本<>会重演一次当年C++他爹在设计整个语言过程中的历程 level 4 <>是stl的字典,要什么都可以查得到 学c++不能不学stl,那么首先是<>,它和圣经一样是你日常行为的规范 <>让你从oo向gp转变 光用不行,我们还有必要了解stl的工作原理,那么<>会解决你所有的困惑 level 5 对于c++无非是oo和gp,想进一步提升oo,<>是一本主席这么多年的经验之谈,是很长esp的 一位stl高手是不能不去了解template的,<>是一本百科全书,足够你看完后对于gp游刃有余 <>是太过聪明的人写给明眼人看的 好书有很多,不能一一列举 以上我的读书经历,供各位参考。接下来的无非就是打怪练级,多听多写多看;boost、stl、loki这些都是利器,斩妖除魔,奉劝各位别再土法练钢了。 at last,无他,唯手熟尔。 忘了一本《thinking in C++》 也是经典系列之一 <>这本圣经的作者Scott Meyesr在给<>序言的时候高度的赞赏了Andrei同志的工作:C++社群对template的理解即将经历一次巨大的变化,我对它所的任何事情,也许很快就会被认为是陈旧的、肤浅的、甚至是完全错的。 就我所知,template的世界还在变化,速度之快就像我1995年回避写它的时候一样。从发展的速度来看,我可能永远不会写有关template的技术书籍。幸运的是一些人比我勇敢,Andrei就是这样一位先锋。我想你会从此书得到很多收获。我自己就得到了很多——Scott Meyers September2000。 并且,Scott Meyers 在最近的Top5系列文章中,评价C++历史里面最重要5本书中、把Modern C++ Design列入其中,另外四本是它自己的effective c++、以及C++ Programming Language、甚至包括《设计模式》和《C++标准文档》。 显然,Scott Meyers已经作为一个顶尖大师的角度承认了<>的价值。 并且调侃地,可以把是否使用其中模板方法定义为,现代C++使用者和非现代C++使用者,并且检讨了自己在早期版本Effective对模板的忽视,最后重申在新版本Effective第七章节加入大量对模板程序设计的段落,作为对这次失误的补偿。 并且,在这里要明确的是<>并不是一本泛型编成的书,也不是一本模板手册。其中提出了基于策略的设计方法,有计划和目的的使用了模板、面向对象和设计模式。虽然Andrei本人对模板的研究世界无人能敌,但对其他领域的作为也令人赞叹。 任何做游戏的人都不能忽视OpenAL把,你在开发者的名单里能看到Loki的名字:) 最近很忙,无时间写文章,小奉献一下书籍下载地址。虽然经过验证,但是不感肯定各位一定能下: 中文 http://www.itepub.net/html/ebookcn/2006/0523/40146.html 英文 http://dl.njfiw.gov.cn/books/C/Essential%20C

65,186

社区成员

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

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