Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) StdSet_differenceExample1

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdSet_differenceExample1/CppStdSet_differenceExample1.pro

 

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

include(../../Libraries/Boost.pri) #Or use the code below
# win32 {
#   INCLUDEPATH += \
#     ../../Libraries/boost_1_54_0
# }

SOURCES += main.cpp

 

 

 

 

 

./CppStdSet_differenceExample1/main.cpp

 

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

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic pop

int main()
{
  //v contains all values from zero to ten
  const std::vector<int> v { 0,1,2,3,4,5,6,7,8,9 };

  //w contains all squares from 1 to an including 25
  std::vector<int> w { 1,4,9,16,25 };

  assert(std::is_sorted(v.begin(),v.end())
    && "std::set_difference expects a sorted container");
  assert(std::is_sorted(w.begin(),w.end())
    && "std::set_difference expects a sorted container");

  //x is the difference of v and w:
  //that is, the elements in v, that are
  //not in w
  std::vector<int> x;

  std::set_difference(v.begin(),v.end(),w.begin(),w.end(),
    std::back_inserter(x));

  //so x contains all non-squares between zero to ten
  assert(x.size() == 7);
  assert(x[0] == 0);
  assert(x[1] == 2);
  assert(x[2] == 3);
  assert(x[3] == 5);
  assert(x[4] == 6);
  assert(x[5] == 7);
  assert(x[6] == 8);

  //Show x on screen
  std::copy(x.begin(),x.end(),std::ostream_iterator<int>(std::cout," "));
  std::cout << '\n';
}

/* Screen output

0 2 3 5 6 7 8

*/

 

 

 

 

 

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