data structure C++

Discussion in 'Programmer's Corner' started by coolroose, Mar 5, 2012.

  1. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    Hi,
    Can someone help me with data structure in C++ problem:

    here is the question:

    Use a forward iterator for an STL vector on ints. Populate the vector with 10 ints.& display the contents of the vector. Next zero the vector entries & display the vector again.


    your help is very appreciate
    Thank you
     
  2. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    Try this out:

    Code ( (Unknown Language)):
    1.  
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <vector>
    5.  
    6. using namespace std;
    7.  
    8. int main(int argc, char *szArgv[])
    9. {
    10.     vector<int> data;
    11.     vector<int>::iterator it;
    12.     int i;
    13.  
    14.     // Populate the vector with 10 integers
    15.     for (i=0;i<10;i++)
    16.     {
    17.         data.push_back(2*i+1);
    18.     }
    19.  
    20.     // Display contents of vector using forward iterator
    21.     printf("Populated data:\n");
    22.  
    23.     for (it=data.begin();it!=data.end();it++)
    24.     {
    25.         printf("%i\n", (*it));
    26.     }
    27.  
    28.     // Zero the entries
    29.     for (it=data.begin();it!=data.end();it++)
    30.     {
    31.         (*it) = 0;
    32.     }
    33.  
    34.     // Display the contents once more
    35.     printf("Cleared data:\n");
    36.  
    37.     for (it=data.begin();it!=data.end();it++)
    38.     {
    39.         printf("%i\n", (*it));
    40.     }
    41.  
    42.     return 0;
    43. }
    44.  
    45.  
    46.  
     
  3. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    That' s really helpful, but it has to be in c++
    Do u have the c++ version.
    Thanks
     
  4. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    I am pretty sure that is C++. You can tell by the
    #include <vector>, using namespace std, and data.begin()
     
  5. coolroose

    Thread Starter New Member

    Aug 31, 2010
    16
    0
    Great!!
    It is running with no error. I just made a few changes #include <stdio.h> to #include <iostream> and #include <algorithm>

    I appreciated it. Thank you so much
     
Loading...