Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) StdUniqueExample3

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppStdUniqueExample3/CppStdUniqueExample3.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

 

 

 

 

 

./CppStdUniqueExample3/main.cpp

 

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

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#include <boost/shared_ptr.hpp>
#pragma GCC diagnostic pop

const std::vector<boost::shared_ptr<int>> Create()
{
  const boost::shared_ptr<int> a { new int(1) };
  const boost::shared_ptr<int> b { new int(2) };
  std::vector<boost::shared_ptr<int>> v {
    a,b,a,a,b,b,a,a,a,a,b,b,b,b
  };
  return v;
}


int main()
{
  {
    std::vector<boost::shared_ptr<int>> v { Create() };
    assert(std::count(v.begin(),v.end(),nullptr) == 0);
    std::sort(v.begin(),v.end());
    assert(std::count(v.begin(),v.end(),nullptr) == 0);
    v.erase(std::unique(v.begin(),v.end()),v.end());
    assert(std::count(v.begin(),v.end(),nullptr) == 0);
    assert(v.size() == 2);
  }
  {
    std::vector<boost::shared_ptr<int>> v { Create() };
    assert(std::count(std::begin(v),std::end(v),nullptr) == 0);
    std::sort(std::begin(v),std::end(v));
    assert(std::count(std::begin(v),std::end(v),nullptr) == 0);
    v.erase(std::unique(std::begin(v),std::end(v)),std::end(v));
    assert(std::count(std::begin(v),std::end(v),nullptr) == 0);
    assert(v.size() == 2);
  }
  {
    std::vector<boost::shared_ptr<int>> v { Create() };
    assert(std::count(std::begin(v),std::end(v),nullptr) == 0);
    std::sort(std::begin(v),std::end(v));
    assert(std::count(std::begin(v),std::end(v),nullptr) == 0);

    std::vector<boost::shared_ptr<int>> w;
    std::unique_copy(std::begin(v),std::end(v),std::back_inserter(w));
    assert(std::count(std::begin(w),std::end(w),nullptr) == 0);
    assert(w.size() == 2);
  }
}

 

 

 

 

 

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