Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) Copy_if

 

copy_if is an algorithm that was dropped from the C++98 STL by accident, but added to the C++11 STL.

 

 

 

 

 

C++98 std::copy_if

 

std::copy_if is an algorithm that was dropped from the C++98 STL by accident.

 

Prefer algorithm calls over hand-written loops [1][2].

 

 

 

 

 

C++98 Example definition of copy_if

 

//Copy_if was dropped from the standard library by accident.
template<typename In, typename Out, typename Pred>
Out Copy_if(In first, In last, Out res, Pred Pr)
{
  while (first != last)
  {
    if (Pr(*first))
      *res++ = *first;
    ++first;
  }
  return res;
}

 

 

 

 

 

C++11 std::copy_if

 

std::copy_if is an algorithm similar to std::copy, except that a predicate can also be supplied. std::copy_if resides in the C++11 STL header file algorithm.

 

 

#include <algorithm>
#include <cassert>
#include <vector>

int main()
{
  //C++11 initializer list
  const std::vector<int> v = { 0,1,2,3,4,5,6 };

  //Only copy the even values to w
  //using C++11 lambda expression
  std::vector<int> w;
  std::copy_if(v.begin(), v.end(),
    std::back_inserter(w),
    [](const int i) { return i % 2 == 0; } );

  //Check all even values are indeed copied
  //using another C++11 initializer list
  assert(w == std::vector<int>( { 0,2,4,6 } ) );
}

 

Prefer algorithm calls over hand-written loops [1][2].

 

 

 

 

 

References

 

  1. Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 18.12.1: 'Prefer algorithms to loops.
  2. Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 43: 'Prefer algorithm calls over hand-written loops'

 

 

 

 

 

copy_if.h

 

//---------------------------------------------------------------------------
/*
Copy_if, what should have been std::copy_if
Copyright (C) 2011 Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/CppCopy_if.htm
//---------------------------------------------------------------------------
#ifndef COPY_IF_H
#define COPY_IF_H
//---------------------------------------------------------------------------
//Copy_if was dropped from the standard library by accident.
//From http://richelbilderbeek.nl/CppCopy_if.htm
template<typename In, typename Out, typename Pred>
Out Copy_if(In first, In last, Out res, Pred Pr)
{
  while (first != last)
  {
    if (Pr(*first))
      *res++ = *first;
    ++first;
  }
  return res;
}
//---------------------------------------------------------------------------
#include <string>
#include <vector>
//---------------------------------------------------------------------------
struct Copy_if_version
{
  static const std::string GetVersion()
  {
    return "2.0";
  }
  static const std::vector<std::string> GetVersionHistory()
  {
    std::vector<std::string> v;
    v.push_back("2011-xx-xx: Version 1.0: initial version");
    v.push_back("2011-06-22: Version 2.0: added versioning");
    return v;
  }
};
//---------------------------------------------------------------------------
#endif // COPY_IF_H

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml