05 Multilists
05 Multilists
05 Multilists
Fuente: https://realpython.com/python-iterators-iterables/
STL
std::vector
• Dynamic array, grows in
memory as needed.
3
Vector
Exercise
#include <vector>
#include <iostream>
int main() {
std::vector<int> arr;
1.for (int
Insert somei=0; i<6;
data ... i++)
arr.push_back(10-i);
for (int ind=0; ind<arr.size(); ind++)
std::cout
2. Print << arr[ind]
the inserted << screen
data on the " "; ...
std::cout << std::endl;
}
4
Vector
Exercise
#include <vector>
#include <iostream>
int main() {
std::vector<int> arr;
for (int i=0; i<6; i++)
arr.push_back(10-i);
for (int ind=0; ind<arr.size(); ind++)
std::cout << arr[ind] << " ";
std::cout << std::endl;
}
5
STL
std::deque
• Two-headed tail, dynamic
array growing at both ends.
6
Deque
Ejercicio
#include <iostream>
#include <deque>
int main() {
std::deque<int> dcola; Espacio para
for (int i=0; i<3; i++) { Imagen, icono o
3. Insert some data at both ends ... diagrama
dcola.push_back(i);
dcola.push_front(10-i);
}
for (int ind=0; ind<dcola.size(); ind++)
4. Print the inserted
std::cout data on the<<screen
<< dcola[ind] " "; ...
std::cout << std::endl;
}
7
Deque
Exercise
#include <iostream>
#include <deque>
int main() {
std::deque<int> dcola; Espacio para
for (int i=0; i<3; i++) { Imagen, icono o
diagrama
dcola.push_back(i);
dcola.push_front(10-i);
}
for (int ind=0; ind<dcola.size(); ind++)
std::cout << dcola[ind] << " ";
std::cout << std::endl;
}
8
STL
std::list
• Double chained list,
connection to next and
previous.
9
List
Exercise
#include <iostream>
#include <list>
int main() {
std::list<int> lista;
for (int i=0; i<3; i++) { Espacio para
lista.push_back(i); Imagen, icono o
5. Insert some data at both
lista.push_front(10-i); ends ... diagrama
}
std::list<int>::iterator it;
for (it=lista.begin(); it!=lista.end(); it++)
6. Print the inserted
std::cout << *it data on the screen ...
<< " ";
std::cout << std::endl;
}
10
List
Exercise
#include <iostream>
#include <list>
int main() {
std::list<int> lista;
for (int i=0; i<3; i++) { Espacio para
lista.push_back(i); Imagen, icono o
lista.push_front(10-i); diagrama
}
std::list<int>::iterator it;
for (it=lista.begin(); it!=lista.end(); it++)
std::cout << *it << " ";
std::cout << std::endl;
}
11
ON-SCREEN OUTPUT
Vector<T> Deque<T> List<T>
vector<int> seq; deque<int> seq; list<int> seq;
vector<int>::iterator it; deque<int>::iterator it; list<int>::iterator it;
for (it=seq.begin(); for (it=seq.begin(); for (it=seq.begin();
it!=seq.end(); it++) { it!=seq.end(); it++) { it!=seq.end(); it++) {
std::cout << *it << std::cout << *it << std::cout << *it <<
std::endl; std::endl; std::endl;
} } }
Espacio para
vector<int> seq; deque<int> seq; list<int>
Imagen, icono o seq;
vector<int>::reverse_iterator deque<int>::reverse_iterator list<int>::reverse_iterator
diagrama
it; it; it;
for (it=seq.rbegin(); for (it=seq.rbegin(); for (it=seq.rbegin();
it!=seq.rend(); it++) it!=seq.rend(); it++) it!=seq.rend(); it++)
{ { {
std::cout << *it << std::cout << *it << std::cout << *it <<
std::endl; std::endl; std::endl;
} } }
12
Multilists
Multilists
Dynamic matrix representation
int **p_mat;
p_mat = new int* [size1];
for (int i=0; i<size1; i++)
*(p_mat+i) = new int [size2];
14
Multilists
Dynamic matrix representation
size1
p_mat
15
Multilists
Dynamic matrix representation
size2
*(p_mat+0)
*(p_mat+1)
size1
*(p_mat+2)
*(p_mat+3)
*(p_mat+4)
p_mat *(p_mat+5)
16
Multilists
Matrix representation as a set of lists
17
Multilists
Matrix representation as a set of lists
head
18
Multilists
Matrix representation as a set of lists
head
19
Multilists
Matrix representation as a set of lists
Multilist: sequence of sequences
Advantage: They do not all have to be the same size.
head
20
Multilists
Implementation
List of integers:
std::list<int> integers;
head
4 8 13
22
Multilists
Implementación → List of lists.
23
Multilists
Exercise
• Consider the implemented TAD Vehicle
class Vehicle {
protected:
unsigned short model;
string regPlate;
unsigned int dailyRate;
public:
unsigned short getModel();
string getRegPlate();
unsigned int getDailyRate();
...
};
24
Multilists
Exercise
25
Multilists
Exercise
26
Multilists
Ejercicio
• Assuming that the parking lots are identified by a
consecutive integer, how would a vehicle be inserted into a
given parking lot?
27
Multilists
Ejercicio
• Assuming that the parking lots are identified by a
consecutive integer, how would a vehicle be inserted into a
given parking lot?
void InsertVeh( Vehicle nVeh, int idParq )
{
std::list< std::list<Vehicle> >::iterator itP;
itP = entity.begin();
for (int i = 1; i <= idParq; i++)
itP++;
itP->push_back(nVeh);
}
28
Multilists
Exercise
• How would you calculate the total number of vehicles in the
entity?
29
Multilists
Exercise
• How would you calculate the total number of vehicles in the
entity?
unsigned int totalVehs( )
{
unsigned int total = 0;
std::list< std::list<Vehicle> >::iterator itP;
itP = entity.begin();
for ( ; itP != entity.end(); itP++)
total += itP->size();
return total;
}
30
Multilists
Exercise
• How would the total daily value received by the entity for parked
vehicles be calculated?
unsigned int totalDaily( )
{
unsigned int total = 0;
std::list< std::list<Vehicle> >::iterator itP;
std::list<Vehicle>::iterator itV;
itP = entity.begin();
for ( ; itP != entity.end(); itP++) {
itV = itP->begin();
for ( ; itV != itP->end(); itV++)
total += itV-> getDailyRate();
}
return total;
} 31
Thank you