1. В чем ошибки? Нерабочие участки программы закомментированы.
2. Как реализовать по убыванию с помощью итератора? Что за итератор возвращает обратное значение
3. Как присвоить значение итератору? У меня что то = не работает.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#define n 5
using namespace std;
///////////////////////////////////////
class liquido{
protected:
string name;
double ro;
public:
liquido(){
name="acqua";
ro=0;
}
liquido(string str){
name=str;
ro=0;
}
liquido(string str, double p){
name=str;
ro=p;
}
void print(){
cout << "Liquido " << name << endl;;
cout << "R\'o " << ro << endl;;
}
void set_ro(double p){
ro=p;
}
void set_name(string str){
name=str;
}
};
/////////////////////////////////////////////
class alcohol:public liquido{
float carico;
public:
alcohol(){
name="acqua";
ro=0;
carico=0;
}
alcohol(string str){
name=str;
ro=0;
}
alcohol(string str, double p){
name=str;
ro=p;
carico=0;
}
alcohol(string str, double p, float c){
name=str;
ro=p;
carico=c;
}
void set_c(float c){
carico=c;
}
void print(){
cout << "\nLiquido " << name << endl;;
cout << "R\'o " << ro << "; Carico " << carico << endl;
}
friend bool operator==(const alcohol& a1, const alcohol& a2){
return (a1.name==a2.name);
}
friend bool operator<(const alcohol& a1, const alcohol& a2){
return (a1.name<a2.name);
}
};
//////////////////////////////////////////////
////Function objects//////////////////////////
//////////////////////////////////////////////
class compareAlcohol{
public:
bool operator()(const alcohol* a1, alcohol* a2){
return *a1<*a2;
}
};
//////////////////////////////////////////////
//class printAlcohol{
//public:
// void operator() (const alcohol* a){
// *a->print();
// }
//};
/////////////////////////////////////////////
/////////MAIN////////////////////////////////
/////////////////////////////////////////////
int main(){
alcohol Martini("Martini Bianco 1863", 986.5, 18.5f);
alcohol Martini2("Martini Bianco 1863", 986.5, 18.5f);
Martini.print();
Martini.set_c(19.5f);
Martini.set_ro(987.);
Martini.print();
////////////////////////////////
alcohol* temp;
string gen;
vector<alcohol*> MyVector;
vector<alcohol*>::iterator MyIterator;
for (int i=0; i<n; i++){
gen= "Alcohol"+i;
cout << gen << endl;
temp = new alcohol(gen, i+10, i);
MyVector.push_back(temp);
}
cout << MyVector.size()<<endl;
// for_each(MyVector.begin(), MyVector.end(), printAlcohol);//output
// sort(MyVector.begin(), MyVector.end(), compareAlcohol);
/////////////////
///////////////
//////////ADD ELEMENT///////////
////////////////////////////////
temp = new alcohol("NEW ALCOHOL", 893.7, 39.4f);
// MyIterator=3;
// MyVector.insert(temp,MyIterator);
// for_each(MyVector.begin(), MyVector.end(), printAlcohol);//output
////////////////////////////////
////////DELETE ELEMENT//////////
////////////////////////////////
cout << "Which element delete?";
cin >> i;
delete MyVector[i];
// MyIterator = i;
// MyVector.erase(MyIterator,MyIterator);
// for_each(MyVector.begin(), MyVector.end(), printAlcohol);//output
////FIND ELEMENT/////
string sname;
cout << "Put Name for search";
cin >> sname;
// MyIterator=find(MyVector.begin(), MyVector.end(), sname);
// cout << MyIterator;
////////////CLEAR///////////////////////////////
while (!MyVector.empty()){
delete MyVector.back();
MyVector.pop_back();
}
return 0;
}