multiset的使用

本文展示了一个使用C++标准库中的multiset进行集合运算的例子,包括并集、交集和差集的操作,并通过iostream输出结果。
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{ const int N = 10;
  int a[N] = {4, 1, 1, 1, 1, 1, 0, 5, 1, 0};
  int b[N] = {4, 4, 2, 4, 2, 4, 0, 1, 5, 5};
  multiset<int> A(a, a + N);
  multiset<int> B(b, b + N);
  multiset<int> C;
  cout << "Set A: ";
  copy(A.begin(), A.end(), ostream_iterator<int>(cout, " "));
  cout << endl;
  cout << "Set B: ";
  copy(B.begin(), B.end(), ostream_iterator<int>(cout, " "));  
  cout << endl; 
  cout << "Union: ";
  set_union(A.begin(), A.end(), B.begin(), B.end(),ostream_iterator<int>(cout, " "));
  cout << endl;
  cout << "Intersection: ";
  set_intersection(A.begin(), A.end(), B.begin(), B.end(),ostream_iterator<int>(cout, " "));
  cout << endl;  
  set_difference(A.begin(), A.end(), B.begin(), B.end(),inserter(C, C.begin()));
  cout << "Set C (difference of A and B): ";
  copy(C.begin(), C.end(), ostream_iterator<int>(cout, " "));
  cout << endl;

 
### C++ 中 multiset使用方法及定义示例 #### 1. multiset 的基本概念 `std::multiset` 是 C++ 标准库中的关联容器,允许存储多个具有相同值的元素,并且默认按照升序排列[^1]。与 `std::set` 不同,`std::multiset` 允许重复元素的存在。 #### 2. multiset 的定义与初始化 以下是 `std::multiset` 的几种常见定义和初始化方式: ```cpp #include <iostream> #include <set> int main() { // 使用初始化列表创建 multiset std::multiset<int> multiset1 = {1, 2, 2, 3, 4}; // 使用拷贝构造函数 std::multiset<int> multiset2(multiset1); // 使用赋值操作 std::multiset<int> multiset3; multiset3 = multiset2; // 单个元素插入 std::multiset<int> multiset4; multiset4.insert(5); } ``` #### 3. 自定义比较函数 如果需要对 `std::multiset` 的元素进行自定义排序,可以定义一个比较函数或比较结构体,并将其作为模板参数传递给 `std::multiset`。以下是一个示例: ```cpp #include <iostream> #include <set> #include <string> // 定义键类型为结构体 struct Key { int id; std::string name; }; // 定义比较函数对象 struct Compare { bool operator()(const Key& a, const Key& b) const { if (a.id != b.id) { return a.id < b.id; // 首先按 id 排序 } return a.name < b.name; // 如果 id 相同,则按 name 排序 } }; int main() { // 使用自定义比较函数创建 multiset std::multiset<Key, Compare> customMultiset; // 插入元素 customMultiset.insert({1, "Alice"}); customMultiset.insert({1, "Bob"}); customMultiset.insert({2, "Charlie"}); // 遍历 multiset for (const auto& key : customMultiset) { std::cout << "Key: (" << key.id << ", " << key.name << ")" << std::endl; } return 0; } ``` #### 4. 常见使用函数 以下是一些常用的 `std::multiset` 成员函数及其用法: - **`insert()`**:插入元素。 - **`erase()`**:删除指定元素或特定位置的元素。 - **`find()`**:查找指定元素的第一个匹配项。 - **`count()`**:统计指定元素的出现次数。 - **`lower_bound()` 和 `upper_bound()`**:返回指向第一个不小于或大于指定值的迭代器。 - **`swap()`**:交换两个 `std::multiset` 的内容[^2]。 示例代码如下: ```cpp #include <iostream> #include <set> int main() { std::multiset<int> myMultiset = {1, 2, 2, 3, 4}; // 查找值为 2 的所有元素 auto range = myMultiset.equal_range(2); for (auto it = range.first; it != range.second; ++it) { std::cout << "Found: " << *it << std::endl; } // 统计值为 2 的出现次数 std::cout << "Count of 2: " << myMultiset.count(2) << std::endl; // 删除值为 2 的所有元素 myMultiset.erase(2); // 遍历 multiset for (const auto& value : myMultiset) { std::cout << value << " "; } std::cout << std::endl; return 0; } ``` #### 5. 注意事项 - 如果元素是自定义类型(如结构体),必须提供有效的比较函数以确保严格弱序关系[^3]。 - 默认情况下,`std::multiset` 按照元素的升序排列。如果需要降序排列,可以在比较函数中反转逻辑。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值