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

This is a discussion on "Filtering" a buffer(a std::deque here)-Which method is better? within the C++ Programming forums, part of the General Programming Boards category; What I did.. Code: for (auto temp :mybuffer) { if(some_condition) <do something with temp> else newbuffer.push_back(temp) } mybuffer = newbuffer; ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Posts
    2,597

    "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 ?
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,639
    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-07-2005, 11:05 PM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 07: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, 06:59 AM

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