Sunteți pe pagina 1din 9

Ministerul Educa iei al Republicii Moldova

Universitatea Tehnic a Moldovei


Departamentul Inginerie Software i Automatic

Tema: STL n C++

RAPORT
Lucrare de laborator nr. 1
La Analiza i proiectarea programelor orientate pe obiecte

A efectuat:
st. gr. SI-141
E. Cucu

A verificat:
lect. superior
M. Balan

Chi inu 2017


Scopul lucrrii

nsuirea tehnologiilor de programare cu utilizarea STL n C++.

Obiectiv

Scrierea a trei programe folosind STL. Primul i al doilea program trebuie s


demonstreze lucrul cu containere STL, a treia utilizarea algoritmilor STL.

Mersul lucrrii:

Pentru nceput am definit clasa utilizator, cu care voi lucra n viitor.


Am ales clasa maina, care are ca cimpuri a clasei modelul i marca, n care am definit
constructorii, setter-rii, getter-rii, am suprancrcar operatorii de ntrare-ieire, i operatorii
de comparare pentru sortarea ulterioar.
class Car {
private:
string mark;
string model;
public:
Car();
Car(const string &mark, const string &model);
bool operator<(const Car &rhs) const;
bool operator>(const Car &rhs) const;
bool operator==(const Car &rhs) const;
bool operator!=(const Car &rhs) const;
const string &getMark() const;
const string &getModel() const;
void setModel(const string &model);
void setMark(const string &mark);
friend ostream &operator<<(ostream &os, const Car &car);
friend istream &operator>>(istream &is, Car &car);
};
Descrierea funcionala a clasei poate fi gasit la ANEXA B.
Acum putem utiliza clasa data n containerele STL conform variantei propuse.

8 multimap deque float

Am iniializat dou multimap-uri i 2 deque, la fel i 3 liste.


Pentru printarea elemenetelor containerilor ne folosim de iteratori.
for (auto item : firstMap) {
cout << "Category: " << item.second << " " << item.first;
}

2
Pentru cutarea dup o condiie am folosit un algoritm STL numit find_if(), care
primete nceputul containerului, sfritul, i un predicat dup care se va efectua cutarea.
if(find_if(secondMap.begin(), secondMap.end(), [](pair<Car, int> a) {
return a.first.getMark() == "Mercedes"; }) != secondMap.end()) {
cout << "Found" << endl;
};

n cazul dat predicatul este [](pair<Car, int> a) { return a.first.getMark() == "Mercedes";


}) care va fi folosit la fiecare iterare a algoritmului, i va returna TRUE n cazul n care se
va satisface condiia predicatului.

Pentru calcularea numarului de elemente care satisfac o condiie am folosit agorimul


count_if().
auto number = count_if(thirdList.begin(), thirdList.end(), [](Car car){ return car.getMark()
== "Mercedes";});

3
Concluzii

Efectund lucrarea de laborator am studiat funciile bibliotecii STL din C++.


Este un instrument puternic, specific limbajului, care simplific considerabil procesul
de programare.
Utilizarea containerilor permite stocarea obiectelor i prelucrarea lor specific, iar
utilizarea algoritmilor ne permite uor s operm i s colaborm cu orice tip de
containere.

4
Bibliografie:

1 Containere C++ [surs electronic]


http://www.cplusplus.com/reference/stl/
2 Note de laborator: Indicaii metodice

5
ANEXA A

Car.h
class Car {
private:
string mark;
string model;
public:
Car();
Car(const string &mark, const string &model);
bool operator<(const Car &rhs) const;
bool operator>(const Car &rhs) const;
bool operator==(const Car &rhs) const;
bool operator!=(const Car &rhs) const;
const string &getMark() const;
const string &getModel() const;
void setModel(const string &model);
void setMark(const string &mark);
friend ostream &operator<<(ostream &os, const Car &car);
friend istream &operator>>(istream &is, Car &car);
};

ANEXA B

