home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


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 avert—or at least minimize—unnecessary 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 vector’s 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 element’s value.

Steps

1.  Change to your base source directory and create a new subdirectory named VECTOR_MODIFICATION.
2.  Start your source code editor and type the following code into a file named MAIN.CPP.
// demonstrating how to modify the
// value of a vector’s element

#include <vector>
#include <iostream>
#include <string>
using namespace std;
void main()
{
  vector <string> vs; //instantiate a vector of string objects
  string temp = “hello world”; //instantiate a string object and
                                         //initialize it
  vs.push_back(temp); //insert first element to vs
  cout<<”first element using []: “<< vs[0] <<endl; //using
                                                  //subscript operator
  cout<<”first element using at(): “<< vs.at(0) <<endl; //using
		       //at() member function
  temp = “hello STL”;  //assign a new value
  vs[0] = temp; //modify the value of the first element of vs
  cout<<”first element modified using []:  “<< vs[0] <<endl;
  temp = “hello vector”;
  vs.at(0) = temp; //assign a new value to the first element of vs
  cout<<”first element modified using at(): “<< vs.at(0) <<endl;
}//end main
3.  Save MAIN.CPP. Next, compile and link MAIN.CPP.
If your compiler complains about the following #include statements:
#include <vector>
#include <iostream>
#include <string>

comment out (or remove) the using directive just below these #include statements and change them to read:
#include <vector.h>
#include <iostream.h>
#include <string.h>
4.  Compile and link the program.
5.  Run the program; the output should be as follows:
first element using []: hello world
first element using at(): hello world
first element modified using []: hello STL
first element modified using at(): hello vector

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 let’s 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;


Previous Table of Contents Next


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.