• No results found

The “vector” class

N/A
N/A
Protected

Academic year: 2022

Share "The “vector” class"

Copied!
47
0
0

Loading.... (view fulltext now)

Full text

(1)

IIT Bombay

Computer Programming

Dr. Deepak B Phatak Dr. Supratik Chakraborty

Department of Computer Science and Engineering IIT Bombay

Session : Template Class “list”

(2)

IIT Bombay

• Template classes and functions

• C++ Standard Library

The “string” class

The “vector” class

The “map” class

Quick Recap of Relevant Topics

(3)

IIT Bombay

• The template class “list”

Overview of This Lecture

(4)

IIT Bombay

Doubly Linked List

Convenient way to represent dynamically created sequence of objects/data items

Memory allocated for consecutive objects in list not necessarily contiguous, may not even be allocated at same time

Data Item

1

Data Item

2

Data Item

3

Data Item

4

Front of list Back of list

Forward link

Backward link

?

?

(5)

IIT Bombay

Deletion in Doubly Linked List

Efficient (constant time) deletion of objects

Data Item

1

Data Item

2

Data Item

3

Data Item

4

Front of list Back of list

(6)

IIT Bombay

Deletion in Doubly Linked List

Efficient (constant time) deletion of objects

Data Item

1

Data Item

3

Data Item

4

Front of list Back of list

(7)

IIT Bombay

Insertion in Doubly Linked List

Efficient (constant time) insertion of objects

Data Item

1

Data Item

3

Data Item

4

Front of list Data Back of list

Item 2

(8)

IIT Bombay

Insertion in Doubly Linked List

Efficient (constant time) insertion of objects

Data Item

1

Data Item

2

Data Item

3

Data Item

4

Front of list Back of list

(9)

IIT Bombay

Accessing n

th

Element in Doubly Linked List

Inefficient: Requires traversing list

Data Item

1

Data Item

2

Data Item

3

Data Item

4

Front of list Back of list

Example: Accessing 3

rd

element in list

(10)

IIT Bombay

Comparison with a Vector

• Contiguous memory locations for successive elements

• Insertion and deletion much more expensive Requires copying of data items/objects

• Accessing n

th

element is efficient (constant time)

Data Item

1

Data Item

2

Data Item

3

Data Item

4

Choice of linked list vs vector depends on nature of program

E.g., Sorting: Linked list is a good choice

(11)

IIT Bombay

The “list ” class

• For representing and manipulating doubly linked lists

Template class : Can be instantiated with type of data item

• Dynamically allocate/de-allocate memory

• Dynamic memory management built in

• “list” objects are container objects

• Must use #include <list> at start of program

• Large collection of member functions

• We’ll see only a small subset

(12)

IIT Bombay

Simple Programming using “list ”

#include <iostream>

#include <list>

using namespace std;

int main() {

list<string> names;

list<string> books (3, “Alice in Wonderland”);

list<int> numbers (10, -1);

… Some other code …

}

Creates an empty list of strings

Name of list

(13)

IIT Bombay

Simple Programming using “list ”

#include <iostream>

#include <list>

using namespace std;

int main() {

list<string> names;

list<string> books (3, “Alice in Wonderland”);

list<int> numbers (10, -1);

… Some other code …

}

Creates a list of 3 strings.

Each string in the list is

“Alice in Wonderland”

(14)

IIT Bombay

Simple Programming using “list ”

#include <iostream>

#include <list>

using namespace std;

int main() {

list<string> names;

list<string> books (3, “Alice in Wonderland”);

list<int> numbers (10, -1);

… Some other code …

}

Creates a list of 10 integers.

Each integer in the list is -1

(15)

IIT Bombay

Finding the Size of a “list ”

#include <iostream>

#include <list>

using namespace std;

int main() {

list<string> names;

list<string> books (3, “Alice in Wonderland”);

list<int> numbers (10, -1);

cout << “Sizes: “;

cout << names.size() << “ “ << books.size() << “ “ << numbers.size();

cout << endl;

return 0;

}

Sizes: 0 3 10

(16)

IIT Bombay

Adding Elements at Front/Back of a “list ”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

… Some other code … }

names:

(17)

IIT Bombay

Adding Elements at Front/Back of a “list ”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

… Some other code … }

names:

“Abdul”

(18)

IIT Bombay

Adding Elements at Front/Back of a “list ”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

… Some other code … }

names:

“Ajanta” “Abdul”

(19)

IIT Bombay

Adding Elements at Front/Back of a “list ”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

… Some other code … }

names:

“Ajanta” “Abdul” “Bobby”

(20)

IIT Bombay

Adding Elements at Front/Back of a “list ”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

… Some other code … }

names:

“Alex” “Ajanta” “Abdul” “Bobby”

(21)

IIT Bombay

Removing Elements From Front/Back of a “list”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

names.pop_back();

… Some other code …

}

names:

“Alex” “Ajanta” “Abdul” “Bobby”

(22)

IIT Bombay

Removing Elements From Front/Back of a “list”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

names.pop_back();

