Sets and Set Unions

I am working on a programming project that is an NFL survivor pool, and I am supposed to create sets of division, the create a union of those sets into the conferences.

EX:
AFCNorth <--- {BR, CIN, CLV, PS}
AFCEast <--- {BB, MD, NEP, NYJ}
AFCSouth <--- {HT, IC, JJ, TT}
AFCWest <--- {DB, KCC, LAC, OR}
NFCNorth <--- {CB, DL, GBP, MV}
NFCEast <--- {DC, NYG, PE, WR}
NFCSouth <--- {AF, CP, NOS, TBB}
NFCWest <--- {AC, LAR, SF, SS}

 Create the following sets using the union operator:

AFC <--- AFCNorth U AFCEast U AFCSouth U AFCWest
NFC <--- NFCNorth U NFCEast U NFCSouth U NFCWest
NFL <--- AFC U NFC

I am thinking about just creating arrays of each division then adding the arrays into the conferences, then adding the conferences into NFL.

I was wondering if this would work, or if there is possibly a more efficient way to create these unions.
I am thinking about just creating arrays

Why not just create sets instead? Then instead of calling sort() and set_union() as you would for arrays, you could just append the sets together.
@cubbi how do you create sets in C++?
There's a reference on this very site; if you type 'set' in the search bar there, it takes you to http://www.cplusplus.com/reference/set/set/?kw=set

as for code, it's just
set<string> AFCNorth = {"BR", "CIN", "CLV", "PS"};
don't forget to #include <set>
Last edited on
http://www.cplusplus.com/reference/set/set/

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <set>
#include <string>

int main()
{
   // create an empty set and insert the items
   std::set<std::string> AFCNorth;
   AFCNorth.insert("BR");
   AFCNorth.insert("CIN");
   AFCNorth.insert("CLV");
   AFCNorth.insert("PS");

   std::cout << "AFC North:\n";

   // displaying the set contents using iterators and a for loop
   for (auto team = AFCNorth.begin(); team != AFCNorth.end(); team++)
   {
      std::cout << *team << ' ';
   }
   std::cout << "\n\n";

   // filling a set on creation
   std::set<std::string> NFCNorth { "CB", "DL", "GBP", "MV" };

   std::cout << "NFC North:\n";

   // displaying the contents using a range-based for loop
   for (auto& team : NFCNorth)
   {
      std::cout << team << ' ';
   }
   std::cout << "\n\n";

   // create a blank set to merge other sets
   std::set<std::string> NFL;

   // insert the entire contents of each set
   NFL.insert(AFCNorth.begin(), AFCNorth.end());
   NFL.insert(NFCNorth.begin(), NFCNorth.end());

   std::cout << "NFL:\n";

   for (auto& team : NFL)
   {
      std::cout << team << ' ';
   }
   std::cout << '\n';

}

AFC North:
BR CIN CLV PS

NFC North:
CB DL GBP MV

NFL:
BR CB CIN CLV DL GBP MV PS
C++17 adds a new member function for std::set. std::set::merge()

39
40
   NFL.insert(AFCNorth.begin(), AFCNorth.end());
   NFL.insert(NFCNorth.begin(), NFCNorth.end());


can be written as:
39
40
   NFL.merge(AFCNorth);
   NFL.merge(NFCNorth);
std::set sorts the objects automatically. If you don't want objects to be sorted use std::unordered_set.

With std::set each object in the set is unique, no duplicates. If you want duplicates use std::multiset.

std::multiset is sorted automatically, the same as std::set. If you want unsorted duplicates, use std::unordered_multiset.
@Furry Guy: set::merge() does not have the same behaviour than set::insert()
merge() will not allocate new elements, but simply move them. so at the end, AFCNorth will be empty


@OP: ¿can a team be in more than one division?
you can use the knowledge of your domain to simplify things.
sort() will be a simply append, no need to sort as the intersection will always be empty


> a more efficient way to create these unions.
you don't even have 30 elements, not going to matter.
I feel like I missed something: Why are we avoiding set_union() here?
@ne555, I never said merge() wouldn't MOVE the elements. Neither that it would exhibit the same behavior. Simply mentioned there is a new member function.

cppreference indicates there is no moving involved, though.

No elements are copied or moved, only the internal pointers of the container nodes are repointed.


https://en.cppreference.com/w/cpp/container/set/merge

@jonnin, not avoiding std::set_union, just showing alternatives which are member functions of std::set. I was going to post an example using it, but got lazy.
> I never said merge() wouldn't MOVE the elements
perhaps you should

> Neither that it would exhibit the same behavior.
you said «can be written as»
¿what's that suppose to mean then?

> cppreference indicates there is no moving involved, though.
yeah, `move' was a wrong term
what I want to highlight is that the source container will end up empty
I am starting to think that c++ set is one of the worst things Ive seen in the language. Its telling that 99% of the examples I see use vectors instead of sets to explain the set operations.
Last edited on
Topic archived. No new replies allowed.