C++ STL set, constructor code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ set to construct a set that is empty or that is a copy of all or part of some other set in C++ programming

To show: How to use the C++ set, constructor to construct a set that is empty or that is a copy of all or part of some other set in C++ programming

 

// C++ STL set, constructor

#include <set>

#include <iostream>

using namespace std;

 

char main(void)

{

// iterators

set <char>::iterator st0_Iter, st1_Iter, st2_Iter, st3_Iter, st4_Iter, st5_Iter, st6_Iter;

// containers, create an empty set st0 of key type char

set <char> st0;

// create an empty set st1 with the key comparison function of less than, then insert 6 elements

set <char, less<char> > st1;

 

// insert/push data

st1.insert('p');

st1.insert('e');

st1.insert('g');

st1.insert('P');

st1.insert('y');

st1.insert('b');

 

// create an empty set st2 with the key comparison function of greater than, then insert 2 elements

set <char, greater<char> > st2;

st2.insert('f');

st2.insert('k');

// create a set st3 with the allocator of set st1

set <char>::allocator_type st1_Alloc;

st1_Alloc = st1.get_allocator();

set <char> st3(less<char>(), st1_Alloc);

st3.insert('u');

st3.insert('U');

// set st4, a copy of set st1

set <char> st4(st1);

// create a set st5 by copying the range st1[_First, _Last)

set <char>::const_iterator st1_PIter, st1_QIter;

st1_PIter = st1.begin();

st1_QIter = st1.begin();

st1_QIter++;

st1_QIter++;

st1_QIter++;

set <char> st5(st1_PIter, st1_QIter);

// create a set st6 by copying the range st4[_First, _Last) and with the allocator of set st2

set <char>::allocator_type st2_Alloc;

st2_Alloc = st2.get_allocator();

set <char> st6(st4.begin(), ++st4.begin(), less<char>(), st2_Alloc);

 

// more operations

cout<<"Operation: set <char> st0;"<<endl;

cout<<"st0 set data: ";

for(st0_Iter = st0.begin(); st0_Iter != st0.end(); st0_Iter++)

cout<<" "<<*st0_Iter;

cout<<endl;

cout<<"\nOperation1: set <char, less<char> > st1;"<<endl;

cout<<"Operation2: st1.insert('p')..."<<endl;

cout<<"st1 set data: ";

for(st1_Iter = st1.begin(); st1_Iter != st1.end(); st1_Iter++)

cout<<" "<<*st1_Iter;

cout<<endl;

cout<<"\nOperation1: set <char, greater<char> > st2;"<<endl;

cout<<"Operation2: st2.insert('f')..."<<endl;

cout<<"st2 set data: "<<*st2.begin()<<" "<<*++st2.begin()<<endl;

cout<<"\nOperation1: set <char> st3(less<char>(), st1_Alloc);"<<endl;

cout<<"Operation2: st3.insert('u')"<<endl;

cout<<"st3 set data: ";

for(st3_Iter = st3.begin(); st3_Iter != st3.end(); st3_Iter++)

cout<<" "<<*st3_Iter;

cout<<endl;

cout<<"\nOperation: set <char> st4(st1);"<<endl;

cout<<"st4 set data: ";

for(st4_Iter = st4.begin(); st4_Iter != st4.end(); st4_Iter++)

cout<<" "<<*st4_Iter;

cout<<endl;

cout<<"\nOperation: set <char> st5(st1_PIter, st1_QIter);"<<endl;

cout<<"st5 set data: ";

for(st5_Iter = st5.begin(); st5_Iter != st5.end(); st5_Iter++)

cout<<" "<<*st5_Iter;

cout<<endl;

cout<<"\nOperation: set <char> st6(st4.begin(), ++st4.begin(), less<char>(), st2_Alloc);"<<endl;

cout<<"st6 set data: ";

for(st6_Iter = st6.begin(); st6_Iter != st6.end(); st6_Iter++)

cout<<" "<<*st6_Iter;

cout<<endl;

return 0;

}

 

Output examples:

 

Operation: set <char> st0;

st0 set data:

Operation1: set <char, less<char> > st1;

Operation2: st1.insert('p')...

st1 set data: P b e g p y

Operation1: set <char, greater<char> > st2;

Operation2: st2.insert('f')...

st2 set data: k f

Operation1: set <char> st3(less<char>(), st1_Alloc);

Operation2: st3.insert('u')

st3 set data: U u

Operation: set <char> st4(st1);

st4 set data: P b e g p y

Operation: set <char> st5(st1_PIter, st1_QIter);

st5 set data: P b e

Operation: set <char> st6(st4.begin(), ++st4.begin(), less<char>(), st2_Alloc);

st6 set data: P

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index