# Use set_intersection to intersect two lists : intersect « Set Multiset « C++

Home
C++
 1 Bitset 2 Class 3 Console 4 Data Structure 5 Data Type 6 Deque 7 Development 8 File 9 Function 10 Generic 11 Language 12 List 13 Map Multimap 14 Overload 15 Pointer 16 Qt 17 Queue Stack 18 Set Multiset 19 STL Algorithms Binary search 20 STL Algorithms Heap 21 STL Algorithms Helper 22 STL Algorithms Iterator 23 STL Algorithms Merge 24 STL Algorithms Min Max 25 STL Algorithms Modifying sequence operations 26 STL Algorithms Non modifying sequence operations 27 STL Algorithms Sorting 28 STL Basics 29 String 30 Valarray 31 Vector
 C++ » Set Multiset » intersect Screenshots
Use set_intersection to intersect two lists
 ```   #include  #include  #include  #include  #include  #include  #include  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 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 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 result;    set_intersection( listA.begin(), listA.end(),inspector_B.begin(), inspector_B.end(),back_inserter( result ) );    for_each( result.begin(), result.end(),mem_fun_ref( &PC::print ) ); }           ```
Related examples in the same category
 1 Intersect two sets 2 Create the intersection of list1 and list2