Thread: Two questions involving STD Lists

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    9

    Two questions involving STL Lists

    In my game, i use a list for my entities, among other things.

    my first question is... is it possible to declare an extern list? i want to be able to access my list of entities from anywhere in my code (including other source files). example...

    in main.cpp....
    Code:
    list <CBaseEntity *> entities;
    blalbbllah
    blahblah
    blah
    in say, weapons.h....
    Code:
    extern ????????? entities;
    and now in weapons.cpp.....
    Code:
    object_thingy *object = new Object();
    entities.push_back(object);

    and finally my second question, how would i go about getting the total number of elements in a list? for example, i want to check if a list contains over 200 elements, and then delete everything past 200 to save on memory.
    Last edited by IonBlade; 12-09-2003 at 05:15 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You mean STL lists, right?

    1) No clue.
    2) Use the size( ) method to get the size, then you can use an iterator to get to the 200th element, then use the erase( ) method.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by IonBlade
    and finally my second question, how would i go about getting the total number of elements in a list? for example, i want to check if a list contains over 200 elements, and then delete everything past 200 to save on memory.
    I'm not sure a list is the best option if you need to iterate to find the 200th element. You'll probably want to access that 200th element randomly - meaning you won't need to loop through all 199 elements before it. I don't know of any efficient way to lop off all elements after 200 in a list container without navigating your way to the 200th element first.

    A better container might be a deque or a vector. You can get an iterator to the 200th element simply by calling at(200) (technically that would be the 201st element). You can then use that iterator with erase/remove. You can push_back on to a deque or vector just like a list, but you cannot insert an element into the middle as efficiently as you can with a list. However, if you don't do any inserts, only push_back, then deque or vector is for you. Or if you use push_front, then deque (not vector) might be better.

    As far as your first question, I don't know any reason why it wouldn't be possible. Are you having problems with it?

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    9
    yeah lol, STL. STD? god now i feel like an idiot damn abbreviations, SDL, STL, STD, same thing..

    As far as your first question, I don't know any reason why it wouldn't be possible. Are you having problems with it?
    i'm just not sure of the syntax. and yes i have tried and i got errors.

    Code:
    extern list entities;
    extern list *entities;
    extern list<CBaseEntity *> entities;
    i have tried all those, and get "syntax error before ';' ".

    sorry if i sound noobish, but i'm just starting with the STL. and that list of 200 elements is what i'm using to keep a console/message log, it's just a list of char strings, and i want to delete past messages when about 200 exist.
    Last edited by IonBlade; 12-09-2003 at 05:19 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    extern works fine with stl, the only problems you might find is forgetting to actually declare it somewhere, or more than one place. Both don't show up utill linkage though. I would suggest
    Code:
    #ifndef CONLIST_H
    #define CONLINST_H
    #include<deque>
    #include<string>
    typedef std::deque<std::string> conlist_t;
    extern conlist_t entities;
    #endif
    
    //in one, and only one cpp file
    #include"conlist.h"
    conlist_t entities;
    and just let entities expand to it's little hearts desire. If 200 lines is too much to deal with from a user standpoint then wrap deque around your own class that has a limit that when you add a line and size() > limit it pop_front()s when it push_back()s the new line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Why std:: and not using namespace std?
    By swappo in forum C++ Programming
    Replies: 4
    Last Post: 07-03-2009, 03:10 PM
  3. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. questions questions questions.....
    By mfc2themax in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 08-14-2001, 07:22 AM