1 / 15

STL Data Types in C++

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.

makan
Download Presentation

STL Data Types in C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. STL Data Types in C++ Vectors, lists and queues

  2. 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.

  3. #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; }

  4. 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

  5. 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.

  6. #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; }

  7. 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!

  8. 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.

  9. #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; }

  10. 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.

  11. Vectors, lists and deques vector <type> Insert & Delete here deque <type> Insert & Delete here list <type> Insert & Delete at Any position

  12. 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; }

  13. 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; }

  14. 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;

More Related