160 likes | 280 Views
STL Data Types in C++. Vectors, lists and queues. The Standard Template Library. As we have already seen C++ supports all of the data types and structures that C offers. As well as these there is the data types in the standard template library.
E N D
STL Data Types in C++ Vectors, lists and queues
The Standard Template Library • As we have already seen C++ supports all of the data types and structures that C offers. • As well as these there is the data types in the standard template library. • These are held in a separate library from C++ and are available for many (if not most) C++ implmentations.
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector <string> v; cout << " Enter lines of text to be sorted,\n"; cout << " followed by the word stop:\n"; for (;;) {string s; getline(cin,s); if ( s == "stop") break; v.push_back(s); } //for sort(v.begin(), v.end()); cout << " The samelines after sorting: \n"; for (int i=0; i < v.size(); i++) cout << v[i] << endl; return 0; }
Vectors • Note the use of • #includes • vector <string> v declaration • v.push_back(s) method call • sort(v.begin(), v.end()) call to sort function • With vector methods as parameters • cout << v[i] << endl; access to vector elements
Using iterators • In the STL there are a special class of access types, called iterators, that act as pointers to the special classes like vectors. • These are declared with the cumbersome syntax • vector <string>::iterator i • Obviously the above is for our vector of strings.
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main() { vector <string> v; cout << " Enter lines of text to be sorted,\n"; cout << " followed by the word stop:\n"; for (;;) {string s; getline(cin,s); if ( s == "stop") break; v.push_back(s); } //for sort(v.begin(), v.end()); cout << " The samelines after sorting: \n"; for (vector <string>::iterator i=v.begin(); i != v.end(); i++) cout << *i << endl; return 0; }
Reverse iterators • It is possible to go backwards through a vector using a reverse iterator • This is declared • vector <string>::reverse_iterator i; • It would be used as follows • For (i=v.rbegin(); i != v.rend(); i++) • cout << *i << endl; • This would go backward through the vector – despite the ++ operation!
Vectors, Lists and Deques • The above are known as container adaptors. They all offer efficient means of storage and retrieval. • The simplest change to our program would have been to use a deque instead of a vector. • The following program shows how slight, in syntax that program is.
#include <iostream> #include <string> #include <deque> #include <algorithm> using namespace std; int main() { deque <string> v; cout << " Enter lines of text to be sorted,\n"; cout << " followed by the word stop:\n"; for (;;) {string s; getline(cin,s); if ( s == "stop") break; v.push_back(s); } //for sort(v.begin(), v.end()); cout << " The samelines after sorting: \n"; for (deque <string>::iterator i=v.begin(); i != v.end(); i++) cout << *i << endl; return 0; }
Vectors, lists and deques • Vectors permit data to be added/deleted at the end of the vector. Can randomly access data. • Deques permit data to be added/deleted at the beginning or end. Can randomly access data. • List can add or delete data at any position in the list. Can’t randomly access data.
Vectors, lists and deques vector <type> Insert & Delete here deque <type> Insert & Delete here list <type> Insert & Delete at Any position
Example list program #include <string> #include <iostream> #include <list> using namespace std; void showlist (const string &str, const list<int> &L) { list<int>::const_iterator i; cout << str << endl << " "; for (i=L.begin(); i != L.end(); i++) cout << *i << " "; cout << endl; }
int main() { list<int> L; int x; cout <<"Enter positive integers, followed by 0: \n"; while (cin >> x, x != 0 ) L.push_back(x); showlist("Initial list:", L); L.push_front(123); showlist("After inserting 123 at beginning: ", L); list<int>::iterator i = L.begin(); L.insert(++i,456); showlist("After inserting 456 at the second position: ", L); i = L.end(); L.insert(--i,999); showlist("After inserting 999 just before the end: ", L); i = L.begin(); x = *i; L.pop_front(); cout << "Deleted at the beginning: " << x << endl; showlist("After this deletion: ", L); i = L.end(); x = *--i; L.pop_back(); cout << "Deleted at the end: " << x << endl; showlist("After this deletion: ", L); i = L.begin(); x = *++i; cout << "To be deleted: " << x << endl; L.erase(i); showlist("After this deletion (of second element): ", L); return 0; }
Points to note with list • The use of • void showlist (const string &str, const list<int> &L) • list<int>::const_iterator i; • The member functions • push_front(), push_back(), insert() and erase() • The latter 2 being random access functions • Also note the use of • i = L.end(); x = *--i;