Lecture 8 - Using Sequential Containers

Lecture 8 - Using Sequential Containers - Using Sequential...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Using Sequential Containers Lecture 8 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/˜hkaiser/fall_2011/csc1254.html
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Programming Principle of the Day Principle of least astonishment (POLA/PLA) ▫ The principle of least astonishment is usually referenced in regards to the user interface, but the same principle applies to written code. ▫ Code should surprise the reader as little as possible. That means following standard conventions, code should do what the comments and name suggest, and potentially surprising side effects should be avoided as much as possible. http://en.wikipedia.org/wiki/Principle_of_least_astonishment 9/22/2011, Lecture 8 CSC 1254, Fall 2011, Using Sequential Containers 2
Background image of page 2
Abstract We start looking beyond vector and string. We will focus on sequential containers and demonstrate a couple of problems we can solve when applying them. The standard library’s architecture will start to get visible. That will help us to start understanding how to use all of the different containers in the standard library. 9/22/2011, Lecture 8 CSC 1254, Fall 2011, Using Sequential Containers 3
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Separating Students into Categories Sort out failed students ▫ Who failed? ▫ Remove from our data Create a new vector of student_data containing only students who succeeded: // predicate to determine whether a student failed bool fail_grade(student_info const & s) { return grade(s) < 60; } Push student data onto one of two containers based on this predicate 9/22/2011, Lecture 8 CSC 1254, Fall 2011, Using Sequential Containers 4
Background image of page 4
Separating Students into Categories What’s wrong here? (Hint: what’s the memory consumption?) // separate passing and failing student records: first try vector<student_info> extract_fails(vector<student_info>& students) { vector<student_info> pass, fail; for (vector<student_info>::size_type i = 0; i != students.size(); ++i) { if (fail_grade(students[i])) fail.push_back(students[i]); else pass.push_back(students[i]); } students = pass; return fail; } 9/22/2011, Lecture 8 CSC 1254, Fall 2011, Using Sequential Containers 5
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Separating Students into Categories Requires twice as much memory ▫ Each record is held twice Better to copy failed students, removing the data from original vector ▫ How to remove elements from a vector? ▫ Slow, too slow for larger amounts of data. Why? What happens if all students have failed? ▫ This can be solved by either using a different data structure or by modifying the algorithm 9/22/2011, Lecture 8 CSC 1254, Fall 2011, Using Sequential Containers 6
Background image of page 6
Erasing Elements in Place Slow, but direct solution (Why is it slow?) // second try: correct but potentially slow vector<student_info> extract_fails(vector<student_info>& students) { vector<student_info> fail;
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 25

Lecture 8 - Using Sequential Containers - Using Sequential...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online