data structure C++

Thread Starter

coolroose

Joined Aug 31, 2010
16
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
 

AsmCoder8088

Joined Apr 17, 2010
15
Try this out:

Rich (BB code):
#include <stdio.h>
#include <stdlib.h>
#include <vector>

using namespace std;

int main(int argc, char *szArgv[])
{
    vector<int> data;
    vector<int>::iterator it;
    int i;

    // Populate the vector with 10 integers
    for (i=0;i<10;i++)
    {
        data.push_back(2*i+1);
    }

    // Display contents of vector using forward iterator
    printf("Populated data:\n");

    for (it=data.begin();it!=data.end();it++)
    {
        printf("%i\n", (*it));
    }

    // Zero the entries
    for (it=data.begin();it!=data.end();it++)
    {
        (*it) = 0;
    }

    // Display the contents once more
    printf("Cleared data:\n");

    for (it=data.begin();it!=data.end();it++)
    {
        printf("%i\n", (*it));
    }

    return 0;
}
 

kubeek

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

Thread Starter

coolroose

Joined Aug 31, 2010
16
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
 
Top