Using Standard Template Library Components

 < Day Day Up > 



This section will demonstrate the use of several STL components and how iterators are used to manipulate container elements. Along the way you will be introduced to several important functions shared by all STL container components that make using iterators to manipulate container elements easy. Additionally, the use of several STL algorithms will be demonstrated. Essentially, once you know how to use one STL container you know how to use them all. Your job then is to determine what STL component best suits your particular need.

Using vector

A vector provides the functionality of an array with the additional functionality of being dynamically sizeable. Since it is a class template, it can be used to store just about any element type. I will caution that how a vector behaves with different types, especially pointers, is a function of the quality of the version of the STL library that ships with your compiler. If you have a fairly recent implementation of the C++ STL everything should work fine. Again, consult your compiler's documentation.

Example 15.13 gives a main() function showing a vector of floats being declared and utilized like an ordinary array.

Listing 15.13: main.cpp

start example
  1  #include <iostream>  2  using namespace std;    3  #include <vector>  4  5  int main(){  6       vector<float> v(10);  7  8       for(int i=0; i<10; i++){  9            v[i] = ( i + .25); 10       } 11 12       for(int i=0; i<10; i++){ 13            cout<<v[i]<<endl; 14       } 15 16       return 0; 17  }
end example

The output from this program is shown in figure 15-8.


Figure 15-8: Results of Running Example 15.13

Notice how the vector v is accessed using the subscript operator. Example 15.14 shows the same vector being utilized in a slightly different fashion.

Example 15.14: main.cpp

start example

click to expand

end example

Compare the difference between the declaration of the vector object in example 15.13 and that of Example 15.14. In example 15.13, the vector object v was given a size of 10 whereas in example15.14 no size was given. On line 9 in example 15.14 the push_back() function is used to insert elements at the end of the array. In the for statement shown on line 12 the size() function is used to determine the number of elements managed by the container.

The for statement on line 17 shows how an iterator is declared and utilized to iterate over the vector container elements. The begin() function returns an iterator pointing to the first element in the container. The end() function returns the iterator of one past the last element. Each container element is then accessed by dereferencing the iterator as shown in the body of the for statement. Figure 15-9 shows the results of running example 15.14.


Figure 15-9: Results of Running Example 15.14

Notice that both versions of the for statement produce the same results.

Example 15.15 shows the sort algorithm being used on a vector object.

Listing 15.15: main.cpp

start example
  1  #include <iostream>  2  using namespace std;  3  #include <vector>  4  #include <algorithm>  5  6  int main(){  7       vector<int> v;  8  9       v.push_back(25); 10       v.push_back(278); 11       v.push_back(57); 12       v.push_back(82); 13       v.push_back(2); 14       v.push_back(29); 15       v.push_back(485); 16       v.push_back(123); 17       v.push_back(1); 18       v.push_back(10); 19 20       for(int i = 0; i<v.size(); i++){ 21           cout<<v[i]<<" "; 22       } 23       cout<<endl; 24 25       sort(v.begin(), v.end()); 26 27       for(vector<int>::iterator i = v.begin(); i != v.end(); ++i){ 28           cout<<*i<<" "; 29       } 30 31       return 0; 32  } 33 
end example

In this example, the push_back() function is used to load a vector object v with ten wildly random integers plucked from the depths of my brain. (Randomness not guaranteed!) The first for statement prints the contents of the vector to the console in their insertion order. The sort algorithm function is called on line 25. It takes an iterator that points to the first element to be included in the sort and an argument denoting one element past the last element to be sorted. The proper iterators are obtained by using the vector object's begin() and end() functions. The for statement on line 27 then prints the now-sorted vector elements. Figure 15-10 shows the results of running example 15.15.


Figure 15-10: Results of Running Example 15.15

Using list

Example 15.16 gives a main() function showing an STL list component in action.

Listing 15.16: main.cpp

start example
  1  #include <iostream>  2  using namespace std;  3  #include <list>   4  5  int main(){  6       list<int> l;  7  8       l.push_back(82);  9       l.push_back(25); 10       l.push_back(63); 11       l.push_back(8); 12       l.push_back(75); 13       l.push_back(12); 14       l.push_back(23); 15       l.push_back(129); 16       l.push_back(273); 17       l.push_back(1); 18 19       for(list<int>::iterator i = l.begin(); i != l.end(); i++){ 20            cout<<*i<<" "; 21       } 22       cout<<endl; 23 24       l.sort(); 25 26       for(list<int>::iterator i = l.begin(); i != l.end(); i++){ 27           cout<<*i<<" "; 28       } 29       cout<<endl; 30 31       return 0; 32  }
end example

Two important comparisons can be made between the use of a list as compared to a vector. First, the interface to each container is similar by design. The push_back() function works on both. Second, their contained elements are manipulated in a uniform manner by using iterators.

Third, the sort algorithm does not work on lists. Notice that the sort() function called on line 24 is actually a list member function, not a stand-alone algorithm. Figure 15-11 shows the results of running this program.


Figure 15-11: Results of Running Example 15.16

Quick Review

Container, iterators, and algorithms all work together to add ready-made functionality to your programs. Getting to know the STL can save you a lot of otherwise wasted effort expended in trying to replicate what is already available.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net