C++_containers - Intro to the C+ Standard Template Library...

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 Intro to the 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 Extensive use of function and class templates, concepts The STL makes use of several new ideas too 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; // type traits typedef value_type * pointer; typedef value_type & reference; typedef const_value_type* const_pointer; typedef value_type & reference; typedef size_t size_type; … … typedef pointer iterator; // iterator types typedef const_pointer const_iterator; … … }; From Matthew H. Austern, “ Generic Programming and the STL” Example: Making a C++ Array an STL Container
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 }; Example: Array-based STL Container, Continued
Background image of page 4
CSE 332: C++   STL containers Basic Requirements for an STL Container Contains elements of a parameterized type Sets/ranges of contained elements cannot overlap Element cannot belong to more than one container Adding and copying elements is  by value Container owns the elements Container’s destructor destroys what it contains Provide interfaces to contained values Functions to add/remove values, e.g.,  push_back Operations, e.g.,  operator[]  for random access Should obey the “principle of least surprise” For example a linked list would not provide  []
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 / 22

C++_containers - Intro to the C+ Standard Template Library...

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