Containers Jason Goffeney 2/23/2006. Containers The STL is a standard part of probably all C++ implementations It features implementations of most of.

Slides:



Advertisements
Similar presentations
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
Advertisements

Vectors, lists and queues
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Stack and Queue Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Stacks, Queues, and Deques. A stack is a last in, first out (LIFO) data structure –Items are removed from a stack in the reverse order from the way they.
Data Structures Using C++ 2E
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Object Oriented Data Structures
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
Review for Midterm Chapter 1-9 CSc 212 Data Structures.
CMSC 202 Lesson 23 Templates II. Warmup Write the templated Swap function _______________________________ void Swap( ________ a, ________ b ) { _______________.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
Data Structures Using C++ 2E
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Basic Concepts of OOP in C++ Darvay Zsolt. C++ 2 Outline  The namespace and its members  The using declaration and directive  The address operator.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
Exception Handling. C++ 2 Outline  Throwing and handling exceptions  Exceptions of different types  The new operator and the exceptions  Re-throwing.
C++ Programming Part 2 Michael Griffiths Corporate Information and Computing Services The University of Sheffield
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Lecture 7 : Intro. to STL (Standard Template Library)
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Data Structures: CSCI Chapter 1 Data Structures: CSCI Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Overloading operators C++ incorporates the option to use standard operators to perform operations with.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
Introduction The STL is a complex piece of software engineering that uses some of C++'s most sophisticated features STL provides an incredible amount.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Chapter 6 Methods for Making Data Structures. 2 Dynamic Arrays in Data Structures In almost every data structure, we want functions for inserting and.
Lecture 7.  There are 2 types of libraries used by standard C++ The C standard library (math.h) and C++ The C++ standard template library  Allows us.
Vectors the better arrays. Why Vectors l vectors are implemented as a class (a template to be exact) l they serve the same purpose as arrays but using.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
List Structures What is a list? A homogeneous collection of elements with a linear relationship between the elements linear relationship - each element.
C++ Programming:. Program Design Including
Pointers and Linked Lists
Pointers and Linked Lists
CS 215 Final Review Ismail abumuhfouz Fall 2014.
C++ Templates.
Data Structure and Algorithms
C Programming Tutorial – Part I
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 5 Classes.
Collections Intro What is the STL? Templates, collections, & iterators
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
CMSC 341 Lecture 5 Stacks, Queues
Stacks, Queues, and Deques
Exam 1 Review CS 3358.
Pointers and Linked Lists
Exam 1 Review CS 3358.
Lecture 8 : Intro. to STL (Standard Template Library)
Data Structures & Algorithms
Lab4 problems More about templates Some STL
STL List.
Lesson 25 Miscellaneous Topics
Collections Intro What is the STL? Templates, collections, & iterators
An Introduction to STL.
Vectors the better arrays.
SPL – PS1 Introduction to C++.
STL List.
Presentation transcript:

Containers Jason Goffeney 2/23/2006

Containers The STL is a standard part of probably all C++ implementations It features implementations of most of the handy basic container classes, such as, linked lists, vectors, and hashes. Qt also implements many of these containers as well as many more. STL classes will work with any C++ code while Qt classes will only work with Qt/C++ code.

Containers How to include in your program. –STL classes use C++ style includes (which means you leave the.h off its header file). #include –Qt 4.0 classes also use C++ style include but also have capitalization #include

Containers How to include in your program. –Note: STL classes are in the std namespace which means that you usually need to prepend std:: to each STL keyword when using it std::vector newVector –If you want to avoid doing this then follow your STL includes with using namespace std; #include Using namespace std;

Containers Both STL and Qt containers are designed as templates which take a class or struct as a parameter for their declaration. For example, suppose you have a class called Point and want to make a list of points –STL: list pointList; –Qt: QList pointList;

Containers This will also work for primitive types such as int, float, char, etc. –STL: list pointList; –Qt: QList floatList; If you want to create them as pointers than you can do that too. –STL: list * pointList = new list (); –Qt: QList * floatList = new QList ();

Containers: Style Notes If a container will need to exist outside of the scope of a single function then it is generally declared in the class definition in the header file (usually in the private section). For a non-pointer container then you are done and can just use it in the rest of your code. For a pointer declare it in the header and create the container in the constructor of the source file. –In Header: QVector * ptList; –In Constructor: ptList = new QVector ();

Containers: Style Notes For the remainder of this I will likely assume non-pointers and use a “.” to show access to methods of the containers. But pointers are useful and speed up passing containers into functions. The take home point though is that “->” is used to access methods from pointers.

Containers: Vector Vectors are simple dynamically expandable and collapsible lists. Operations: –Construct: STL: vector myVector; Qt: QVector myVector; –Add an element to the end STL: myVector.push_back(5.34); Qt: myVector.append(5.34);

Containers: Vector Operations: –Insert (position, item): STL, Qt: myVector.insert(2, 4.35); –Remove an element (position): STL: myVector.erase(5); Qt: myVector.remove(5); –Access an element (position): STL/Qt: float f1 = myVector.at(3); myVector.at(0) = 3.67;

Containers: Vector Operations: –Get current size of vector STL, Qt: int size = myVector.size(); –Get first element STL, Qt: float first = myVector.front(); –Get last element STL, Qt: float first = myVector.back();

Containers: Vector Operations: –Iterating through a vector There are two ways to iterate through vectors –The old way: for(i = 0; i < myVector.size(); i++) { printf( “The float is %f\n”, myVector.at(i)); } –Problem: If you remove elements from the list while iterating bad things may happen

Containers: Vector STL Iterators: vector myIntVector; vector ::iterator myIntVectorIterator; //Iterator object for list // Add some elements to myIntVector myIntVector.push_back(1); myIntVector.push_back(4); myIntVector.push_back(8); for(myIntVectorIterator = myIntVector.begin(); myIntVectorIterator != myIntVector.end(); myIntVectorIterator++) { cout<<*myIntVectorIterator<<" "; //Should output }

Containers: Vector Qt Iterators: QVector vector;... QVectorIterator i(vector); while (i.hasNext()) { cout << i.next(); }

Containers: Vector These are a few of the ways it is possible to manipulate STL and Qt Vector containers. Look at their references to learn more.

Other Containers: Lists – Doubly linked list that support faster insertions and deletions than vector. Hashes – A container that stores and retrieves an item by a key. Allows for very fast lookups of items but only a single item can be stored per key Multihash – Like a hash except multiple objects can be stored per key with Stacks, Queues, etc…

Example One of the basic model formats is the Alias.obj format. It defines a list of vertex coordinates and then a list of faces and the indices of the vertices that compose them. 0. (0,0) 1. (50,0) 3. (50,50)2. (0,50) Vertices v 0 0 v 50 0 v 0 50 v Face f f 1 3 2

Example I want a way to store my simple model use later. So I need to make some classes or structures. s are simple to represent as a couple of Integers. (Note: the QPoint class already exists in Qt). struct Point2i { //A point that holds 2 integer values int x, y; };

Example I want a way to store my simple model use later. So I need to make some classes or structures. Vertices are simple to represent as a couple of Integers. (Note: the QPoint class already exists in Qt). class Vertex2i { public: //Constructor Vertex2i(int xi, int yi)(x = xi; y = yi;} //A point that holds 2 integer values int x, y; };

Example A face can be composed of any number of vertices so a list of point indices could be useful here. class Face { public: //A face holds ? vertices vector vertices; };

Example I will load my vertices into a vector of vertices. vector vertexList; Vertex2i v0(0,0); Vertex2i v1(50,0); Vertex2i v2(0,50); Vertex2i v3(50,50); vertexList.push_back(v0); vertexList.push_back(v1); vertexList.push_back(v2); vertexList.push_back(v3);

Example I will build my Faces and load them into a list as well. vector faceList; Face f1; f1.vertices.push_back(0); f1.vertices.push_back(1); f1.vertices.push_back(2); Face f2; f2.vertices.push_back(1); f2.vertices.push_back(3); f2.vertices.push_back(2); faceList.push_back(f1); faceList.push_back(f2);

Example Now I want to draw the faces. for(int j = 0; j < faceList.size(); j++) { glBegin(GL_LINE_LOOP); //Start drawing vector tempList = faceList.at(j).vertices; for(int m = 0; m < tempList.size(); m++) { int vIdx = tempList.at(m); glVertex2i( vertexList.at(vIdx).x, vertexList.at(vIdx).y); } glEnd(); //Stop drawing }

Example If you want to try it: –Put the Vertex2i and Face class definitions in the top of your GLWidget.h below the includes. –Declare the vectors in your GLWidget class with the appropriate includes. –In the GLWidget constructor put all the loading code –In the GLWidget paintGL method put the drawing code right below the glClear statement

Example Generally, a model will be loaded from a file which makes the vector filling less tedious. Notice that by adding a z coordinate to he Vertex class this will easily handle 3D models as well.