Set 和 Map 用法及介绍

本文详细介绍了JavaScript中的四种基本数据结构:数组、对象、Set和Map。数组是有序且可重复的元素集合,查找速度慢但有长度;对象为键值对,键必须为字符串或Symbol,添加删除速度快;Set是不允许重复元素的集合,插入删除快速,可遍历;Map则是键值对存储,键可为任意类型,具有长度属性。文章还提供了各种操作方法的示例,如添加、删除、查找等,并展示了如何利用Set进行数组去重。

数组:

        紧密结构,可以排序,元素可以重复

        查找需要遍历,查找速度慢,有长度

对象:

        松散结构,键值对存在,键必须是字符和Symbol类型

        添加删除速度快,不能排序,查找速度极快,没有长度

Set:

        元素不能重复的集合,松散结构,插入删除速度快,不是键值对

        查找需要遍历,有长度

Map:

        键值对存在,类似于hashMap、类似于对象

        对象的key只能是字符串或者Symbol,map的键可以是任何类型

        map有长度


  Set集合              

1.创建Set

        var numberSet = new Set( )

2.添加数据

        numberSet.add( 添加的数据 )

        当添加的数据在Set集合中已经存在时,将不会再被添加进去

3.删除指定数据/清空集合

        numberSet.delete( 指定数据 )

        返回 true 代表删除成功

                false 代表删除失败

        numberSet.clear( ) 清空集合

4.查找集合中有没有指定数据

        numberSet.has( 要查找的数据 )

                返回 true 代表存在

                        false 代表不存在

5.判断集合中有多少元素

        numberSet.size

6.遍历数组

        for ···· each遍历

        for ···· of es6新增的遍历方法 所有的迭代器都可以使用 for - of

7.小技巧(利用set给数组去重)

var arr = [1,2,3,4,1,2,3,6]
arr = Array.from( new Set(arr) )
consloe.log(arr)

Map

1.创建Map

        var person = new Map();

2.添加数据

        person.set( " 键名 "," value " )

        当添加数据时,添加的数据中两个键名重复时,新的数据会覆盖旧的数据

3.删除数据/清空map

        person.delete( 指定key名 ) --删除指定数据

        person.clear ( ) ---清空map集合

4.获取某个值

        person.get ( " 键名 " )

5.查看map中有多少个元素

        person.size

6.查看Map中有无某个key

        person.has( " key " )

        返回 true 代表存在,返回false代表不存在

7.遍历Map

        for ···· each遍历

        for ···· of es6新增的遍历方法