Car.cpp
Car::Car(const string &mark, const string &model) : mark(mark), model(model) {}
const string &Car::getMark() const {
return mark;
}
void Car::setMark(const string &mark) {
Car::mark = mark;
}
const string &Car::getModel() const {
return model;
}
void Car::setModel(const string &model) {
Car::model = model;
}
ostream &operator<<(ostream &os, const Car &car) {
os << "mark: " << car.getMark() << " model: " << car.getModel() << endl;
return os;
}
istream &operator>>(istream &in, Car &car) {
cout << "Input mark of the car : " << endl;
string input;
cin >> input;
car.setMark(input);
cout << "Input model of the car : " << endl;
cin >> input;
car.setModel(input);
return in;
}
bool Car::operator==(const Car &rhs) const {
return mark.compare(rhs.mark) == 0;
}
bool Car::operator!=(const Car &rhs) const {
return mark.compare(rhs.mark) != 0;
}
bool Car::operator<(const Car &rhs) const {
return mark.compare(rhs.mark) > 0;
}
bool Car::operator>(const Car &rhs) const {
return mark.compare(rhs.mark) < 0;
}
Car::Car() {
}

6
ANEXA C

main.cpp
#include <iostream>
#include <map>
#include <deque>
#include <list>
#include <algorithm>
#include "Car.h"
int main() {
deque<Car> firstDeque;
deque<Car> secondDeque;
multimap<Car, int> firstMap;
multimap<Car, int> secondMap;
list<Car> firstList;
list<Car> secondList;
list<Car> thirdList;
firstMap.insert(pair<Car, int>(Car("Audi", "A1"), 1));
firstMap.insert(pair<Car, int>(Car("Audi", "A3"), 1));
firstMap.insert(pair<Car, int>(Car("Audi", "A5"), 1));
firstMap.insert(pair<Car, int>(Car("Audi", "A7"), 1));
firstMap.insert(pair<Car, int>(Car("Mercedes", "A-Class"), 2));
firstMap.insert(pair<Car, int>(Car("Mercedes", "C-Class"), 2));
firstMap.insert(pair<Car, int>(Car("Mercedes", "E-Class"), 2));
firstMap.insert(pair<Car, int>(Car("Mercedes", "S-Class"), 2));
firstMap.insert(pair<Car, int>(Car("BMW", "3-series"), 3));
firstMap.insert(pair<Car, int>(Car("BMW", "5-series"), 3));
firstMap.insert(pair<Car, int>(Car("BMW", "7-series"), 3));
secondMap.insert(pair<Car, int>(Car("Audi", "A1"), 1));
secondMap.insert(pair<Car, int>(Car("Audi", "A3"), 1));
secondMap.insert(pair<Car, int>(Car("Audi", "A5"), 1));
secondMap.insert(pair<Car, int>(Car("Audi", "A7"), 1));
secondMap.insert(pair<Car, int>(Car("Mercedes", "A-Class"), 2));
secondMap.insert(pair<Car, int>(Car("Mercedes", "C-Class"), 2));
secondMap.insert(pair<Car, int>(Car("Mercedes", "E-Class"), 2));
secondMap.insert(pair<Car, int>(Car("Mercedes", "S-Class"), 2));
secondMap.insert(pair<Car, int>(Car("BMW", "3-series"), 3));
secondMap.insert(pair<Car, int>(Car("BMW", "5-series"), 3));
secondMap.insert(pair<Car, int>(Car("BMW", "7-series"), 3));
firstDeque.push_back(Car("VW", "Passat"));
firstDeque.push_back(Car("VW", "Golf"));
firstDeque.push_back(Car("VW", "Polo"));
firstDeque.push_back(Car("Skoda", "Octavia"));
firstDeque.push_back(Car("Skoda", "Superb"));
firstDeque.push_back(Car("Skoda", "Fabia"));
cout << "Elements of firstMap : " << endl;
for (auto item : firstMap) {
cout << "Category: " << item.second << " " << item.first;
}
string condition;
cout << endl << "Delete cars with model : ";
cin >> condition;
for (auto it = firstMap.begin(); it != firstMap.end(); ) {
if (it -> first.getModel() == condition)
it = firstMap.erase(it);
else
++it;
}
cout << endl << "Elements of multimap after deleting by condition: " << endl;
for (auto item : firstMap) {
cout << "Category: " << item.second << " " << item.first;
}
cout << "Edit a car from firstMap : " << endl;
string mrk, mdl;
cout << "Enter Mark : ";

7
cin >> mrk;
cout << "Enter Model : ";
cin >> mdl;
auto it = firstMap.find(Car(mrk, mdl));
multimap<Car, int> aux;
if (it -> first.getMark() != "" && it -> first.getModel() != "" ) {
cout << "Input the new mark : ";
cin >> mrk;
cout << "Input the new model : ";
cin >> mdl;
aux.insert(pair<Car,int>(Car(mrk,mdl), it -> second));
if (it != firstMap.end()) {
firstMap.erase(it);
firstMap.insert(pair<Car, int> (Car(mrk, mdl), it -> second));
}
}
cout << endl << "Elements of multimap after edit : " << endl;
for (auto item : firstMap) {
cout << "Category: " << item.second << " " << item.first;
}
cout << endl << "Show firstDeque" << endl;
for (auto item : firstDeque) {
cout << item;
}
string mark, secondMark, model, secondModel;
cout << endl << "Deleting the elements from the map. \n"
"Enter the mark of the car to start: ";
cin >> mark;
cout << "Enter the model of the car to start: ";
cin >> model;
cout << "Enter the mark of the car to stop: ";
cin >> secondMark;
cout << "Enter the model of the car to stop: ";
cin >> secondModel;
auto it1 = firstMap.find(Car(mark, model));
auto it2 = firstMap.find(Car(secondMark, secondModel));
while (it1 != it2){
firstMap.erase(it1++);
}
cout << endl << "Inserting from MAP to DEQUE all the elements after deleting" << endl;
firstMap.erase(it2);
for (auto item : firstMap) {
firstDeque.push_back(item.first);
}
cout << endl << "FirstMap after modifications\n";
for (auto item : firstMap) {
cout << "Category: " << item.second << " " << item.first;
}
cout << endl << "Show firstDeque after modifications" << endl;
for (auto item : firstDeque) {
cout << item;
}
cout << endl << endl << "Elements of secondMap : " << endl;
for (auto item : firstMap) {
cout << "Category: " << item.second << " " << item.first;
}
cout << endl << "Find if secondMap contains elements with mark Mercedes" << endl;
if(find_if(secondMap.begin(), secondMap.end(), [](pair<Car, int> a) {
return a.first.getMark() == "Mercedes"; }) != secondMap.end()) {
cout << "Found" << endl;
};
for_each(secondMap.begin(), secondMap.end(),
[&secondDeque] (pair<Car, int> it ) {
if (it.first.getMark() == "Mercedes") {
secondDeque.push_back(it.first);
}
}
);
cout << endl << "Copy all elements from secondMap with mark Mercedes to secondDeque" <<
endl;

8
for (auto item : secondDeque) {
cout << item;
}
for_each(secondMap.begin(), secondMap.end(),
[&firstList] (pair<Car, int> it ) {
firstList.push_back(it.first);
}
);
for_each(secondDeque.begin(), secondDeque.end(),
[&secondList] (Car it ) {
secondList.push_back(it);
}
);
firstList.sort([](Car a, Car b) {
return a < b;
});
secondList.sort([](Car a, Car b) {
return a < b;
});
cout << endl << "First List Sorted : " << endl;
for(auto item : firstList) {
cout << item;
}
cout << endl << "Second List Sorted : " << endl;
for(auto item : secondList) {
cout << item;
}
thirdList.merge(firstList);
thirdList.merge(secondList);
cout << endl << "Third List : " << endl;
for(auto item : thirdList) {
cout << item;
}
auto number = count_if(thirdList.begin(), thirdList.end(), [](Car car){ return
car.getMark() == "Mercedes";});
if(number > 0) {
cout << "Found in merged list " << number << " elements with mark Mercedes" << endl;
} else {
cout << "0 elements found in merged list" << endl;
}
return 0;
}