stl list interface

Aug 13, 2008 at 10:22pm
Hi all,

Very stupid question, but I cant seem to get around it, a little help would be appreciated.

I am reading threads/semaphores and have come across some code which needs modification, I have gotten through some areas (in terms of understanding), where I need help is the following :

there is global code in the beginning as follows:
 
  MyList= new list<int>;


The program asks the user for the size of the list, say for example 20, so here is the question: How do i get to set the size of the list to 20 (or more better, how do i set up the list to use 20?)

below is the full code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void* myfunc(void* p)
{

  
  // I know this is where the code goes for inserting items BUT HOW? 


  //after items are in the list  
  for(int i = 1; i <= limit; i++)
    {
      sem_wait(&slot_avail);
      buffer.push_back(i);
      sstr.clear(); 
      sstr.str("");
      sstr << "Producer inserted item " << i;
      DEBUG('p', sstr.str());
      sem_post(&item_avail);
    }


  return 0;
}



As always, help is always appreciated :)
Aug 13, 2008 at 10:40pm
the STL list has no limit. It is implemented sort of like a Linked List. In which each element points to the previous element and the subsequent element. It does this internally.

Any limit you wish to impose on this list, will have to be done by you, by keeping count of the elements in the list (list.size()) and not allowing anyone to push any more items onto the list if it's size matches whatever limit you put into place.

also, if you make the list with
MyList = new list<int>;

be sure to delete the list when you're done with it.
delete MyList;

Edit: Has no limit except perhaps by the amount of free system memory
Last edited on Aug 13, 2008 at 10:40pm
Aug 13, 2008 at 10:47pm
so if I say buffer.size = 20 is that correct? It gave me an error on this...

I just need to set the list size to 20 how do I do that?
Aug 14, 2008 at 12:20am
no.. you want to get the size, not set it. Lists again don't have a limit

so you can find out how big the list is by saying
int mysize = buffer.size();

then if you want to limit it, if mysize is 20.. and you don't want to add any more than 20.. then when someone tries to add another, just return an error.
Aug 14, 2008 at 12:34am
An STL list is actually a linked-list, so its size is limited only by how much memory you have.

You can change the number of nodes actually allocated using the resize() method:
1
2
std::list <int> MyList;
MyList.resize( 20 );

See here for more http://www.cplusplus.com/reference/stl/list/

Hope this helps.
Aug 14, 2008 at 6:58am
People are talking about linked lists, but in case you don't know what a linked list is, basically all you need to know is that it resizes itself automatically as you insert and remove items from it.
You are probably used to normal arrays where you must specify a size. STL containers are not like this, you do not need to specify a size. They start out empty, and you can add as much stuff as you'd like. If you ever need to know how big it is currently, you can call MyList.size().

Assuming limit is the number of things you are inserting into your list, it may look something like this:

[code=c++]
int num;
for( int i = 0; i < limit; i++ ) {
cout << "Enter a number: ";
cin >> num;
MyList.push_back( num );
}
[/code]

After you get this working, it might be worthwhile to read up on linked-lists to get an understanding of how the list class can do this without you specifying a set size, it is a fairly fundamental first step in learning about data structures.
Aug 14, 2008 at 2:23pm
@Duoas/@mahlerfive,

Thanks very much for your answers and links.

Topic archived. No new replies allowed.