Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use collections in C++

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report