Lecture 12 - Using Library Algorithms

Lecture 12 - Using Library Algorithms - Using Library...

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

View Full Document Right Arrow Icon
Using Library Algorithms Lectures 15 and 16 Hartmut Kaiser [email protected] 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 Avoid Creating a YAGNI (You aren’t going to need it) - You should try not to add functionality until you need it. http://en.wikipedia.org/wiki/YAGNI 10/17/2011 Lecture 15, 10/19/2011 Lecture 16 CSC 1254, Fall 2011, Using Library Algorithms 2
Background image of page 2
Abstract Many containers share similar operations, like insert() or erase(). Those have the same interface for all of them (even for strings). All containers expose a companion iterator type allowing to navigate through the elements stored in the container. Again, all of them expose a similar interface We will see how the library exploits these similarities by exposing generic algorithms: by exposing uniform interfaces independent of the container they are applied to. 10/17/2011 Lecture 15, 10/19/2011 Lecture 16 CSC 1254, Fall 2011, Using Library Algorithms 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
Analyzing Strings Looking back to picture concatenation: // copy entire bottom picture, use iterators for (vector<string>::const_iterator it = bottom.begin(); it != bottom.end(); ++it) { ret.push_back(*it); } // copy entire bottom picture, use vector facilities ret.insert(ret.end(), bottom.begin(), bottom.end()); There is an even more generic solution: // copy entire bottom picture, use standard algorithm std::copy(bottom.begin(), bottom.end(), back_inserter(ret)); 10/17/2011 Lecture 15, 10/19/2011 Lecture 16 CSC 1254, Fall 2011, Using Library Algorithms 4
Background image of page 4
Generic Algorithms std::copy is a generic algorithm ▫ Not part of any container ▫ Its operation is determined by its arguments Most of the time the standard algorithms expect iterators std::copy takes 3 iterators (begin, end, out) and copies the range [begin, end) to a sequence starting at out (extending as necessary) 10/17/2011 Lecture 15, 10/19/2011 Lecture 16 CSC 1254, Fall 2011, Using Library Algorithms 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
Standard Algorithm: copy Writing copy(begin, end, out); Is equivalent to (except for iterators not being copied): while (begin != end) *out++ = *begin++; What does ‘ *out++ = *begin++; ’ mean? { *out = *begin; ++out; ++begin; } 10/17/2011 Lecture 15, 10/19/2011 Lecture 16 CSC 1254, Fall 2011, Using Library Algorithms 6
Background image of page 6
Iterator Adaptor std::back_inserter() is an iterator adaptor ▫ Function returning an iterator created based on its arguments ▫ Here, it takes a container and returns an iterator, which when used as a destination, appends elements to that container This will append all of bottom to ret: copy(bottom.begin(), bottom.end(), back_inserter(ret)); 10/17/2011 Lecture 15, 10/19/2011 Lecture 16 CSC 1254, Fall 2011, Using Library Algorithms 7
Background image of page 7

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

View Full DocumentRight Arrow Icon
Caveats: copy This will not work (why?): copy(bottom.begin(), bottom.end(), ret); // ret is not an iterator, but a container This will compile, but not work (why?): copy(bottom.begin(), bottom.end(), ret.end());
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/29/2011 for the course CSC 1254 taught by Professor Blanks,l during the Fall '08 term at LSU.

Page1 / 54

Lecture 12 - Using Library Algorithms - Using Library...

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

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