Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use collections in C++

Shulou Source: shulou.com Published: 2022-06-01 20:49:52 10月03日 Update

This article mainly explains "how to use sets in c++". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how to use sets in c++" together.

***** It is recommended to use std::set--element du in baistl standard template library, which does not allow zhi to duplicate dao, or std::multiset--element to allow duplication. These two kinds of collective efficiency zhuan rate is very high.

*****

****** For example:

std::set mySet;

int a,b,c,d;

a=1;

a=2;

a=3;

a=4;

mySet.insert(a);

mySet.insert(b);

mySet.insert(c);

mySet.insert(d); //erase(), find(), size() function corresponding to delete, find, count elements

You might ask, why not just use arrays? The biggest advantage of std::set is that the elements in the collection are sorted by key. std::set stores only one value if it encounters duplicate key values, while std::multiset allows duplicate elements to exist side by side.

===============

The set in c++ is very useful, especially for finding some intersection, union, or output that does not repeat.

Note that sets are automatically sorted from smallest to largest, and duplicate elements are automatically removed.

Here are a few very common and useful methods:

1、insert()

Put the elements in insert into a collection, as follows:

#include

#include

using namespace std;

int main() {

set s;

s.insert(1);//put 1 into the set s

return 0;

}

2、find()

find(value) returns an iterator whose set value corresponds to value;

#include

#include

using namespace std;

int main() {

set s;

s.insert(1);

s.insert(2);

auto it=s.find(2);

cout

Tags: Element output centering central set address sort function learning number code key content interval picture case array result easy to use iteration from small to large Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia MySQL Microsoft Docker vpn