Site icon Learn C++

What Is Set (std::set) In Modern C++?

What Is Set stdset In Modern C++

Modern C++ has a lot of options to add and modify data members with its amazing data holders, arrays, structs, pointers, and containers. Containers are powerful data storage arrays in C++ and they are very useful to iterate and search. A container is a holder object that stores data elements (a collection of data objects). std::array, std::vector, and std::map are these kinds of containers. There is another useful container std::set, in this post, we explain what std::set is. Before that let’s remind ourselves what containers are in C++ programming and what are their types.

What is a container in modern C++?

Containers are modern data storage arrays in modern C++ and they are very useful to iterate and search data with their powerful methods and properties.

A container is a holder object that stores data elements (a collection of data objects). They are implemented as a class template to define objects that can be used with modern rules of C++ (The rule of 6), they allow great flexibility in the different data types supported as elements, they can be used with int, float, double, etc. or with struct types, they can be used with other modern types of C++, lambdas and templates. Thus, the developer can create different data sets in memory, these can be static or dynamic, they are safe and optimized well.

What are the basic container types modern C++?

The C++ Standards library defines 4 container types. 

If you want to learn more about these each type, here are some basic details,

What is std::set in modern C++?

The set (std::set) is an associative container defined in <set> header that contains unique objects in a key type, and they are sorted. This container sorts members by using the Compare key comparison class. Sets are used to list unique key values in sorted order, we can use the find method to search, the erase method to remove a member, and insert, insert_range, and emplace methods to insert new members, each of these operations has logarithmic complexity.

Here is the general definition in the header set.

[crayon-6763a4235963a882259762/]

since C++17, it is defined in pmr namespace as below.

[crayon-6763a42359642201111480/]

We can use begin(), end(), … iterations, and ranges to operate and list them. We can use the insert or insert_range() method to add new key values. There are many other useful modifier methods, such as; clear(), emplace(), erase(), swap(), extract(), merge() operations. There are other lookup methods in addition to the find method, such as, count, contains, equal_range, etc.

Is there a simple example about std::set in modern C++?

Here is a simple example of the set of int keys.

[crayon-6763a42359643901002989/]

as you see we define some key values and we added two more key values, note that 23 is also defined before. Now, we can print all of them as below.

[crayon-6763a42359644919773760/]

and the output will be as below.

[crayon-6763a42359645043278625/]

Note that, members of std::set are automatically sorted, and while we add 23 values again, it is listed once because each of the members are unique keys.

Let’s see another example of the set of string keys,

[crayon-6763a42359646379279299/]

and we can list them as below.

[crayon-6763a42359649330805705/]

here is the output which is automatically sorted.

[crayon-6763a4235964a399255403/]

Is there a full example about std::set in modern C++?

This example below includes all above and also if you are asking how you can use find() method to search a key value here is how you can do it,

[crayon-6763a4235964b650992278/]

here is the output of this full example.

[crayon-6763a4235964c568713867/]

As you see set is very useful container to sort unique key values, if you want to list multiple same values then you should use multiset (std::multiset). If you want to use key values that points another values then you can use std::map or std::multimap.

For more new details about std::set, you can check this paper n2543.

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Exit mobile version