Thread: pop_back() function in the vector class

  1. #1
    Vector Man
    Guest

    pop_back() function in the vector class

    does this function pop out the top value in a vector? how do i pop out the top value and store it in another variable? it doesn't work when i do this:

    x = stringvector.pop_back();

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    how about just pop()?

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Vectors don't have a pop() function. Why not just use the [] operator? If you want the first element it'd be vectorname[0], if you want the last element it'd be vectorname[vectorname.size()];

  4. #4
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537

    Re: pop_back() function in the vector class

    Originally posted by Vector Man
    does this function pop out the top value in a vector? how do i pop out the top value and store it in another variable? it doesn't work when i do this:

    x = stringvector.pop_back();
    Check the syntax of the pop_back function, its possible you have to pass a variable reference in rather than returning one.
    Couldn't think of anything interesting, cool or funny - sorry.

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    It supports and erase() method:

    vector::size_type size;
    size = svec.size();

    svec.erase(size);

    But you should also probably be using iterators to identify the type.
    Last edited by Troll_King; 09-03-2002 at 04:23 AM.

  6. #6
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Some function definitions:
    Code:
    void pop_back();
    
    reference back();
    const_reference back() const;
    //The member function returns a reference to the last element of the controlled sequence, which must be non-empty.
    vector's pop_back()'s return type is void. back() returns a reference to the last element, so just use that before you pop_back()

    here's a list of all the functions in vector:
    Code:
    vector
    allocator_type ˇ assign ˇ at ˇ back ˇ begin ˇ capacity ˇ clear ˇ 
    const_iterator ˇ const_reference ˇ const_reverse_iterator ˇ 
    difference_type ˇ empty ˇ end ˇ erase ˇ front ˇ get_allocator ˇ 
    insert ˇ iterator ˇ max_size ˇ operator[] ˇ pop_back ˇ push_back ˇ 
    rbegin ˇ reference ˇ rend ˇ reserve ˇ resize ˇ reverse_iterator ˇ 
    size ˇ size_type ˇ swap ˇ value_type ˇ vector
    just look at 'vector' in MSVC or check out this link at dinkumware:
    http://dinkumware.com/manuals/reader...&h=vector.html

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Originally posted by Eibro
    Vectors don't have a pop() function. Why not just use the [] operator? If you want the first element it'd be vectorname[0], if you want the last element it'd be vectorname[vectorname.size()];
    Code:
    vectorname[vectorname.size()-1];  //Or my program is buggier than I think
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Originally posted by Eibro
    Vectors don't have a pop() function. Why not just use the [] operator? If you want the first element it'd be vectorname[0], if you want the last element it'd be vectorname[vectorname.size()];
    d'oh... i was thinking of a queue

  9. #9
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Sang-drax
    Code:
    vectorname[vectorname.size()-1];  //Or my program is buggier than I think
    I do that all the time. I know better, yet I still do it. That's it, i'm outta here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM