Sie sind auf Seite 1von 4

C++ Tips Article 3

Aether Lee Hachioji, Japan Abstract

In this article, taking vector as an example, we will take a glance of how to utilize containers with assistance of iterators and algorithms in C++. With these examples, distinguishing features of C++ are introduced.

1. Using vector instead of an array


In short, use a vector any time you want to deploy an array.

Example 3-1. Deploying a vector of integers. #include <iostream> #include <vector> using namespace std; int main() { vector<int> intArray; intArray.push_back(10); intArray.push_back(20); intArray.push_back(30); for (int i=0; i<intArray.size(); i++) cout << intArray[i] << endl; return 0; }
Unlike the conventional way of declaring an array, size is not a necessary parameter when initiating a vector. A vector can grow freely in size. The function push_back adds the new element to the end of the vector, and increases the size by 1. To have a similar freegrowing array in C, there is more to take care of, such as memory allocation and pointer assignment. The container vector is much safer.

Aether Lee, C++ Tips Article 3. 25-Jan-10

In the example above, we put int in the angle brackets after vector declaration to make it a sequence of integers. Similarly, we can also define a vector of doubles with double, a vector of strings with string, or even a vector of other type of classes.

2. Using iterators
In example 3-1, an integer i is temporarily defined in the for loop as an index to iterate over each element in the vector. Iterators can be used to perform the same task.

Example 3-2. Using iterators to loop over all the elements #include <iostream> #include <vector> using namespace std; int main() { vector<int> intArray; intArray.push_back(10); intArray.push_back(20); intArray.push_back(30); for (vector<int>::iterator it = intArray.begin(); it < intArray.end(); ++it) cout << *it << endl; return 0; }
For standard containers, several iterators are defined. Here we see begin() and end(). They return iterators which point to the first element and the end of the container (the position past the last element) respectively. In the line of cout, the concept of an iterator is implied: it is similar to a pointer. The indirection operator * can also be used to dereference an iterator and get the content which it points, and the prefix increment operator ++ is used to advance the iterator.

3. Using algorithms
With iterators, manipulation of elements in containers can be done with algorithms. Algorithms are function templates that work with iterators. In the following example 3-3, the previous example is re-written with an algorithm.

Example 3-3. Using an algorithm. #include <iostream> #include <vector> #include <algorithm>

Aether Lee, C++ Tips Article 3. 25-Jan-10

using namespace std; void display(int item) { cout << item << endl; } int main() { vector<int> intArray; intArray.push_back(10); intArray.push_back(20); intArray.push_back(30); for_each(intArray.begin(), intArray.end(), display); return 0; }
The algorithm used here is for_each, it iterates the elements from the first to the last which are pointed by the iterators begin() and end(), and feeds the value of each element to the function display. As shown in the example, for_each is predefined in the standard library <algorithm>. The library contains a rich set of algorithms, such as counting, searching, comparison, sorting and more. The following example uses copy to do the same thing as in the previous example.

Example 3-4. Using another algorithm. #include #include #include #include <iostream> <vector> <algorithm> <iterator>

using namespace std; int main() { vector<int> intArray; intArray.push_back(10); intArray.push_back(20); intArray.push_back(30); copy(intArray.begin(), intArray.end(), ostream_iterator<int>(cout, "\n")); return 0;

Aether Lee, C++ Tips Article 3. 25-Jan-10

}
We use the copy algorithm to copy items from begin() to end() to the standard output cout designated by an output stream iterator ostream_iterator which is also a template.

4. References
Ray Lischner, C++ in a nutshell, OReilly, USA, 2003.

5. Copyright
Verigy owns the copyrights of this article

Aether Lee, C++ Tips Article 3. 25-Jan-10

Das könnte Ihnen auch gefallen