一个c++ stl相关的问题 ,可以说很简单,可以说很难
在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也贴出来
相关推荐
C++ STL 参考手册Cpp_STL_ReferenceManual.pdf
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
浅谈c++ stl迭代器失效的问题
之前看《C++ Primier》的时候,也解到在顺序型窗口里insert/erase会涉及到迭代器失效的问题,并没有深究。今天写程序的时候遇到了这个问题。 1 莫名其妙的Erase 最初我的程序是酱紫的,别说话,我知道这样是有问题的,可这样是最直观的想法 int arr[]={0,1,2,3,4,5,6,7,8,9,10}; vector<int> a(arr,arr+sizeof(arr)/sizeof(*arr));for (auto it = a.begin(); it != a.end();++it ){ if ((*it)&1){ a.erase(it);
33个c++重要知识总结(cpp代码)
c++基础知识复习,包含了33个cpp文件,每个文件包含特定主题的知识回顾,使用特定案例重温某些不常用但是重要的算法或函数的使用方法,含注释。
c / c++ / cpp / stl 中文帮助文档手册chm格式下载
c / c++ / cpp / stl 中文帮助文档手册chm格式下载 C/C++ 语言参考 基本C/C++ 预处理命令 操作符优先级 转义字符 ASCII码表 基本数据类型 关键字 标准 C 库: Standard C I/O Standard C String & Character Standard C Math Standard C Time & Date Standard C Memory Other standard C functions C++ C++ I/O C++ Strings C++ 标准模板库 C++ Bitsets C++ Double-Ended Queues C++ Lists C++ Maps C++ Multimaps C++ Multisets C++ Priority Queues C++ Queues C++ Sets C++ Stacks C++ Vectors Iterators 全部的 C 函数 全部的 C++ 函数
简单说说STL的内存管理
将其描述为空间配置器,理由是allocator可以将其它存储介质(例如硬盘)做为stl 容器的存储空间。由于内存是allocator管理的主要部分,因此,本文以STL内存管理为出发点介绍allocator


