list first element

This is a discussion on list first element within the C++ Programming forums, part of the General Programming Boards category; I have a problem with assigning 'a' in with the following code: int *a; list<int> new_list; new_list.push_back(5); a=new_list.front(); can anyone ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    list first element

    I have a problem with assigning 'a' in with the following code:
    int *a;
    list<int> new_list;
    new_list.push_back(5);
    a=new_list.front();

    can anyone tell me how can i store the first element from the list in my variable a?

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    38
    That is because new_list.front() returns the first element in the list, not a pointer. new_list.front() is equivalent to *(new_list.begin()). You should check out the STL to make sure you understand what STL functions return before you use them. Here is a great link:
    http://www.sgi.com/tech/stl/table_of_contents.html

    the line should be *a = new_list.front(), or you can get rid of the pointer variable, or you could use an iterator, would would probably much more useful, although i do not know what you intended to do with this variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  3. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 06:37 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21