Thread: Deque question;

  1. #1
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100

    Deque question;

    i was hopeing to use one of the STL sequences to hold my list of data for my project. But the problem is, it seems that none of them have the ability to remove a piece of data from the middle of them and then shrink.

    What i want :
    i.e. if i have 3 things in my deque, and i delete the middle one, the deque size will now only be 2.

    What i get:
    if i have 3 things, and i delete the middle, i get one NULL space in between!

    any suggestions?
    Sorry, but i'm a Code::Blocks man now.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You aren't deleting it correctly, or you are accessing the values incorrectly. Show your code. If you erase an item from the middle of a deque, vector, list or any other standard C++ container then the size will go from 3 to 2.

  3. #3
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Code:
    class Data 
    {
    ....
    void setName(string name);
    void eraseName(int pos) {Names[pos].erase();};
    void showAll();
    ....
    }
    void Data::setName(string name)
    {
        Names.push_back(name);
    }
    void Data::showAll()
    {
        for (int i = 0; i < Names.size(); i++){
            cout << i << ":\t" << Names[i] << endl;
        }
    }
    
    ....
    
    Data *mydata = new  Data();
    mydata->setName("Mark");
    mydata->setName("Cindy");
    mydata->setName("Steve");
    mydata->setName("Clide");
    mydata->eraseName(1);
    mydata->showAll();
    .......
    has the output of:

    Code:
    0:      Mark
    1:
    2:      Steve
    3:      Clide
    Sorry, but i'm a Code::Blocks man now.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You're calling erase on the wrong object.
    Assuming Names is a vector, you could do this:
    Code:
    void eraseName(int pos) { Names.erase( Names.begin() + pos ); };
    If it's a list, it won't work because it's not a random access iterator.
    A deque also has random access iterators, so that should work on deques too.
    Last edited by cpjust; 06-24-2008 at 03:47 PM. Reason: Also works with deques...

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What you are doing is not calling std::deque::erase to remove an item from the deque, but std::string::erase on the string contained at index pos with default arguments which removes all contents from the string.

    Look up usage of std::deque::erase
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User Terran's Avatar
    Join Date
    May 2008
    Location
    Nashua, NH
    Posts
    100
    Quote Originally Posted by cpjust View Post
    Code:
    void eraseName(int pos) { Names.erase( Names.begin() + pos ); };
    That did it, cool. That saves me from having to write my own linked list for my application!
    Sorry, but i'm a Code::Blocks man now.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Or:
    Code:
    Names.erase(pos);

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by swoopy View Post
    Or:
    Code:
    Names.erase(pos);
    But erase() takes an iterator, not an int: http://www.cppreference.com/cppdeque/erase.html

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >But erase() takes an iterator, not an int: http://www.cppreference.com/cppdeque/erase.html
    Yes, you're right cpjust. For some reason, the name pos for the parameter made me think position (as in index). And I also assumed the two argument version was for iterators. But now I see both are.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deque best practices
    By George2 in forum C++ Programming
    Replies: 10
    Last Post: 03-02-2008, 08:11 PM
  2. deque
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 02-29-2008, 01:35 AM
  3. Using DEQUE within Class Definition
    By wbeasl in forum C++ Programming
    Replies: 8
    Last Post: 10-07-2007, 09:50 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM