PDA

View Full Version : c++ <list>


rego
05-16-2007, 02:12 PM
Hi there,

wonder if anyone can help :)

here is my code

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <list>


using namespace std;
using std::list;

typedef list <int> IntList;

void DisplayList(IntList pipeline)
{
for (IntList::const_iterator i = pipeline.begin(); i != pipeline.end(); i++ )
{
cout << *i << " ";
}
}

void PrepList(IntList pipeline)
{
srand( time(NULL) );
int num;
for(int i = 0; i < 3; i++)
{
num = rand() % 7;
pipeline.push_back(num);
}
}


int main()
{
IntList pipeline;

PrepList(pipeline);

DisplayList(pipeline);// <<<<< NEED THIS TO PRINT THE LIST VALUES

system("PAUSE");
return 0;
}

I just need some one to point me in the right direction regarding how to change PrepList into a function which can return a <list> type for main() to print out the list (if that's even possible) or any way in which this can be done

To recap i need to be able to create a random list in a function and then display it in the main part of the program using a display function.

At the moment the PrepList function is of type void which is obviously wrong :)

Many Thanks!
Rego

dave_
05-16-2007, 03:07 PM
instead of IntList pipeline
type PrepList(IntList &pipeline)
and DisplayList(const IntList &pipeline) look at http://en.wikipedia.org/wiki/Reference_(C++)




you could alternatively use pointers

rego
05-17-2007, 12:47 AM
thank you, i'll try this when i get home :)

rego
05-18-2007, 11:32 AM
instead of IntList pipeline
type PrepList(IntList &pipeline)
and DisplayList(const IntList &pipeline) look at http://en.wikipedia.org/wiki/Reference_(C++)




you could alternatively use pointers


just tried it and it works like a charm! :) thanks again

t0rakka
06-26-2007, 12:01 AM
FYI; _why_ it works is because you did pass by value - a local copy (in the stack) of the list was being modified. >B)