![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
7.6 Traverse through a containers elements?Problem Up until now, I have accessed a single element at a time. However, I need a convenient method for accessing all the elements in a container so I can display them on the screen. Technique Pointers are used when traversing built-in arrays. For example, the standard C routines, strcpy and strcmp, use a pointer to char that points to the first element of the char array. This pointer is incremented to move to the next char in the array. STL by its nature is a generic framework. Genericity means type independence. Built-in pointers, on the other hand, narrow the choice of supported types. For example, a pointer on most platforms is represented as a 32-bit integer. This size is insufficient for traversing a container larger than 2GB. STL, therefore, uses iterators rather than bare pointers. Iterators can be thought of as generic pointers. They behave much like pointers. You can dereference an iterator to examine the element it points to. You can use the operators ++ and -- with an iterator to move one position ahead or back in a container, and so on. Yet, the underlying representation of an iterator neednt be a pointer. The following example demonstrates how to use iterators to access a containers elements. This example will use string as its container. string resembles a vector<char>. It shares many common features with vector. However, vector is generic, whereas string is only applicable to char. string and vector are probably the most widely used STL containers. The sample program will show how to create a string object, initialize it, and iterate through its elements with iterators. Steps
How It Works The first source line in MAIN.CPP includes the definition of the iostream class library. Next, the definition of string class is included. string is declared in namespace std, therefore, the third source line contains a using directive to instruct the compiler to look up all references to string in std: #include <iostream> #include <string> using namespace std; The first source line inside main() instantiates an object, str, of type string, and initializes it with a most original value, hello world. string str = hello world; The following source line introduces us to the notion of STL iterators. Lets analyze it in detail: string::const_iterator p = str.begin(); The sequence string::const_iterator is a type name. Class string, like every container in STL, contains several types of iterators that are used to traverse its elements. Note that when you define an ordinary pointer, you have to specify to which type of object the pointer points; for example, char * is a pointer to char. In STL, you use a specialization followed by the scope resolution operator, ::, to indicate the specialization to which the iterator can be applied. Please note that string is a specialization; string is a shorthand for the following unwieldy specialization: basic_string<char, char_traits<char>, allocator<char> > Whew! Fortunately, this arcane syntax is well hidden by a friendlier typedef named string. In other words, string is a char-specialized version of the basic_string container. An ordinary pointer can be used to modify the object to which it points. When a pointer is not supposed to modify its object, declare it as pointing to a const object. For example: int strcmp (const char *, const char *); //the arguments of strcmp //may not be modified The const specifier guarantees the pointer is not used to modify the pointed object. The pointer itself, however, can be modifiedit can be assigned a new value, incremented, and so on. Likewise, there are two basic types of iterators: const_iterator and plain iterator. The rule of thumb is to use a const_iterator unless you have a good reason to use a plain iterator. This example displays the contents of a string without modifying its elements. Therefore, a const_iterator is used. The string::const_iterator is named p. It is initialized to point to the first element of str. The member function begin(), which is a member of other STL containers, as you will see next. returns the address of the first element in its object. Remember that the value returned from begin() is not the first element itself, but its address. To make this distinction clearer, compare it to built-in arrays: char car[10]; const char * p = &car[0]; //p gets the address of the first array //element, not the element itself
|
![]() |
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|