STL库中的集合set简介(C++,Java)

62 篇文章 0 订阅
14 篇文章 0 订阅

1.简介:


集合是数学中的一个概念,通俗地理解,集合是由一些不重复的数据组成的。比如{1,2,3}就是一个有1,2,3三个元素的集合。C++和Jav的标准库中的集合支持高效的插入、删除和查询操作,这3个操作的时间复杂度都是O(lgn),其中n是当前集合中元素的个数。如果用数组,虽然插入的时间复杂度为O(1),但是删除和查询的时间复杂度为O(n),效率太低。
C++中我们常用的集合是set,在Java中常用的集合是HashSet。


2.引用库


C++中set的实现在一个<set>头文件中,在代码开头引入这个头文件,并且同样加上一句using namespace std;
#include <set>
using namespace std;

Java中HashSet在java.util.HashSet包里面实现。通过import的方式引进库。
import java.util.HashSet;

3.构造一个集合


C++中直接构造一个set的语句:set<T> s.这样我们定义了一个名为s,储存T类型的数据的集合,其中T是集合要储存的数据类型。初始的时候s是空集合。

Java中通过HashSet<T> set = new HashSet<T>()构造一个储存T类型数据的HashSet对象。

4.插入元素


C++中用insert()方法向集合中插入一个新的元素。注意如果集合中已经存在了某个元素,再次插入不会产生任何效果,集合中是不会出现重复元素的。
#include <set>
#include <string>
using namespace std;
int main() {
    set<string> country;  // {}
    country.insert("China"); // {"China"}
    country.insert("America"); // {"China", "America"}
    country.insert("France"); // {"China", "America", "France"}
    return 0;
}
Java通过add()向集合中添加元素,注意如果集合中已经存在了某个元素,再次插入不会产生任何效果,集合中是不会出现重复元素的。
import java.util.HashSet;
public class HashSetTest {
    public static void main(String[] args) {
        HashSet<String> country = new HashSet<String>(); // {}
        country.add("China"); // {"China"}
        country.add("America"); // {"China", "America"}
        country.add("France"); // {"China", "America", "France"}
    }
}

5.删除元素


C++中通过erase()方法删除集合中的一个元素,如果集合中不存在这个元素,不进行任何操作。
#include <set>
#include <string>
using namespace std;
int main() {
    set<string> country;  // {}
    country.insert("China"); // {"China"}
    country.insert("America"); // {"China", "America"}
    country.insert("France"); // {"China", "America", "France"}
    country.erase("America"); // {"China", "France"}
    country.erase("England"); // {"China", "France"}
    return 0;
}
Java中通过remove()方法删除集合中的一个元素,如果集合中不存在这个元素,不进行任何操作。
import java.util.HashSet;
public class HashSetTest {
    public static void main(String[] args) {
        HashSet<String> country = new HashSet<String>(); // {}
        country.add("China"); // {"China"}
        country.add("America"); // {"China", "America"}
        country.add("France"); // {"China", "America", "France"}
        country.remove("America"); // {"China", "France"}
        country.remove("England"); // {"China", "France"}
    }
}

6.查找元素


C++中如果你想知道某个元素是否出现在集合中,你可以直接用count()方法。如果集合中存在我们要查找的元素,返回1(true),否则返回0(false)。
#include <set>
#include <string>
#include <stdio.h>
using namespace std;
int main() {
    set<string> country;  // {}
    country.insert("China"); // {"China"}
    country.insert("America"); // {"China", "America"}
    country.insert("France"); // {"China", "America", "France"}
    if (country.count("China")) {
        printf("China belong to country");
    }
    return 0;
}
Java中如果你想知道某个元素是否在集合中出现,你可以直接用contains()方法。如果集合中存在我们要查找的元素,返回true,否则会返回false。
import java.util.HashSet;
public class HashSetTest {
    public static void main(String[] args) {
        HashSet<String> country = new HashSet<String>(); // {}
        country.add("China"); // {"China"}
        country.add("America"); // {"China", "America"}
        country.add("France"); // {"China", "America", "France"}
        if (country.contains("China")) {
            System.out.println("China belong to country");
        }
    }
}

7.遍历元素


C++通过迭代器可以访问集合中的每个元素,迭代器就好比指向集合中的元素的指针。如果你不了解迭代器,你只需要先记住用法。
#include <set>
#include <string>
#include <iostream>
using namespace std;
int main() {
    set<string> country;  // {}
    country.insert("China"); // {"China"}
    country.insert("America"); // {"China", "America"}
    country.insert("France"); // {"China", "America", "France"}
    for (set<string>::iterator it = country.begin(); it != country.end(); ++it) {
        cout << (*it) << endl;
    }
    return 0;
}
因为set集合不是数组不能像vector一样进行cout<<country[i];
Java中遍历元素的方法很简单,只需要“一个冒号”即可完成遍历。
import java.util.HashSet;
public class HashSetTest {
    public static void main(String[] args) {
        HashSet<String> country = new HashSet<String>(); // {}
        country.add("China"); // {"China"}
        country.add("America"); // {"China", "America"}
        country.add("France"); // {"China", "America", "France"}
        for (String name : country) {
            System.out.println(name);
        }
    }
}

注意:在C++遍历set是从小到大进行的,而Java中的HashSet是无序的,如果需要将所有元素有序输出,需要将所有元素取出后对其进行排序。


8.清空


C++和Java中都需要调用clear()方法就可清空set或者HashSet。


9.常用方法总结



欢迎大家踩一踩http://47.107.118.184

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值