Abstract: std: set is used as an associated container of the standard library to sort internal elements. This feature allows you to insert and sort a group of elements. Std: the initial design of set is to complete the concept of "set" in mathematics, as is the interface it provides. This article briefly introduces this standard library container.
To use std: set, I only need:
# Include
// Define a set of Integer Data
Std: set </span> int> set;
Like all STL containers, the basic use of std: set is so simple that it is not difficult to use even advanced functions. STL is a designed and available library for debugging.
The following will show some simple examples. These examples are very simple. First, we will introduce the "sort" Usage:
# Include
Int array [5] = {12, 34, 10, 98, 3 };
Const size_t array_size = sizeof (array)/sizeof (array [0]);
Std: set </span> int> set;
For (size_t I = 0; I </span> array_size; ++ I)
{
// Insert data into the set and sort the data automatically
Set. insert (array [I]);
}
// At this time, it is sorted and output in sequence below
Std: set </span> int >:: const_iterator B = set. begin ();
For (; B! = Set. end (); ++ B)
{
Std: cout <* B <'\ n ';
}
In general consciousness, a set does not have many operations. In STL, std: set does not have many operations, and its sorting is automatic. We can insert an element, you can also delete an element or iterator. The following simple example includes the features of std: set itself:
# Include
Int array [5] = {12, 34, 10, 98, 3 };
Const size_t array_size = sizeof (array)/sizeof (array [0]);
// A new method for defining containers
Std: set </span> int> set (array, array + array_size );
// Insert an element
Set. insert (23 );
// Remove an element
Set. erase (10 );
// Remove an element that is not in the Set
// Do nothing at this time. n will return 0 as the return value.
// Indicates that no element is removed.
Size_t n = set. erase (11 );
// Www. software8.co
// Use the iterator to find an element
Std: set </span> int >:: const_iterator result = set. find (98 );
Std: set </span> int> other;
// Exchange the content of Two Sets
Std: swap (set, other );
// Clear all content
Other. clear ();
Eg:
# Include
# Include
# Include
Using namespace std;
Int main (void)
{
Std: set _ signals;
_ Signals. insert (SIGTERM );
_ Signals. insert (SIGINT );
_ Signals. insert (SIGCHLD );
For (std: set: const_iterator I = _ signals. begin (); I! = _ Signals. end (); ++ I)
Cout <* I <endl;
_ Signals. clear ();
Return 0;
}