Vectors

This is a discussion on Vectors within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm new to C++, so please bear with me I'm trying to use the vector STL and I need ...

  1. #1
    Unregistered
    Guest

    Vectors

    Hi,

    I'm new to C++, so please bear with me

    I'm trying to use the vector STL and I need to delete an element from the vector.

    I have created an object called Book and I store them in a vector of Books (vector<Book>).

    On certain occasions, I need to delete a Book from the vector.
    I'm having trouble with this:

    The way I'm trying to do is to transfer the contents of the vector of Books a temp vector. Then I will copy the Books except for the deleted book back to the original vector.

    for example:
    (There exists a populated vector<Books> books)

    Code:
    vector<Books> temp;
    string title; //title of book to be deleted
    
    books.swap(temp);
    
    for (int i=temp.size()-1; i>=0; i--)
    {    if (temp[i].getTitle() != title)
          { books.pushback(temp.pop_back());       //ERROR
          }
          else
          {temp.popback();
          }
    }
    However, i get an error like this:
    invalid use of void expression.

    is there anyway I can do this operation?
    Thanks a lot!!

  2. #2
    Unregistered
    Guest
    There is no need to copy the items over to another vector

    http://www.cppreference.com/cppvecto...ils.html#erase

    just erase the item

    one of the benefits of vectors

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 09:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 03:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 11:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 12:13 PM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM

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