随笔 - 632  文章 - 17  评论 - 54  阅读 - 93万

C++ set集合测试

一、概述

  案例:c++ stl之set集合练习

二、代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <set>
#include <string>
 
using namespace std;
 
//打印set元素
void printSet(set<int> &s){
    for(set<int>::iterator it=s.begin();it!=s.end();it++){
        cout << *it<<" ";
    }
    cout <<endl;
}
void test(){
    set<int> s;
    //向set集合中添加元素
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    s.insert(30);
    printSet(s);
 
    if(s.empty()){
        cout << "set is Null"<<endl;
    }else{
        cout << "set is Not Null"<<endl;
    }
    //删除元素为30的元素
    s.erase(30);
    printSet(s);
}
 
void test2(){
    set<int> s;
    s.insert(10);
    s.insert(20);
    s.insert(30);
    s.insert(40);
    s.insert(50);
    s.insert(60);
 
    set<int>::iterator pos = s.find(30);
    if(pos!=s.end()){
        cout << "I found element:"<<*pos<<endl;
    }else{
        cout <<"I Not Found Element"<<endl;
    }
 
    int num = s.count(40);
    cout <<"key=40 size is :"<<num<<endl;
 
    set<int>::iterator pos2 = s.lower_bound(30);
 
    if (pos2 != s.end())
    {
        cout << "lower_bound value is:" << *pos2 << endl;
    }
    else
    {
        cout << "lower_bound value not fond" << endl;
    }
 
    pos2 = s.upper_bound(30);
    if (pos2 != s.end())
    {
        cout << "upper_bound value is :" << *pos2 << endl;
    }
    else
    {
        cout << "upper_bound value not found" << endl;
    }
    //
    pair<set<int>::iterator,set<int>::iterator> ret = s.equal_range(30);
    if(ret.first!=s.end()){
        cout << "equal_range lower_bound value is:" << *ret.first << endl;
    }else
    {
        cout << "equal_range lower_bound value not found" << endl;
    }
 
 
    if (ret.second != s.end())
    {
        cout << "equal_range upper_bound value is:" << *ret.second << endl;
    }
    else
    {
        cout << "equal_range upper_bound value not found" << endl;
    }
}
 
void test3(){
    pair<string,int> p("tony",30);
    cout << "name: "<<p.first<< " age:"<<p.second<<endl;
 
    pair<string,int> p2 = make_pair("luoluoyang",3);
    cout << "name: "<< p2.first<<" age: "<<p2.second<<endl;
}
 
 
void test4(){
    set<int> s;
    pair<set<int>::iterator,bool> ret = s.insert(10);
    if(ret.second){
        cout << "insert success"<<endl;
    }else{
        cout << "insert fail"<<endl;
    }
 
    ret = s.insert(10);
    if(ret.second){
        cout <<"second insert success"<<endl;
    }else{
        cout <<"second insert fail"<<endl;
    }
    printSet(s);
 
 
}
 
void test5(){
     
}
 
/**
 *
 * set集合的特性:可以过滤掉重复的元素
 * */
int main(int argc, char const *argv[])
{
    // test();
    // test2();
    // test3();
    test4();
    return 0;
}

  

posted on   飘杨......  阅读(84)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示