6, 10, 2, 8, 4 is a set; 2, 4, 6, 8, 10 is a set of the same integers, arranged in ascending order. In Mathematics, a set has unique elements (distinct elements), and that is, no element occurs more than once. Furthermore, a multiset is a set, where any element can occur more than once. 6, 6, 10, 2, 2, 8, 4, 4, 4 is a multiset. 2, 2, 4, 4, 4, 6, 6, 8, 10 is the same multiset, but with the elements arranged in ascending order. This article does not deal with multiset. It deals with the C++ data structure called, set.
A map in software is like an array, but it is an array with two columns instead of one. The first column has the keys and the second column has the values. Each row is one pair, making a key/value pair. A key is directly related to its value.
An example of a map is 'c',30, 'b',20, 'd',30, 'e',40, 'a',10. The first key/value pair inserted here, is 'c',3, where 'c' is the key and 30 is the value. This map is not ordered by keys. Ordering this map by keys produces 'a',10, 'b',20, 'c',30, 'd',30, 'e',40. Notice that there can be duplicated values, but not duplicated keys. An ordered map is a map ordered by keys.
A multiset is to a set, as a multimap is to a map. This means that there are maps with duplicate keys. An example of a multimap is 'a',10, 'b',20, 'b',20, 'c',30, 'c',30, 'd',30, 'e',40. And as stated above, this article does not deal with multimap, rather, it deals with the C++ data structure called, map.
In C++, a data structure is a structure with properties (data members) and methods (member functions). The data of the structure is a list; a set is a list; a map is a list of key/value pairs.
This article discusses the basics of sets and maps in C++, and to better understand this article, the reader should have had a basic knowledge of C++.
Article Content:
- Class and its Objects
- Creating a set or a map
- Iterator Basics
- Element Access for set and map
- Order of Elements in a set or map
- Other Commonly Used Member Functions
- Conclusion
Class and its Objects:
In C++, the set, the map, and other similar structures are called containers. A class is a generalized unit with data members, which are variables, and member functions that are related. When data members are given values, an object is formed. However, an object is formed in a process called, instantiation. As a class can lead to different values for the same data member variables, different objects can then be instantiated from the same class.
In C++, an unusable set is a class, as well as an unusable map. When an object is instantiated from the unusable set or the unusable map, the object becomes the real data structure. With the set and map data structures, the principal data member is a list. Well, the set and the map form a group of containers called, ordered associative containers. Unordered set and the unordered map also exist, but those are unfortunately not addressed in this article.
Creating a set or a map:
Instantiating a set from its set class is creating a set; instantiating a map from its map class is creating a map. The object so created is given a name of the programmer's choice.
In order to create a set, the program should begin with:
#include
#include
using namespace std; Note the directive “#include ”, which includes the set library that has the set class from which set data structures will be instantiated.
In order to create a map, the program should begin with:
#include
#include