C ++ standard library: std_set as an Ordered Set

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.