(C++) StdSetExample1

February 24, 2017 · View on GitHub

 

 

 

 

 

(C++) StdSetExample1

 

C++98STLQt
CreatorLubuntu

 

std::set example 1: basics 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: ./CppStdSetExample1/CppStdSetExample1.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 }

 

 

 

 

 

./CppStdSetExample1/main.cpp

 


#include <cassert> #include <cstdlib> #include <set> #include <vector> int main() {   //Basic std::set operations   {     //An empty set     std::set<int> s;     assert(s.empty() && "A std::set is empty when created");     //x is a random value     const int x = std::rand();     //x cannot be in the std::set yet     assert(s.count(x)==0);     //Add x to the std::set     s.insert(x);     assert(s.count(x)==1);     //A std::set does not allow duplicates,     //so again adding x has no effect     s.insert(x);     assert(s.count(x)==1);     //Remove x from the set     s.erase(x);     assert(s.count(x)==0);   }   //Creating a std::set that orders its elements the other way around   {     std::set<int                   > s; //Default-ordered std::set     std::set<int,std::greater<int> > t; //Other way around     const int a = 3;     const int b = 1;     const int c = 2;     s.insert(a); t.insert(a);     s.insert(b); t.insert(b);     s.insert(c); t.insert(c);     std::vector<int> v; //For default-ordered std::set     std::vector<int> w; //For other std::set     std::copy(s.begin(),s.end(),std::back_inserter(v));     std::copy(t.begin(),t.end(),std::back_inserter(w));     assert(v.size() == 3);     assert(w.size() == 3);     assert(v[0] < v[1] && v[1] < v[2]);     assert(w[0] > w[1] && w[1] > w[2]);   } }