Cse 20232 lecture 30 stl containers
This presentation is the property of its rightful owner.
Sponsored Links
1 / 22

CSE 20232 Lecture 30 – STL Containers PowerPoint PPT Presentation


  • 109 Views
  • Uploaded on
  • Presentation posted in: General

CSE 20232 Lecture 30 – STL Containers. What is a container class? STL Containers vector, list, dequeue STL Container Adapters stack, queue, priority_queue STL Associative Containers set, map, multiset , multimap Examples: vector & list. What is a container?.

Download Presentation

CSE 20232 Lecture 30 – STL Containers

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Cse 20232 lecture 30 stl containers

CSE 20232Lecture 30 – STL Containers

  • What is a container class?

  • STL Containers

    • vector, list, dequeue

  • STL Container Adapters

    • stack, queue, priority_queue

  • STL Associative Containers

    • set, map, multiset , multimap

  • Examples: vector & list


What is a container

What is a container?

  • A container is a template class that

    • Holds a collection of data items

    • Generally is dynamically resizable

    • Has well defined functions for

    • Accessing, inserting, removing, and traversing items

  • The items in some containers are in order, and in others are not

  • Some allow access by index, some by iterator, and some by key value


Vector container

Vector Container

  • Vector – like a super array

    • Resizable using

      • constructor with size parameter

      • resize(n) to set size directly

      • push_back(value) to append a new value

      • pop_back() to remove last value

    • Indexed access using [i] operator,

      • but adding ith element this way does not resize vector

    • Can contain any class of objects once type is specified in declaration


Dequeue container

Dequeue container

  • Dequeue – like a vector except

    • Values may be added and removed from the beginning as well as the end, using …

      • push_front(value) and pop_front().


Comparing containers array vector dequeue

Comparing containersarray – vector – dequeue

Array size is fixed

Vector is expandable at one end

Dequeue is expandable at both ends


List container

List container

  • List – is a doubly-linked list

    • Values are easily (efficiently) inserted and removed at any place in the list

    • Size is always exactly the size needed for the items stored

    • Access is sequential only, no indexed access

    • Traversal of all elements is by using iterators

    • Manipulation functinos include …

      • pushback(value), push_front(value),

      • pop_back() pop_front(),

      • insert(pos,value) and erase(pos)


Iterator

Iterator

  • Iterator – like a super pointer

  • All containers have functions begin() and end() that return iterators

    • begin() returns an iterator to the first item

    • end() returns an iterator just past the last iterm

  • Iterators are moved to the next item (iter++) or the previous item (iter--)

  • Iterators are dereferenced (*iter) to access the item it points to


Lists and iterators

Lists and iterators

iter [ ]

iter = pL.begin();

while (iter != pL.end())

{

cout << “Node value: “ << *iter << endl;

iter++;

}

This moves the iterator down the list


Stack adapter

Stack Adapter

  • Stack – may be a vector, list or dequeue

  • Has last-in,first-out (LIFO) behavior

  • Functions

    • push(value) – adds new value

    • top() – returns most recently added value

    • pop() – removes most recently added value

pop & top

push


Queue adapter

Queue Adapter

  • Queue – may be a vector, list or dequeue

  • Has first-in,first-out (FIFO) behavior

  • Functions

    • push(value) – adds new value

    • front() – returns least recently added value

    • pop() – removes least recently added value

  • Priority_queue – like a queue except …

    • Items have weight or priorities that establish their order in the queue

push

pop & front


Set container

Set container

  • Set – like a mathematical set

    • Is a balanced search tree behind the scenes

    • Contains no duplicate values

    • Data items are stored in order

    • Iterators traverse items in order

  • Multiset – like a set except it may contain duplicates

2

12

37


Map container

Map container

  • Map – like a database table

    • Is a balanced search tree behind the scenes

    • Contains key,value pairs

    • Data items are stored in order by key

    • No duplicate keys are allowed

    • Iterators traverse items in order

    • Access is by key or iterator

    • A form of key based indexing is supported

  • Multimap – like a map except it may contain duplicates

Bob:12

Frank:33

Sue:3


Where are they defined

Where are they defined?

  • Include the following headers for each type of container needed in your program

  • <vector> // for vectors

  • <dequeue> // for dequeues

  • <list> // for lists

  • <stack> // for stacks

  • <queue> // for queues & priority_queues

  • <set> // for sets and multisets

  • <map> // for maps and multimaps