### set 容器的使用方法 `set` 是 C++ STL 中的一种关联容器,用于存储唯一且有序的元素。在 `set` 中,元素的值也作为其键,因此不允许重复。`set` 中的元素默认按照升序排列,底层通过红黑树实现。对于自定义类型的元素,可以通过重载 `<` 运算符或提供自定义比较器来指定排序规则。 #### 基本操作示例: 1. **插入元素**:使用 `insert` 方法将元素添加到 `set` 中。 2. **删除元素**:使用 `erase` 方法删除指定的元素。 3. **查找元素**:使用 `find` 方法查找指定的元素,返回一个迭代器。 4. **遍历元素**:使用迭代器遍历 `set` 中的所有元素。 ```cpp #include <iostream> #include <set> int main() { // 创建一个set容器 std::set<int> mySet; // 插入元素 mySet.insert(10); mySet.insert(20); mySet.insert(15); // 遍历set容器 for (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl; // 查找元素 auto it = mySet.find(15); if (it != mySet.end()) { std::cout << "找到元素: " << *it << std::endl; } else { std::cout << "未找到元素" << std::endl; } // 删除元素 mySet.erase(20); // 再次遍历set容器 for (const auto& elem : mySet) { std::cout << elem << " "; } std::cout << std::endl; return 0; } ``` #### 自定义类型的比较方式: 当 `set` 存储自定义类时,需要提供比较方式。可以通过重载 `<` 运算符或使用自定义比较类(仿函数)来实现。 ```cpp #include <iostream> #include <set> #include <string> class Student { public: std::string name; int age; Student(const std::string& name, int age) : name(name), age(age) {} // 重载<运算符以支持默认排序 bool operator<(const Student& other) const { return age < other.age; // 按年龄升序排列 } }; // 自定义比较类 struct StudentComparator { bool operator()(const Student& a, const Student& b) const { return a.name < b.name; // 按姓名升序排列 } }; int main() { // 使用默认比较器 std::set<Student> studentSet1; studentSet1.insert({"Alice", 20}); studentSet1.insert({"Bob", 18}); studentSet1.insert({"Charlie", 22}); // 遍历set容器 for (const auto& student : studentSet1) { std::cout << student.name << " (" << student.age << ")" << std::endl; } // 使用自定义比较类 std::set<Student, StudentComparator> studentSet2; studentSet2.insert({"Alice", 20}); studentSet2.insert({"Bob", 18}); studentSet2.insert({"Charlie", 22}); // 遍历set容器 for (const auto& student : studentSet2) { std::cout << student.name << " (" << student.age << ")" << std::endl; } return 0; } ``` ### map 容器的使用方法 `map` 是 C++ STL 中的一种关联容器,用于存储键值对(`key-value`),其中每个键值对的键是唯一的。`map` 中的元素按照键的升序排列,底层通过红黑树实现。对于自定义类型的键,可以通过重载 `<` 运算符或提供自定义比较器来指定排序规则。 #### 基本操作示例: 1. **插入元素**:可以使用 `insert` 方法或 `[]` 运算符插入键值对。 2. **删除元素**:使用 `erase` 方法删除指定的键值对。 3. **查找元素**:使用 `find` 方法查找指定的键值对,返回一个迭代器。 4. **遍历元素**:使用迭代器遍历 `map` 中的所有键值对。 ```cpp #include <iostream> #include <map> int main() { // 创建一个map容器 std::map<std::string, int> myMap; // 插入元素 myMap["Alice"] = 20; myMap["Bob"] = 18; myMap["Charlie"] = 22; // 遍历map容器 for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } // 查找元素 auto it = myMap.find("Bob"); if (it != myMap.end()) { std::cout << "找到元素: " << it->first << " -> " << it->second << std::endl; } else { std::cout << "未找到元素" << std::endl; } // 删除元素 myMap.erase("Charlie"); // 再次遍历map容器 for (const auto& pair : myMap) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` #### 自定义类型的比较方式: 当 `map` 的键是自定义类型时,需要提供比较方式。可以通过重载 `<` 运算符或使用自定义比较类(仿函数)来实现。 ```cpp #include <iostream> #include <map> #include <string> class Key { public: int id; std::string name; Key(int id, const std::string& name) : id(id), name(name) {} // 重载<运算符以支持默认排序 bool operator<(const Key& other) const { return id < other.id; // 按ID升序排列 } }; // 自定义比较类 struct KeyComparator { bool operator()(const Key& a, const Key& b) const { return a.name < b.name; // 按名称升序排列 } }; int main() { // 使用默认比较器 std::map<Key, std::string> myMap1; myMap1[{1, "Key1"}] = "Value1"; myMap1[{2, "Key2"}] = "Value2"; myMap1[{3, "Key3"}] = "Value3"; // 遍历map容器 for (const auto& pair : myMap1) { std::cout << "{" << pair.first.id << ", " << pair.first.name << "}: " << pair.second << std::endl; } // 使用自定义比较类 std::map<Key, std::string, KeyComparator> myMap2; myMap2[{1, "Key1"}] = "Value1"; myMap2[{2, "Key2"}] = "Value2"; myMap2[{3, "Key3"}] = "Value3"; // 遍历map容器 for (const auto& pair : myMap2) { std::cout << "{" << pair.first.id << ", " << pair.first.name << "}: " << pair.second << std::endl; } return 0; } ``` ### 总结 - `set` `map` 都是基于红黑树实现的关联容器。 - `set` 用于存储唯一且有序的元素,而 `map` 用于存储键值对,其中键是唯一的。 - 对于自定义类型的元素或键,可以通过重载 `<` 运算符或提供自定义比较器来指定排序规则。 - 插入、删除、查找等基本操作在 `set` `map` 中都非常相似,主要区别在于 `map` 存储的是键值对。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值