compare two sets and add elements

Hello everyone,
I would like to compare two sets (t_set and n_set). if there is an element in n_set which is not in t_set, I will add it in t_set. However, the below code gives me error for insert function as below:
no matching function for call to ‘std::set<ns3::ndn::pit::IncomingFace>::insert(std::set<ns3::ndn::pit::IncomingFace>::iterator&)’
t_set.insert(tn);

I will appreciate any help.
Thanks
regards

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 typedef std::set< pit::IncomingFace> total_set ;
total_set t_set;
t_set=pitEntry->GetIncoming();
typedef std::set< pit::IncomingFace> negative_set ;
negative_set n_set;
  n_set=pitEntry->GetIncoming_set_neg();
for(std::set< pit::IncomingFace>::iterator tn=n_set.begin(); tn!=n_set.end();tn++)
{
int count=0;
for(std::set< pit::IncomingFace>::iterator it=t_set.begin(); it!=t_set.end();it++)
{
if (it==tn)
{
count=1;
} 
}
if (count==1)
{
t_set.insert(tn);
} 
}
In t_set.insert(tn);, tn is an iterator.

Something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
#include <functional>
#include <iterator>

template < typename T > // add elements in n_set which are not in t_set, to t_set
std::set<T>& insert_missing( std::set<T>& t_set, const std::set<T>& n_set)
{
    // holder for wrapped references to the missing items
    // note: this avoids making unnecessary copies of the missing items
    //       (the other option would be to use move iterators for the insert)
    std::vector< std::reference_wrapper<const T> > missing_items ;

    // get the references to items in n_set that are not there in t_set
    // http://en.cppreference.com/w/cpp/algorithm/set_difference
    std::set_difference( n_set.begin(), n_set.end(), t_set.begin(), t_set.end(),
                         std::back_inserter(missing_items) ) ;

    // and insert (copies of) these items into t_set
    // http://en.cppreference.com/w/cpp/container/set/insert overload (5)
    t_set.insert( missing_items.begin(), missing_items.end() ) ;

    return t_set ;
}

int main()
{
    std::set<int> t_set { 1, 3, 5, 7, 9 } ;
    const std::set<int> n_set { 1, 2, 4, 6, 7, 8, 9 } ;

    for( int v : insert_missing( t_set, n_set ) ) std::cout << v << ' ' ;
    std::cout << '\n' ;
}

http://coliru.stacked-crooked.com/a/af9852fc5abb687f
using std::set_difference():
1
2
3
4
5
6
7
8
9
10
11
12
# include <iostream>
# include <set>
# include <algorithm>
# include <iterator>

int main()
{
  std::set<int> set_a{1, 3, 4, 5};
  std::set<int> set_b{2, 4, 5};
  std::set_difference(set_b.begin(), set_b.end(), set_a.begin(), set_a.end(), std::inserter(set_a, set_a.begin()));
  for (const auto& elem : set_a)std::cout << elem << " ";
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <set>
using namespace std;

template <typename X> void addMissing( set<X> &T, set<X> N )
{
   for ( X element : N ) T.insert( element );              // if there, do nothing; if not there then insert
}


//======================================================================


template <typename X> void printSet( string title, set<X> S )
{
   cout << title << " ";
   for ( X element : S ) cout << element << " ";
   cout << endl;
}


//======================================================================


int main()
{
   set<int> T{ 1, 3, 5, 7, 9 };
   set<int> N{ 1, 2, 4, 6, 7, 8, 9 };

   printSet( "Set T: ", T );
   printSet( "Set N: ", N );

   addMissing( T, N );
   printSet( "Set T u N: ", T );
}


Set T:  1 3 5 7 9 
Set N:  1 2 4 6 7 8 9 
Set T u N:  1 2 3 4 5 6 7 8 9 



You will improve performance (vastly) at the expense of diminishing readability (slightly - in my personal opinion) by passing larger objects by const reference:
template <typename X> void addMissing( set<X> &T, const set<X> &N )
and
template <typename X> void printSet( const string &title, const set<X> &S )
and
for ( const X &element : N ) T.insert( element );
and
for ( const X &element : S ) cout << element << " ";
Last edited on
thank you so much from all your helps.
If ease of programming is the goal, at the cost of run-time efficiency (this should be the goal unless there is a performance constraint), just write a one liner: t_set.insert( n_set.begin(), n_set.end() ) ; Don't mess around with writing a needless function which contains a needless loop in which you try to insert elements one by one.

Otherwise, do it in two steps: a. get the set difference to determine the elements that are missing from the set, and then b. do a bulk insertion of these elements.
Topic archived. No new replies allowed.