120 likes | 278 Views
Sequence Containers. The C++ Standard Template Library provides three sequence containers vector, list and deque Class template vector and class template deque both are based on arrays Class template list implements a linked-list data structure which is studied earlier
E N D
Sequence Containers • The C++ Standard Template Library provides three sequence containers vector, list and deque • Class template vector and class template deque both are based on arrays • Class template list implements a linked-list data structure which is studied earlier • One of the most popular containers in the STL is vector • A vector changes size dynamically
Sequence Containers • Unlike C and C++ ordinary arrays vectors can be assigned to one another • This is not possible with pointer-based, C-like arrays • Array names are constant pointers and cannot be the targets of assignments
Associative Containers • The STL's associative containers provide direct access to store and retrieve elements via keys (often called search keys) • The four associative containers are multiset, set, multimap and map • Each associative container maintains its keys in sorted order • Classes multiset and set provide operations for manipulating sets of values where the values are the keys • There is not a separate value associated with each key
Associative Containers • The primary difference between a multiset and a set is that a multiset allows duplicate keys and a set does not • Classes multimap and map provide operations for manipulating values associated with keys • The primary difference between a multimap and a map is that a multimap allows duplicate keys with associated values to be stored and a map allows only unique keys with associated values
Standard Library container STL • STL can be categorized into the following groupings: • Container classes: • Sequences: • vector: Dynamic array of variables, struct or objects. Insert data at the end • deque: Array which supports insertion/removal of elements at beginning or end of array • list: Linked list of variables, struct or objects functions may include Insert/remove anywhere • Associative Containers: • set (duplicate data not allowed in set), multiset (duplication allowed): Collection of ordered data in a balanced binary tree structure. Fast search. • map (unique keys), multimap (duplicate keys allowed): Associative key-value pair held in balanced binary tree structure. • Container adapters: • stack LIFO • queue FIFO • priority_queue returns element with highest priority.
Standard Library container STL String: • string: Character strings and manipulation • rope: String storage and manipulation • bitset: Contains a more intuitive method of storing and manipulating bits. • Operations/Utilities: • iterator: (examples in this tutorial) STL class to represent position in an STL container. An iterator is declared to be associated with a single container class type. • algorithm: Routines to find, count, sort, search, ... elements in container classes • auto_ptr: Class to manage memory pointers and avoid memory leaks.
//Dynamic array of variables, struct or objects. Insert data at the end #include <iostream>#include <vector>#include <string>using namespace std;main(){vector<string> SS;SS.push_back("The number is 10");SS.push_back("The number is 20");SS.push_back("The number is 30");cout << "Loop by index:" << endl;int ii;for(ii=0; ii < SS.size(); ii++){cout << SS[ii] << endl;}cout << endl << "Constant Iterator:" << endl; vector<string>::const_iterator cii;for(cii=SS.begin(); cii!=SS.end(); cii++){cout << *cii << endl;}cout << endl << "Reverse Iterator:" << endl;vector<string>::reverse_iterator rii;for(rii=SS.rbegin(); rii!=SS.rend(); ++rii){cout << *rii << endl;}cout << endl << "Sample Output:" << endl;cout << SS.size() << endl;cout << SS[2] << endl;swap(SS[0], SS[2]);cout << SS[2] << endl;} Vector Example 1
Output Loop by index:The number is 10The number is 20The number is 30Constant Iterator:The number is 10The number is 20The number is 30Reverse Iterator:The number is 30The number is 20The number is 10Sample Output:3The number is 30The number is 10
//Two / Three / Multi Dimensioned arrays using vector A two dimensional array is a vector of vectors. The vector contructor can initialize the length of the array and set the initial value #include <iostream>#include <vector>using namespace std;main(){// Declare size of two dimensional array and initialize.vector< vector<int> > vI2Matrix(3, vector<int>(2,0)); vI2Matrix[0][0] = 0;vI2Matrix[0][1] = 1;vI2Matrix[1][0] = 10;vI2Matrix[1][1] = 11;vI2Matrix[2][0] = 20;vI2Matrix[2][1] = 21;cout << "Loop by index:" << endl;int ii, jj;for(ii=0; ii < 3; ii++){for(jj=0; jj < 2; jj++){cout << vI2Matrix[ii][jj] << endl;}} Vectors example 2
MAP • Map is a Sorted Associative Container that associates objects of type Key with objects of type Data • Map is a Pair Associative Container, meaning that its value type is pair<const Key, Data> • It is also a Unique Associative Container, meaning that no two elements have the same key
MAP • Inserting a new element into a map does not invalidate iterators that point to existing elements • Erasing an element from a map also does not invalidate any iterators, except, of course, for iterators that actually point to the element that is being erased
#include <string.h> #include <iostream> #include <map> #include <utility> using namespace std; int main() { map<int, string> Employees; // 1) Assignment using array index notation Employees[5234] = "Mike C."; Employees[3374] = "Charlie M."; Employees[1923] = "David D."; Employees[7582] = "John A."; Employees[5328] = "Peter Q."; cout << "Employees[3374]=" << Employees[3374] << endl << endl; cout << "Map size: " << Employees.size() << endl; for( map<int,string>::iterator ii=Employees.begin(); ii!=Employees.end(); ++ii) { cout << (*ii).first << ": " << (*ii).second << endl; } } Example 1