names.pop_front(); names.pop_front();

… Some other code …

}

names:

“Alex” “Ajanta” “Abdul”

(23)

IIT Bombay

Removing Elements From Front/Back of a “list”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

names.pop_back();

names.pop_front(); names.pop_front();

… Some other code …

}

names:

“Ajanta” “Abdul”

(24)

IIT Bombay

Removing Elements From Front/Back of a “list”

int main() {

list<string> names;

names.push_back(“Abdul”);

names.push_front(“Ajanta”);

names.push_back(“Bobby”);

names.push_front(“Alex”);

names.pop_back();

names.pop_front(); names.pop_front();

… Some other code …

}

names:

“Abdul”

(25)

IIT Bombay

Iterator Related Functions in “list ” Class

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it;

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

begin(), end()

member functions

Bobby, Ajanta, Abdul,

(26)

IIT Bombay

Iterator Related Functions in “list ” Class

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::reverse_iterator rit;

for (rit = names.rbegin(); rit != names.rend(); rit++) { cout << *rit << “, “;

}

return 0;

}

rbegin(), rend()

member functions

Abdul, Ajanta, Bobby,

(27)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Ajanta” “Abdul”

(28)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Ajanta” “Abdul”

(29)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Ajanta” “Abdul”

(30)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Alex” “Ajanta” “Abdul”

(31)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

Bobby, Alex, Ajanta, Abdul,

(32)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Alex” “Ajanta” “Abdul”

(33)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Alex” “Ajanta” “Abdul”

(34)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Avi” “Avi” “Alex” “Ajanta” “Abdul”

(35)

IIT Bombay

Inserting in a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); it--; names.insert(it, 2, “Avi”);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

Bobby, Avi, Avi, Alex, Ajanta, Abdul,

(36)

IIT Bombay

Removing from a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); names.erase(it);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Ajanta” “Abdul”

(37)

IIT Bombay

Removing from a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); names.erase(it);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Alex” “Ajanta” “Abdul”

(38)

IIT Bombay

Removing from a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); names.erase(it);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

names:

“Bobby” “Alex” “Abdul”

(39)

IIT Bombay

Removing from a “list ” using Iterator

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); names.erase(it);

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “;

}

return 0;

}

Bobby, Alex, Abdul,

(40)

IIT Bombay

Accessing the Front and Back Elements in a “list ”

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); names.erase(it);

cout << “Front element is: “ << names.front() << endl;

cout << “Back element is: “ << names.back() << endl;

return 0;

} names:

“Bobby” “Alex” “Abdul”

(41)

IIT Bombay

Accessing the Front and Back Elements in a “list ”

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it = names.begin(); it++;

names.insert(it, “Alex”); names.erase(it);

cout << “Front element is: “ << names.front() << endl;

cout << “Back element is: “ << names.back() << endl;

return 0;

} Front element is: Bobby

Back element is: Abdul

(42)

IIT Bombay

Reversing a “list ”

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it;

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl;

names.reverse();

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; return 0;

}

names:

“Bobby” “Ajanta” “Abdul”

(43)

IIT Bombay

Reversing a “list ”

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it;

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl;

names.reverse();

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; return 0;

}

Bobby, Ajanta, Abdul,

(44)

IIT Bombay

Reversing a “list ”

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it;

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl;

names.reverse();

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; return 0;

}

names:

“Abdul” “Ajanta” “Bobby”

(45)

IIT Bombay

Reversing a “list ”

int main() {

list<string> names;

names.push_front(“Abdul”);

names.push_front(“Ajanta”); names.push_front(“Bobby”);

list<string>::iterator it;

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl;

names.reverse();

for (it = names.begin(); it != names.end(); it++) { cout << *it << “, “; } cout << endl; return 0;

}

Abdul, Ajanta, Bobby,

(46)

IIT Bombay

Lists of Complex Data Types

• “list” is a template class

Can be instantiated with any data type Can even have lists of lists of lists …

list<V3> myList1;

list<list<int *> > myList2;

Note the space

(47)

IIT Bombay

Summary

• “list” class and its usage

• Only some features studied

• Several more features exist …

References

Related documents

• Certain memory addresses outside the part of memory allocated to process by operating system. • Dereferencing such an address leads to

“padding” (unused memory locations) after locations allocated for different members of a structure..

• Uses dynamically allocated array to store elements Array can grow or shrink in size. • Dynamic memory management

„ Allocation gives facts weighted assignments to possible completions, leading to an extended version of the data (Allocated Database ).. z The schema of Allocated Database

• Kernel memory may be statically allocated or kernel may ask page allocator for more pages if needed. • KMA shouldn’t suffer much from fragmentation and also should be

– Destroying the only pointer to memory allocated on the heap before it is deallocated (“Memory Leak”)... it might in general be allocated for some

allows other objects to send messages to given object each is unique, even though it has same capabilities think of class of CS15 students. A reference is just the address in

• Our Objectives: To perform static analysis of heap allocated data for making unused data unreachable in order to improve garbage collection and plug memory leaks.. •