1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ stl vector

Discussion in 'C++' started by coolroose, Mar 6, 2012.

  1. coolroose

    coolroose New Member

    Joined:
    Nov 7, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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.*/
     
  2. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    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;
    }
    
    
     
  3. coolroose

    coolroose New Member

    Joined:
    Nov 7, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Run great!
    Thanks
     

Share This Page