Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) StdPartitionExample1

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdPartitionExample1/CppStdPartitionExample1.pro

 

exists(../../ConsoleApplication.pri) {
  include(../../ConsoleApplication.pri)
}
!exists(../../ConsoleApplication.pri) {
  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

 

 

 

 

 

./CppStdPartitionExample1/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);

  //Partition the std::vector in primes and non-primes
  typedef std::vector<int>::iterator Iterator;
  const Iterator p_end = std::partition(v.begin(),v.end(),IsPrime);

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

/* Screen output:

Primes: 1 2 3 7 5
Non-primes: 6 4 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