Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ stl vector (http://www.go4expert.com/forums/cpp-stl-vector-t27963/)

coolroose 6Mar2012 16:19

C++ stl vector
 
Please help me with this question, data structure in c++

Code:
/* Use a foward iterator for an STL vector on ints. Populate the vector with 10 ints&display the content of the vector.
Next zero the vector entries&display the vector again.*/

dearvivekkumar 7Mar2012 11:31

Re: C++ stl vector
 
Code:

/*
 * Includes
 *
 */
#include <iostream>
#include <vector>

using std::vector;
using std::cout;
using std::endl;

int main(int argc, char* argv[])
{
  vector<int> myIntVector;
        size_t i = 0;
        for(i = 0; i < 10; i++){
                myIntVector.push_back(i + 100);
        }

        vector<int>::iterator it;
        i = 0;
        for(it = myIntVector.begin(); it < myIntVector.end(); ++it) {
                cout << "myIntVector[" << i++ << "] = " << *it << endl;
        }

        for(it = myIntVector.begin(); it < myIntVector.end(); ++it) {
                *it = 0;
        }

        cout << "\n\nDisplaying vector after setting all its element to 0\n";
        i = 0;
        for(it = myIntVector.begin(); it < myIntVector.end(); ++it) {
                cout << "myIntVector[" << i++ << "] = " << *it << endl;
        }

        return 0;
}


coolroose 8Mar2012 15:27

Re: C++ stl vector
 
Run great!
Thanks


All times are GMT +5.5. The time now is 23:22.