# c++ <list>

4 replies to this topic

### #1rego

Member

• Members
• 46 posts

Posted 16 May 2007 - 09: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

### #2dave_

Senior Member

• Members
• 584 posts

Posted 16 May 2007 - 10:07 PM

type PrepList(IntList &pipeline)
and DisplayList(const IntList &pipeline) look at http://en.wikipedia..../Reference_(C++)

you could alternatively use pointers

### #3rego

Member

• Members
• 46 posts

Posted 17 May 2007 - 07:47 AM

thank you, i'll try this when i get home :)

### #4rego

Member

• Members
• 46 posts

Posted 18 May 2007 - 06:32 PM

dave_ said:

type PrepList(IntList &pipeline)
and DisplayList(const IntList &pipeline) look at http://en.wikipedia..../Reference_(C++)

you could alternatively use pointers

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

### #5t0rakka

New Member

• Members
• 9 posts

Posted 26 June 2007 - 07: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)

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users