C++/Set Multiset/difference
Содержание
- 1 Create the symmetric difference between list1 and list2
- 2 Get the difference between two sets
- 3 Get the symmetric difference between two sets
- 4 set_difference and back_inserter
- 5 Use set_difference to get values from only one list
- 6 Use set_symmetric_difference() to get the difference between two lists
Create the symmetric difference between list1 and list2
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
template<class InIter>
void show_range(const char *msg, InIter start, InIter end);
int main()
{
list<char> list1, list2, result(15);
list<char>::iterator res_end;
for(int i=0; i < 5; i++)
list1.push_back("A"+i);
for(int i=3; i < 10; i++)
list2.push_back("A"+i);
show_range("Contents of list1: ", list1.begin(), list1.end());
show_range("Contents of list2: ", list2.begin(), list2.end());
res_end = set_symmetric_difference(list1.begin(), list1.end(),list2.begin(), list2.end(),result.begin());
show_range("Symmetric difference of list1 and list2: ",result.begin(), res_end);
return 0;
}
template<class InIter>
void show_range(const char *msg, InIter start, InIter end) {
InIter itr;
cout << msg << endl;
for(itr = start; itr != end; ++itr)
cout << *itr << endl;
}
Get the difference between two sets
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void print(int elem) {
cout << elem << " ";
}
int main(int argc, char** argv) {
vector<int> setOne, setTwo, setThree;
setOne.push_back(1);
setOne.push_back(2);
setOne.push_back(3);
setTwo.push_back(2);
setTwo.push_back(3);
setTwo.push_back(4);
// set algorithms work on sorted ranges
sort(setOne.begin(), setOne.end());
sort(setTwo.begin(), setTwo.end());
vector<int>::iterator newEnd = set_difference(setOne.begin(), setOne.end(), setTwo.begin(),setTwo.end(), setThree.begin());
cout << "The difference between set one and set two is: ";
for_each(setThree.begin(), newEnd, &print);
cout << endl;
return (0);
}
Get the symmetric difference between two sets
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
void print(int elem) {
cout << elem << " ";
}
int main(int argc, char** argv) {
vector<int> setOne, setTwo, setThree;
setOne.push_back(1);
setOne.push_back(2);
setOne.push_back(3);
setTwo.push_back(2);
setTwo.push_back(3);
setTwo.push_back(4);
// set algorithms work on sorted ranges
sort(setOne.begin(), setOne.end());
sort(setTwo.begin(), setTwo.end());
vector<int>::iterator newEnd = set_symmetric_difference(setOne.begin(), setOne.end(), setTwo.begin(),setTwo.end(), setThree.begin());
cout << "The symmetric difference is: ";
for_each(setThree.begin(), newEnd, &print);
cout << endl;
return (0);
}
set_difference and back_inserter
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
class PC{
public:
enum part { keyboard, mouse, monitor };
PC( part a_part = PC::keyboard, int id = 0 );
bool operator<( const PC& rhs ) const;
void print() const;
private:
part part_;
int id_;
};
inline
PC::PC( part a_part, int id ) : part_( a_part ), id_( id ){}
inline bool PC::operator<( const PC& rhs ) const{
return id_ < rhs.id_;
}
void PC::print() const {
string component;
if( part_ == keyboard )
component = "keyboard";
else if( part_ == mouse )
component = "mouse";
else
component = "monitor";
cout << "ID: " << setw( 8 ) << left << id_ << " PC: " << component << endl;
}
int main( )
{
list<PC> listA;
listA.push_back( PC( PC::keyboard, 3 ) );
listA.push_back( PC( PC::mouse, 1 ) );
listA.push_back( PC( PC::monitor, 9 ) );
listA.push_back( PC( PC::keyboard, 2 ) );
listA.push_back( PC( PC::monitor, 8 ) );
list<PC> inspector_B( listA );
inspector_B.front() = PC( PC::mouse, 6 );
inspector_B.back() = PC( PC::monitor, 1 );
// must sort before using set algorithms
listA.sort();
inspector_B.sort();
for_each( listA.begin(), listA.end(),mem_fun_ref( &PC::print ) );
for_each( inspector_B.begin(), inspector_B.end(),mem_fun_ref( &PC::print ) );
vector<PC> result;
result.clear();
set_difference( listA.begin(), listA.end(),inspector_B.begin(), inspector_B.end(),back_inserter( result ) );
for_each( result.begin(), result.end(),mem_fun_ref( &PC::print ) );
}
Use set_difference to get values from only one list
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
class PC
{
public:
enum part { keyboard, mouse, monitor };
PC( part a_part = PC::keyboard, int id = 0 );
bool operator<( const PC& rhs ) const;
void print() const;
private:
part part_;
int id_;
};
inline
PC::PC( part a_part, int id ) : part_( a_part ), id_( id ){}
inline bool PC::operator<( const PC& rhs ) const{
return id_ < rhs.id_;
}
void PC::print() const {
string component;
if( part_ == keyboard )
component = "keyboard";
else if( part_ == mouse )
component = "mouse";
else
component = "monitor";
cout << "ID: " << setw( 8 ) << left << id_ << " PC: " << component << endl;
}
int main( )
{
list<PC> listA;
listA.push_back( PC( PC::keyboard, 3 ) );
listA.push_back( PC( PC::mouse, 1 ) );
listA.push_back( PC( PC::monitor, 9 ) );
listA.push_back( PC( PC::keyboard, 2 ) );
listA.push_back( PC( PC::monitor, 8 ) );
list<PC> inspector_B( listA );
inspector_B.front() = PC( PC::mouse, 6 );
inspector_B.back() = PC( PC::monitor, 1 );
// must sort before using set algorithms
listA.sort();
inspector_B.sort();
vector<PC> result;
// make vector large enough to hold all inspected parts
result.resize( listA.size() + inspector_B.size() );
cout << "\n\nlist B ONLY\n";
vector<PC>::iterator the_end =set_difference( inspector_B.begin(), inspector_B.end(),listA.begin(), listA.end(), result.begin() );
for_each( result.begin(), the_end, mem_fun_ref( &PC::print ) );
}
Use set_symmetric_difference() to get the difference between two lists
#include <algorithm>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <string>
#include <vector>
using namespace std;
class PC
{
public:
enum part { keyboard, mouse, monitor };
PC( part a_part = PC::keyboard, int id = 0 );
bool operator<( const PC& rhs ) const;
void print() const;
private:
part part_;
int id_;
};
inline
PC::PC( part a_part, int id ) : part_( a_part ), id_( id ){}
inline bool PC::operator<( const PC& rhs ) const{
return id_ < rhs.id_;
}
void PC::print() const {
string component;
if( part_ == keyboard )
component = "keyboard";
else if( part_ == mouse )
component = "mouse";
else
component = "monitor";
cout << "ID: " << setw( 8 ) << left << id_ << " PC: " << component << endl;
}
int main( )
{
list<PC> listA;
listA.push_back( PC( PC::keyboard, 3 ) );
listA.push_back( PC( PC::mouse, 1 ) );
listA.push_back( PC( PC::monitor, 9 ) );
listA.push_back( PC( PC::keyboard, 2 ) );
listA.push_back( PC( PC::monitor, 8 ) );
list<PC> inspector_B( listA );
inspector_B.front() = PC( PC::mouse, 6 );
inspector_B.back() = PC( PC::monitor, 1 );
// must sort before using set algorithms
listA.sort();
inspector_B.sort();
for_each( listA.begin(), listA.end(),mem_fun_ref( &PC::print ) );
for_each( inspector_B.begin(), inspector_B.end(),mem_fun_ref( &PC::print ) );
vector<PC> result;
// make vector large enough to hold all inspected parts
result.resize( listA.size() + inspector_B.size() );
vector<PC>::iterator the_end = set_symmetric_difference( listA.begin(),listA.end(), inspector_B.begin(), inspector_B.end(),result.begin() );
for_each( result.begin(), the_end, mem_fun_ref( &PC::print ) );
}