(C++) StdSetExample5

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) StdSetExample5

 

C++98STLQt
CreatorLubuntu

 

std::set example 5 is a std::set example.

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

  • Qt Qt: version 5.4.1 (32 bit)
  • STL STL: GNU ISO C++ Library, version 4.9.2

 

 

 

 

 

Qt project file: ./CppStdSetExample5/CppStdSetExample5.pro

 


TEMPLATE = app CONFIG += console CONFIG -= app_bundle CONFIG -= qt SOURCES += main.cpp # Go ahead and use Qt.Core: it is about as platform-independent as # the STL and Boost QT += core # Go ahead and use Qt.Gui: it is about as platform-independent as # the STL and Boost. It is needed for QImage QT += gui # Don't define widgets: it would defy the purpose of this console # application to work non-GUI #greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG   += console CONFIG   -= app_bundle TEMPLATE = app # # # Type of compile # # CONFIG(release, debug|release) {   DEFINES += NDEBUG NTRACE_BILDERBIKKEL } QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++ unix {   QMAKE_CXXFLAGS += -Werror } win32 {   INCLUDEPATH += \     ../../Libraries/boost_1_54_0 }

 

 

 

 

 

./CppStdSetExample5/main.cpp

 


#include <algorithm> #include <cassert> #include <functional> #include <iostream> #include <iterator> #include <set> #include <string> #include <vector> #include <memory> #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 struct MyClass {   MyClass(const std::string& s, const int x) : m_s(s), m_x(x) {}   const std::string m_s;   const int m_x; }; bool operator<(const MyClass& lhs, const MyClass& rhs) = delete; //Prevent ordering sets by the pointers their addresses bool operator<(const boost::shared_ptr<      MyClass> lhs, const boost::shared_ptr<      MyClass> rhs) = delete; bool operator<(const boost::shared_ptr<      MyClass> lhs, const boost::shared_ptr<const MyClass> rhs) = delete; bool operator<(const boost::shared_ptr<const MyClass> lhs, const boost::shared_ptr<      MyClass> rhs) = delete; bool operator<(const boost::shared_ptr<const MyClass> lhs, const boost::shared_ptr<const MyClass> rhs) = delete; //Display x first std::ostream& operator<<(std::ostream& os, const MyClass& m) {   os << m.m_x << ' ' << m.m_s;   return os; } int main() {   //Put MyClass instances in std::vector   std::vector<boost::shared_ptr<MyClass>> v;   v.push_back(boost::shared_ptr<MyClass>(new MyClass("five",5)));   v.push_back(boost::shared_ptr<MyClass>(new MyClass("four",4)));   v.push_back(boost::shared_ptr<MyClass>(new MyClass("one",1)));   v.push_back(boost::shared_ptr<MyClass>(new MyClass("six",6)));   v.push_back(boost::shared_ptr<MyClass>(new MyClass("three",3)));   v.push_back(boost::shared_ptr<MyClass>(new MyClass("two",2)));   //Create a std::set with a custom MyClass ordering   std::set<boost::shared_ptr<MyClass>,std::function<bool(boost::shared_ptr<MyClass>,boost::shared_ptr<MyClass>)>> s(     [](const boost::shared_ptr<MyClass> lhs, const boost::shared_ptr<MyClass> rhs)     {       return lhs->m_x < rhs->m_x;     }   );   //Copy from std::vector to std::map   std::copy(v.begin(),v.end(),std::inserter(s,s.begin()));   //Show that the set orders by MyClass their x   std::transform(s.begin(),s.end(),std::ostream_iterator<MyClass>(std::cout,"\n"),     [](const boost::shared_ptr<MyClass> m) { return *m; }   ); } /* Screen output: 1 one 2 two 3 three 4 four 5 five 6 six Press <RETURN> to close this window... */