Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 using namespace std; int main() {vector 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 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 ::iterator i Obviously the above is for our vector of strings.

6 #include using namespace std; int main() {vector 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 ::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 ::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 using namespace std; int main() {deque 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 ::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 deque list Insert & Delete here Insert & Delete here Insert & Delete at Any position

12 #include using namespace std; void showlist (const string &str, const list &L) { list ::const_iterator i; cout << str << endl << " "; for (i=L.begin(); i != L.end(); i++) cout << *i << " "; cout << endl; } Example list program

13 int main() {list 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 ::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

15 Points to note with list The use of –void showlist (const string &str, const list &L) –list ::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;


Download ppt "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."

Similar presentations


Ads by Google