C++_STL_containers - The STL is a collection of related...

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

View Full Document Right Arrow Icon
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements Containers Data structures: store values according to a specific organization Iterators Variables used to give flexible access to the values in a container Algorithms Functions that use iterators to access values in containers Perform computations that modify values, or creates new ones Function objects Encapsulate a function as an object, use to modify an algorithm The STL makes use of most of what we’ve covered Functions, classes, structs Extensive use of function and class templates, concepts Typedefs, traits, and associated types
Background image of page 1

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

View Full DocumentRight Arrow Icon
CSE 332: C++ STL containers Intro to C++ STL Containers Goals See how STL containers are implemented See how differences in implementation affect use We’ll cover several kinds Focus on template concepts And how each container’s concept relates to its implementation Example to the left prints v[0] is 1 v[1] is 2 v[2] is 4 #include <iostream> #include <vector> using namespace std; int main (int, char *[]) { vector<int> v; // This would be asking for trouble. ... // v[0] = 1; v[1] = 2; v[2] = 4; // . .. but this works fine. .. v.push_back (1); v.push_back (2); v.push_back (4); // . .. and now this is ok . .. for (size_t s = 0; s < v.size(); ++s) { cout << "v[" << s << "] is " << v[s] << endl; } return 0; }
Background image of page 2
CSE 332: C++ STL containers template <class T, size_t N> struct block{ typedef T value_type; // basic associated types typedef const T const_value_type; typedef value_type * pointer; typedef value_type & reference; typedef const_value_type* const_pointer; typedef const_value_type & const_reference; typedef size_t size_type; …… typedef pointer iterator; // iterator associated types typedef const_pointer const_iterator; …… }; From Matthew H. Austern, “Generic Programming and the STL”  Example: Making a STL Container from an Array
Background image of page 3

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

View Full DocumentRight Arrow Icon
CSE 332: C++ STL containers template <class T, size_t N> struct block{ …… iterator begin() {return data;} // iterator accessors iterator end() {return data+N;} const_iterator begin() const {return data;} const_iterator end() const {return data+N;} …… reference operator [ ] (size_type n) // container interface {return data[n];} const_reference operator [ ] (size_type n) const {return data[n];} size_type size() const {return N;} T data [N]; // contained elements }; Array-based STL Container Example, Continued
Background image of page 4
CSE 332: C++
Background image of page 5

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

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

Page1 / 21

C++_STL_containers - The STL is a collection of related...

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

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