Using vectors lists

Using vectors & lists

  • Example1: vect_list.cpp

    • Shows use of push_back() with vector and push_front() with list

    • Shows use of indexes with vector

    • Shows use of iterator with list

  • Example2: vect_list2.cpp

    • Shows use insert() with list

    • Shows use reverse iterator with list

    • Shows ordering of both and removal of duplicates in list


Vect list cpp

Vect_list.cpp

// vect_list.cpp - JHS 2006

//

// This is a demo of the STL vector and list containers.

#include <vector>

#include <list>

#include <iostream>

using namespace std;

int main()

{

vector<int> numVect; // declare an integer vector

list<int> numList; // declare an integer list

int num;

// at this point the vector and list are empty and have

// essentially no memory space for values


Vect list cpp1

Vect_list.cpp

// now lets get some values and append them to the vector

// and add them to BOTH ends of the list

cout << "Enter a series of non-negative integers (-99 to quit)“

<< endl;

cin >> num; // get a number

while (num != -99) // as long as it is not the sentinel

{

numVect.push_back(num); // append num to end of vector

numList.push_front(num); // prepend num to number list

numList.push_back(num); // append num to number list

cin >> num; // get another number

}


Vect list cpp2

Vect_list.cpp

// finally, lets output the values in both containers

// using indices with the vector and iterators with the list

cout << "Vector: ";

for (int i=0; i<numVect.size(); i++)

// output ith valule in vector, could have used an iterator

cout << ' ' << numVect[i];

cout << endl;

cout << " List: ";

list<int>::iterator iter;

for (iter = numList.begin(); iter != numList.end(); iter++)

// output ith value in list, cannot use an index

cout << ' ' << *iter;

cout << endl;

return 0;

}


Vect list2 cpp

Vect_list2.cpp

// vect_list2.cpp - JHS 2006

//

// This is a demo of the STL vector and list containers.

// showing the use of both as ordered (sorted) sequences

#include <vector>

#include <list>

#include <iostream>

#include <string>

using namespace std;

int main()

{

vector<string> strVect; // declare a string vector

list<string> strList; // declare a string list

string word;


Vect list2 cpp1

Vect_list2.cpp

cout << "Enter a sequence of words." << endl;

while ((cin.peek() != '\n') && (cin >> word))

{ // as long as there is another word, get it and ...

// insert it into the vector, in order

strVect.push_back(word); // append num to end of vector

int i = strVect.size() - 1;

bool done = false;

while ((0 < i) && (! done))

{ // compare new value to one on its left

if (strVect[i] < strVect[i-1]) // if pair is out of order

{

string temp = strVect[i]; // swap them

strVect[i] = strVect[i-1];

strVect[i-1] = temp;

}

else

done = true; // found first value smaller than new one

i--; // move left (down) to next pair

}


Vect list2 cpp2

Vect_list2.cpp

// now insert word into the list, in order

// find last string in list that shoud precede new word

list<string>::iterator iter = strList.begin();

while ((iter != strList.end()) && (*iter < word))

iter++;

// now insert new word

strList.insert(iter,word); // insert word before *itert

}

// now for some outputs in different orders

// output vector values in order

cout << " Vector: ";

for (int i=0; i<strVect.size(); i++)

cout << ' ' << strVect[i]; // output ith value in vector

cout << endl;


Vect list2 cpp3

Vect_list2.cpp

// output list values in order

cout << " List: ";

list<string>::iterator iter;

for (iter = strList.begin(); iter != strList.end(); iter++)

cout << ' ' << *iter; // output ith value in list

cout << endl;

// remove the duplicates from list

for (iter = strList.begin(); iter != strList.end(); iter++)

{

list<string>::iterator dup = iter;

dup++; // point to next item in list

while ((dup != strList.end()) && (*dup == *iter))

// erase duplicate and move dup to next item in list

dup = strList.erase(dup);

}


Vect list2 cpp4

Vect_list2.cpp

// show list without duplicates

cout << "!dupList: ";

for (iter = strList.begin(); iter != strList.end(); iter++)

cout << ' ' << *iter; // output ith value in list

cout << endl;

// show list items in reverse order, using a reverse iterator

cout << " revList: ";

list<string>::reverse_iterator riter;

for (riter = strList.rbegin(); riter != strList.rend(); riter++)

cout << ' ' << *riter; // output ith value in reversed list

cout << endl;

return 0;

}


  • Login