Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) StdPartition_copyExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdPartition_copyExample1/CppStdPartition_copyExample1.pro

 

include(../../ConsoleApplication.pri) #Or use the code below
# QT += core
# QT += gui
# CONFIG   += console
# CONFIG   -= app_bundle
# TEMPLATE = app
# CONFIG(release, debug|release) {
#   DEFINES += NDEBUG NTRACE_BILDERBIKKEL
# }
# QMAKE_CXXFLAGS += -std=c++1y -Wall -Wextra -Weffc++
# unix {
#   QMAKE_CXXFLAGS += -Werror
# }

SOURCES += main.cpp

 

 

 

 

 

./CppStdPartition_copyExample1/main.cpp

 

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <iterator>
#include <vector>

//From http://www.richelbilderbeek.nl/CppIsPrime.htm
bool IsPrime(const int x)
{
  const int max
  = static_cast<int>(
      std::sqrt(static_cast<double>(x))
    ) + 1;

  for (int i=2; i!=max; ++i)
  {
    if (x % i == 0) return false;
  }

  return true;
}

int main()
{
  std::vector<int> v;
  for (int i=1; i!=10; ++i) v.push_back(i);

  std::vector<int> primes;
  std::vector<int> non_primes;

  //Partition the std::vector in primes and non-primes
  std::partition_copy(
    v.begin(),
    v.end(),
    std::back_inserter(primes),
    std::back_inserter(non_primes),
    IsPrime
  );

  std::cout << "Primes: ";
  std::copy(std::begin(primes),std::end(primes),std::ostream_iterator<int>(std::cout," "));
  std::cout << "\nNon-primes: ";
  std::copy(std::begin(non_primes),std::end(non_primes),std::ostream_iterator<int>(std::cout," "));
  std::cout << "\n";
}

/* Screen output:

Primes: 1 2 3 5 7
Non-primes: 4 6 8 9
Press <RETURN> to close this window...

*/

 

 

 

 

 

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