|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
The same problem arises with pointers. Stack<char *> and stack<unsigned char *> are two distinct entities. Similarly, derived classes and base classes are considered distinct types for template specializations. You can imagine how easy it is to bloat your code inadvertently. To avertor at least minimizeunnecessary reduplication of template code, you should try to use a common denominator type. A single vector specialization is sufficient for short, unsigned short, int, and so on. When you pass arguments of different integral types to a function template, try to cast the arguments to a uniform type first. This will ensure that only a single specialization is instantiated. 7.3 Modify a single element of a container?Problem I know now how to insert elements into a vector and how to read their values. However, I need to modify the value of an existing element in a vector. Technique As mentioned before, a vectors element can be accessed using operator [] or the at() member function. Both return a non-const reference to an element of a vector. Also, they can be used to modify the elements value. Steps
How It Works Let us begin by examining the three #include statements in the beginning of the source file: #include <vector> #include <iostream> #include <string> The first statement includes the definition of class vector. The second includes the definitions of iostream class library and the third includes the definition of the string class. Now lets look at the first source line inside main(). In this line, an object of type vector<string> named vs is instantiated: vector <string> vs; The following line declares and initializes a string object. The initial value stored in the string is the immortal hello world. string temp = hello world; Then, the first element is inserted into vs using the member function push_back(). The element inserted is the string object temp that you instantiated in the previous source line. vs.push_back(temp); //insert first element The following two lines display the value of the first element of vs. The subscript operator is used first, and then the at() member function: cout<<first element using []: << vs[0] <<endl; //using subscript operator cout<<first element using at(): << vs.at(0) <<endl; //using at() //member function Having examined the first element, now change its value. The following statement modifies the value of the temp string: temp = hello STL; Next, assign a new value to the first element of vs using the overloaded subscript and assignment operator, exactly as a new value is assigned to an element in a built-in array: vs[0] = temp; //modify the value of the first element of vs To see the effect of the assignment, reexamine the value of the first element: cout<<first element modified using []: << vs[0] <<endl; Now modify the value of the first element again, but this time use the member function at() instead of the overloaded subscript operator. As in the previous modification, first assign a new value to temp: temp = hello vector; The actual modification of the first member of vs takes place in the following statement: vs.at(0) = temp; Again, to see the effect of the assignment, reexamine the value of the first element. This time, use the member function at(): cout<<first element modified using at(): << vs.at(0) <<endl;
|
![]() |
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.
|