Thread: std::list question

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    29

    std::list question

    i know i shoud have read a book, but..
    i went with the internet tutorials, hoping to learn.

    i'm trying to understand how std::list works. i've found some tutorials here: http://www.cppreference.com/cpplist/all.html

    i don't know exactly what std::list is, but it looks like it's redefining the syntax some how, to allow me to use list iterators and vectors.

    i tried this:
    Code:
    int main(int argc, char *argv[]) {
    
    list<int> v;
     v.assign( 10, 42 );
     for( int i = 0; i < v.size(); i++ ) {
       printf("%d\n",v[i]);
     } 
     system("pause");
    }
    + the "using" directives, and windows, iostream, list and algorithm includes.
    i'm trying to compile this with devc++ 4.9.9.0 but it doesn't really work.

    what am i doing wrong?

    thanks guys

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >what am i doing wrong?
    Since std::list is a doubly linked list, I don't think you can use v[i] to get direct access. I think you have to traverse the list instead. Maybe:
    Code:
     for (list<int>::iterator i = v.begin(); i != v.end(); ++i)
     {
       cout << *i << endl;
     }

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    what am i doing wrong?
    lots of things, but let's start with the main issue.
    Code:
    printf("%d\n",v[i]);
    std::list is a linked list, not a random access list, so the subscript operator '[]' will not work with it.

    to iterate over an std::list you use iterators. so rewriting your code it should look like this
    Code:
    int main(int argc, char *argv[])
    {
        typedef list<int> IntList; // makes life easier later
        IntList v;
        v.assign( 10, 42 );
    
        // create an iterator and increment it until it reaches the end of the list
        for(IntList::iterator iter = v.begin(); iter != v.end(); ++iter )
        {
            // printf?!? This is C++, boy! we don't use no stinking printf! :-)
            cout << *iter; // *iter dereferences the iterator (kinda like a pointer)
        } 
    
        // system("pause"); ugh, badness
        cin.ignore();
        cin.get();
    }
    edit: damn you swoopy! stealing my thunder
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    i tried
    Code:
    cout << *i << "\n"
    , since it says endl is not declared.

    but i get this:

    In function `int main(int, char**)':
    invalid type argument of `unary *'

    am i really silly to get this error, or should i link some other libs?

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    what's the thunder, lol? ^^

    till now, thanks for your time, dudes

    i tried your last example, chaos, but cin.ignore and cin.get fail. removed them, and now i get

    Code:
     C:\Devcpp5\Proiecte\list ex\main.o(.text+0x17) In function `ZnwjPv': 
      [Linker error] undefined reference to `std::string::size() const' 
      [Linker error] undefined reference to `std::string::operator[](unsigned int) const' 
      [Linker error] undefined reference to `std::string::operator[](unsigned int) const' 
      [Linker error] undefined reference to `std::string::operator[](unsigned int) const'
    uh. i hate this. c++ looks easy in theory and really hard in practice..

    and, gah, i was wanting to learn opengl, and sdl_net :|

    izuael

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    My dad told me that one of the most difficult things in C++ can be including the right header.

    Are you including the libraries you need? So far I know that you're using
    <iostream>, <list> and <string>

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    heh, i guess he was right.

    well, i don't know. i'm not an expert in C, i have around a week or so. what exactly should i include?

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    izuael,

    I suggest you post you whole program. Then, maybe we can figure-out why simple things like endl and cin.get() are not working for you... By any chance, did you type end-one instead of end-L? It's a lower case 'L' as in end-line.

    You may not be quite ready for linked lists. Linked lists and the STL (standard template library) are an intermediate to advanced topic.

    uh. i hate this. c++ looks easy in theory and really hard in practice..

    and, gah, i was wanting to learn opengl, and sdl_net :|
    Yeah, there is a lot to the C++ language. If you were taking C++ in college, you would take one or two full-semesters of standard C++ before moving-on to graphics or some other advanced-special topic. And, the STL would probably be covered in the 2nd semester...

    C++ is NOT EASY! You can't even find a book that covers the entire language (except the language standard itself), and I'm not even talking about non-standard libraries for GUI, graphics, sound, etc.

    Most tutorials make it seem easy because they are very short and condensed. A beginning C++ book will cover about the same material as the C++ Made Easy tutorial, but it will cover the topics in more depth and detail. For example, Accelerated C++ is about 350 pages, and Teach Yourself C++ In 21 Days is about 750 pages.
    Last edited by DougDbug; 10-17-2006 at 07:32 PM.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    i've not taken any c++ in college, i'm more of an amateur programmer. i learnt php and vb by myself, thought c++ couldn't be too hard.

    i see now that it has completely different princples and syntax, that's why i'm mistaking in the firstplace.

    i don't think i made any mistakes, since i just copy-pasted, in the hope that it would compile. i genneraly copy-paste, compile, and latter analyze and make my own stuff out of examples.

    here is the current code:
    Code:
    #include <windows.h>
    #include <stdio.h>
    
    // Standard libraries (STL)
    #include <iostream>                // cin, cout, cerr etc.
    #include <list>                        // linked list
    #include <string>                // standard string class
    
    // using directives to save on typing
    using std::cout;
    using std::cerr;
    using std::string;
    using std::list;
    // could use this instead if it makes you feel better
    // using namespace std;
    
    int main(int argc, char *argv[])
    {
        typedef list<int> IntList; // makes life easier later
        IntList v;
        v.assign( 10, 42 );
    
        // create an iterator and increment it until it reaches the end of the list
        for(IntList::iterator iter = v.begin(); iter != v.end(); ++iter )
        {
            // printf?!? This is C++, boy! we don't use no stinking printf! :-)
            cout << *iter; // *iter dereferences the iterator (kinda like a pointer)
        } 
    
        // system("pause"); ugh, badness
        cin.ignore();
        cin.get();
    }

  10. #10
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Are you using std::endl and std::cin.get() or just endl and cin.get()? (the more portable and prefered method by many is the std:: ... it is also required by standards if you are not doing the below.)

    or do you have a using namespace std; at the top of your file (the lame not so recommended way to fix the problem)

    EDIT:

    You posted as I was typing heh.

    endl is in the std namespace also, so in the method you are using using std::endl; and cin is also in there so... using std::cin;

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    uhm..
    so what do i have to change/remove, wraithan?

    edited: i got it, i added using std::cin;

    but now still gives these errors
    Code:
     C:\Devcpp5\Proiecte\list ex\main.o(.text+0x17) In function `ZnwjPv': 
      [Linker error] undefined reference to `std::string::size() const' 
      [Linker error] undefined reference to `std::string::operator[](unsigned int) const' 
      [Linker error] undefined reference to `std::string::operator[](unsigned int) const' 
      [Linker error] undefined reference to `std::string::operator[](unsigned int) const' 
     C:\Devcpp5\Proiecte\list ex\main.o(.text+0x148) In function `main': 
      [Linker error] undefined reference to `__gxx_personality_sj0' 
      [Linker error] undefined reference to `std::cout' 
      [Linker error] undefined reference to `std::ostream::operator<<(int)' 
      [Linker error] undefined reference to `std::cin' 
     C:\Devcpp5\Proiecte\list ex\main.o(.text+0x335) In function `Z41__static_initialization_and_destruction_0ii': 
      [Linker error] undefined reference to `std::ios_base::Init::~Init()' 
      [Linker error] undefined reference to `std::ios_base::Init::Init()'
    and definitely too many ohers too post.
    uugh. this is getting really hard :/ i'm just trying something really basic
    Last edited by izuael; 10-17-2006 at 07:47 PM.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I think you are simply using too many headers, and perharps some other things as well. You should find this compilable. If not, I put your compiler in question.
    Code:
    #include <list>
    #include <iostream>
    
    int main ( ) // We aren't using command line arguments yet
    {
       typedef std::list<int> IntList; // typedef assigns an alias to a type
       IntList example;               // Try to use descriptive variables
       example.assign( 10, 42 );      // Creates ten nodes with the value 42
       for ( IntList::iterator idx = example.begin(); idx != example.end(); ++idx )
       {
          std::cout << *idx << " ";   // And I can prove it!
       }
       std::cout.put('\n');
       std::cin.get();                // Pause. I dont think we need ignore() right now.
    
       return 0;
    }
    I must agree with what others have said, it seems kind of early to be jumping into data structures. By all means work with what you are comfortable, but www.cppreference.com is a reference, not a tutorial. To learn, you will need something much more thorough.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Posts
    29
    i tend to agree too.
    please give me a reference to a good compiler and ide. i'm using devc++ and i've got the same error as last time. i'd preffer something that would make it easy for portable code, like not including their own standards over cpp.

    it may be my fault, by jumping in too early into heavy stuff, and even worse, by considering it may be comparable to another language.

    if you please, let me know of some (prefferably online) resource or book too, which can help me put a good foundation to my c/cpp knowledge.

    thanks.

    edit: i'll also have to add that both (the last posted code, with using std::cin and the one posted by you) compiled perfectly (and worked) on visual c++ 6.

    should i go with this, if i'll eventually want to port my apps to linux?
    Last edited by izuael; 10-17-2006 at 08:42 PM.

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Mmm, well I searched and fount this thread:
    What's a good compiler?

    Moreover: You'll find that a later version of MSVC++ is commonly recommended by others in this group. Version 6 is particularly bad; I keep reading about people who have to hack around the standard library. I don't know what's up with Dev-C++ either. Maybe you're using an older version, but Dev-C++ has been dead in development for a while now anyway.

    C++ Book Recommendations. In particular, Eckel's Thinking in C++ is available online, but I never read it.

    In regards to porting to Linux, if you're writing apps according to the Standard, they run almost everywhere.

  15. #15
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Thinking in C++ is awesome, although it is not for the new beginners, it does have a lot of valuable information. It assumes you know C atleast decently but I have no C background and just C++ and found it very useful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. std::list question
    By void_main in forum C++ Programming
    Replies: 5
    Last Post: 11-10-2003, 01:21 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM