Vectors, lists and queues

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

M The University Of Michigan Andrew M. Morgan EECS Lecture 22 Savitch Ch. 16 Intro To Standard Template Library STL Container Classes STL Iterators.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 19 Standard Template Library. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Iterators Constant and mutable.
Thrust & Curand Dr. Bo Yuan
Hold data and provide access to it. Random-access containers: -Allow accessing any element by index -arrays, vectors Sequential containers: -Allow accessing.
For(int i = 1; i
C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
SEG4110 – Advanced Software Design and Reengineering TOPIC J C++ Standard Template Library.
Computer Science 1620 Math Library. Remember this program? suppose that I invest $25000 into a mutual fund that returns 8% per year. Write a program to.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Introduction to C++ Programming. Brief Facts About C++ Evolved from C Designed and implemented by Bjarne Stroustrup at the Bell Labs in the early 1980s.
PHYS707: Special Topics C++ Lectures Lecture 3. Summary of Today’s lecture: 1.Functions (call-by-referencing) 2.Arrays 3.Pointers 4.More Arrays! 5.More.
CS 240 Computer Programming 1
Chapter 12 Separate Compilation and Namespaces. Abstract Data Type (ADT) ADT: A data type consisting of data and their behavior. The abstraction is that.
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Derived data types Dealing with data –Where the information is stored –What value is kept there –What kind of information is stored Address operator Pointers.
Exercise 2.
STL Antonio Cisternino. Introduction The C++ Standard Template Library (STL) has become part of C++ standard The main author of STL is Alexander Stephanov.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
More on the STL vector list stack queue priority_queue.
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
Arrays, Pointers and Structures CS-240 Dick Steflik.
Standard Template Library. Homework List HW will be posted on webpage Due Nov 15.
Standard Template Library C++ introduced both object-oriented ideas, as well as templates to C Templates are ways to write general code around objects.
CSE 332: Combining STL features Combining STL Features STL has containers, iterators, algorithms, and functors –With several to many different varieties.
Standard Template Library Programming paradigm: generic programming the decomposition of programs into components which may be developed separately and.
By – Tanvir Alam.  This tutorial offers several things.  You’ll see some neat features of the language.  You’ll learn the right things to google. 
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
Templates Mark Hennessy Dept Computer Scicene NUI Maynooth C++ Workshop 18 th – 22 nd September 2006.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
Introduction to the Standard Template Library (STL) A container class holds a number of similar objects. Examples: –Vector –List –Stack –Queue –Set –Map.
Lecture 8-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
Lecture 7 : Intro. to STL (Standard Template Library)
Exceptions & Templates
Starting Out with C++, 3 rd Edition Introduction to the STL vector The Standard Template Library (or STL) is a collection of data types and algorithms.
1 The Standard Template Library Drozdek Section 3.7.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Lecture 7-3 : STL Algorithms. STL Algorithms The Standard Template Library not only contains container classes, but also algorithms that operate on sequence.
Copyright © Curt Hill STL Priority Queue A Heap-Like Adaptor Class.
General Computer Science for Engineers CISC 106 Lecture 27 Dr. John Cavazos Computer and Information Sciences 04/27/2009.
C++ Standard Template Library
Lecture 7-2 : STL Iterators
Generic Programming Techniques in C++
Lecture 7-2 : STL Iterators
Today’s Learning Objective
C++ Functions, Classes, and Templates
Random Number Generation
Name: Rubaisha Rajpoot
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Templates. Templates Example: Swap Template Mechanism.
CSE 303 Lecture 25 Loose Ends and Random Topics
Lecture 8-2 : STL Iterators and Algorithms
foo.h #ifndef _FOO #define _FOO template <class T> class foo{
STL Библиотека стандартных шаблонов
STL (Standard Template Library)
Pointers & Functions.
C++ Programming: chapter 10 – STL
Programming Strings.
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Vectors, lists and queues STL Data Types in C++ Vectors, lists and queues

The Standard Template Library As we have already seen C++ supports all of the data types and structures that C offers. As well as these there is the data types in the standard template library. These are held in a separate library from C++ and are available for many (if not most) C++ implmentations.

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector <string> v; cout << " Enter lines of text to be sorted,\n"; cout << " followed by the word stop:\n"; for (;;) {string s; getline(cin,s); if ( s == "stop") break; v.push_back(s); } //for sort(v.begin(), v.end()); cout << " The samelines after sorting: \n"; for (int i=0; i < v.size(); i++) cout << v[i] << endl; return 0; }

Vectors Note the use of #includes vector <string> v declaration v.push_back(s) method call sort(v.begin(), v.end()) call to sort function With vector methods as parameters cout << v[i] << endl; access to vector elements

Using iterators In the STL there are a special class of access types, called iterators, that act as pointers to the special classes like vectors. These are declared with the cumbersome syntax vector <string>::iterator i Obviously the above is for our vector of strings.

#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector <string> v; cout << " Enter lines of text to be sorted,\n"; cout << " followed by the word stop:\n"; for (;;) {string s; getline(cin,s); if ( s == "stop") break; v.push_back(s); } //for sort(v.begin(), v.end()); cout << " The samelines after sorting: \n"; for (vector <string>::iterator i=v.begin(); i != v.end(); i++) cout << *i << endl; return 0; }

Reverse iterators It is possible to go backwards through a vector using a reverse iterator This is declared vector <string>::reverse_iterator i; It would be used as follows For (i=v.rbegin(); i != v.rend(); i++) cout << *i << endl; This would go backward through the vector – despite the ++ operation!

Vectors, Lists and Deques The above are known as container adaptors. They all offer efficient means of storage and retrieval. The simplest change to our program would have been to use a deque instead of a vector. The following program shows how slight, in syntax that program is.

#include <iostream> #include <string> #include <deque> #include <algorithm> using namespace std; int main() { deque <string> v; cout << " Enter lines of text to be sorted,\n"; cout << " followed by the word stop:\n"; for (;;) {string s; getline(cin,s); if ( s == "stop") break; v.push_back(s); } //for sort(v.begin(), v.end()); cout << " The samelines after sorting: \n"; for (deque <string>::iterator i=v.begin(); i != v.end(); i++) cout << *i << endl; return 0; }

Vectors, lists and deques Vectors permit data to be added/deleted at the end of the vector. Can randomly access data. Deques permit data to be added/deleted at the beginning or end. Can randomly access data. List can add or delete data at any position in the list. Can’t randomly access data.

Vectors, lists and deques vector <type> Insert & Delete here deque <type> Insert & Delete here list <type> Insert & Delete at Any position

Example list program #include <string> #include <iostream> #include <list> using namespace std; void showlist (const string &str, const list<int> &L) { list<int>::const_iterator i; cout << str << endl << " "; for (i=L.begin(); i != L.end(); i++) cout << *i << " "; cout << endl; }

int main() { list<int> L; int x; cout <<"Enter positive integers, followed by 0: \n"; while (cin >> x, x != 0 ) L.push_back(x); showlist("Initial list:", L); L.push_front(123); showlist("After inserting 123 at beginning: ", L); list<int>::iterator i = L.begin(); L.insert(++i,456); showlist("After inserting 456 at the second position: ", L); i = L.end(); L.insert(--i,999); showlist("After inserting 999 just before the end: ", L); i = L.begin(); x = *i; L.pop_front(); cout << "Deleted at the beginning: " << x << endl; showlist("After this deletion: ", L); i = L.end(); x = *--i; L.pop_back(); cout << "Deleted at the end: " << x << endl; i = L.begin(); x = *++i; cout << "To be deleted: " << x << endl; L.erase(i); showlist("After this deletion (of second element): ", L); return 0; }

Points to note with list The use of void showlist (const string &str, const list<int> &L) list<int>::const_iterator i; The member functions push_front(), push_back(), insert() and erase() The latter 2 being random access functions Also note the use of i = L.end(); x = *--i;