Main usage of set in C + +

Time:2020-10-7

brief introduction

Set encapsulates the collection in C + +. Its main function is sorting and de duplication. Let’s take a look at it in detail

——————————————————————————————————————————The dividing line of ke’ai——————————————————————————————————————————

Header file

#include

or

#include

When you write code, you have to write one of these two header filesusing namespace std;, so that you can definesetYes.

#include 
#include 
using namespace std;
int main()
{
    
    return 0;
}

definition

set S;

T: C + + any type, such as: int, char, string, STL, such asset > s1; set > > s2;
S: The variable name you defined is replaced by S

——————————————————————————————————————————The dividing line of ke’ai——————————————————————————————————————————

function

The link blew up the comments area to find me(

Returns the iterator of the first element of the set container

Usage:

s.begin();

Returns a constant iterator that points to the first element in the container.
setAll iterators in the container are constant iterators (includingconst_iteratoranditeratorMember type). They can’t be used to modify what they point to, but they can be added and subtracted normally (unless they are constants themselves).
If the container is empty, the iterator value returned should not be dereferenced. It’s almost like begin(

It’s no use(

Usage:

s.cbegin();

Returns a constant iterator that points to the end element in the container. Similar to end(

Usage:

s.cend();

Empty the current set so that the size of the current set is 0

Usage:

s.clear();

Returns the number of X in the container(count(x))

Usage:

s.count(x);

As the name suggests, it’s useless to reverse the beginning (that is, the end)(

Usage:

s.crbegin();

As the name suggests, it’s useless to end in reverse(

Usage:

s.crend();

It’s insert

Usage:

s.emplace(x);

Inserts a new element (if unique) into the collection and prompts at the insertion location. This new element is constructed in place using Arg as its construction parameter.
The insert occurs only if no other element in the container is equivalent to the element being placed (elements in the collection container are unique).
If inserted, this effectively increases the container size by one.
The value in position is used as a hint for the insertion point. However, the element is inserted into its corresponding position in the order described by its internal comparison object, but the function uses this prompt to start searching for the insertion point, which is greatly accelerated when the actual insertion point is at or near that location.
Conclusion: don’t use it(

Usage:

Coo

Bool type. If there are elements in the current geometry, it returns true, otherwise it returns false

usage

if(s.empty())
    Cout < < set is empty < endl;
else
    Cout < < set has elements < endl;

Returns a pointer to the end of the current container

usage

set::iterator it = s.end();

Delete an element or an element of a pointer

usage

std::set myset;
std::set::iterator it;
myset.erase (it);
myset.erase (40);

Search the element equivalent to Val in the container. If it is found, the iterator is returned. Otherwise, the iterator is returned to set:: end.

s.find(x);

Insert an element and click the above one in detail to see the official website

Find the first number greater than or equal to x, find the address to return the number, and return end if it does not exist.

Find the first number less than or equal to x, find the address to return the number, and return end if it does not exist.

The maximum capacity of the container at present

cout << s.max_size() << endl;

Current container capacity

cout << s.size() << endl;

——————————————————————————————————————————The dividing line of ke’ai——————————————————————————————————————————

ergodic

Iterators are required for traversaliterator

#include 
#include 
using namespace std;
int main()
{
    set s;
    s.insert(1);
    s.insert(1);
    s.insert(4);
    s.insert(5);
    s.insert(1);
    s.insert(4);
    s.insert(1);
    s.insert(9);
    s.insert(1);
    s.insert(9);
    s.insert(8);
    s.insert(1);
    s.insert(0);
    for(set::iterator it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
}

Output:

0 1 4 5 8 9
#include 
#include 
using namespace std;
int main()
{
    set s;
    s.insert('a');
    s.insert('b');
    s.insert('d');
    s.insert('b');
    s.insert('a');
    s.insert('b');
    s.insert('c');
    s.insert('b');
    s.insert('c');
    s.insert('c');
    s.insert('a');
    s.insert('a');
    s.insert('a');
    for(set::iterator it = s.begin(); it != s.end(); it++)
        cout << *it << " ";
}

Output:

a b c d

Example:
Explicit random number

You can use set. In fact, most of them appear in the topic as a tool. Generally, there are few people who take this test.

Comment on QAQ