Thread: "Filtering" a buffer(a std::deque here)-Which method is better?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    "Filtering" a buffer(a std::deque here)-Which method is better?

    What I did..
    Code:
    for (auto temp :mybuffer)
    {
        if(some_condition)
             <do something with temp>
        else
              newbuffer.push_back(temp)
    }
    mybuffer = newbuffer;
    And what I thought could work better but could not find a way, without using an invalid iterator.
    1.Iterate through the extent of the original size of 'mybuffer'
    2.instead of pushing the temp 's which do not satisfy some_condition into a new buffer, push them on the back of my_buffer itself .

    Are the two ways essentially same, or is the later better(In that case, suggest an elegant way of doing so)?
    Or is there a process even better ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would consider if one or more standard generic algorithms could come in handy here. Things like std::remove_if and std:artition come to mind.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 10-26-2008, 03:59 AM
  2. what's a "dynamic buffer"?
    By MK27 in forum C Programming
    Replies: 22
    Last Post: 08-08-2008, 08:48 AM
  3. Best "Menu" method?
    By SSJMetroid in forum Game Programming
    Replies: 11
    Last Post: 12-08-2005, 12:05 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM