c++ vector<string> 集合做差集

本文介绍了如何使用C++ STL中的set和vector进行集合去重,并展示了如何通过set_difference计算两个集合的差集。强调在进行差集操作前,必须先确保集合元素去重且已排序,因为未排序或有重复元素可能会导致错误。示例代码详细展示了整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <vector>
#include <set>
#include <iostream>
//#include <iterator>
#include <algorithm>

using namespace std;
/**
 * 参考:
 * (20条消息) C++拾取——stl标准库中集合交集、并集、差集、对称差方法_方亮的专栏-优快云博客_c++ 取交集
 * https://blog.youkuaiyun.com/breaksoftware/article/details/88932820
 */

int main() {
	vector<string> a;
	a.push_back("10.0.0.1");
	a.push_back("10.0.0.5");
	a.push_back("10.0.0.6");
	a.push_back("10.0.0.2");
	a.push_back("10.0.0.2"); //注意有重复元素
	a.push_back("10.0.0.4");
	a.push_back("10.0.0.9");

	std::vector<string> b;
	b.push_back("10.0.0.2");
	b.push_back("10.0.0.5");
	b.push_back("10.0.0.6");
	b.push_back("10.0.0.4");
	b.push_back("10.0.0.7");

	//去掉重复元素
	set<string> sa(a.begin(), a.end());
	a.clear();
	a.resize(sa.size());
	a.assign(sa.begin(), sa.end());
	cout << "集合a 内元素去重后:" << endl;
	for (string iter : a) {
		cout << iter << endl;
	}

	//去掉重复元素
	set<string> sb(b.begin(), b.end());
	b.clear();
	b.resize(sb.size());
	b.assign(sb.begin(), sb.end());
	cout << "集合b 内元素去重后:" << endl;
	for (string iter : b) {
		cout << iter << endl;
	}

//	std::sort(a.begin(), a.end()); //如果没排序的话需要排序
//	std::sort(b.begin(), b.end()); //如果没排序的话需要排序
//	cout << "集合a 内元素排序后:" << endl;
//	for (string iter : a) {
//		cout << iter << endl;
//	}

//	cout << "集合b 内元素排序后:" << endl;
//	for (string iter : b) {
//		cout << iter << endl;
//	}

	std::vector<string> result;

	/*求两个集合的差集去重和排序不能少
	 *原因:Equivalent elements are treated individually, that is, if some element is found m times in [first1, last1) and n times in [first2, last2), it will be copied to d_first exactly std::max(m-n, 0) times. The resulting range cannot overlap with either of the input ranges.
	 */
	std::set_difference(a.begin(), a.end(), b.begin(), b.end(),
			std::back_inserter(result)); //差集求的是排序后的两个集合,所以求差集前一定要对两个集合进行排序,否则可能出错。

	cout << "集合a 和 b 取差集" << endl;
	for (string iter : result) {
		cout << iter << endl;
	}
	return 0;
}

这里需要强调的是在调用set_difference 对两个集合做差集操作前,两个集合一定是去重并且排序了的。这一点很重!不去重或者不排序,都会出现问题。

具体原因:

Equivalent elements are treated individually, that is, if some element is found m times in [first1, last1) and n times in [first2, last2), it will be copied to d_first exactly std::max(m-n, 0) times. The resulting range cannot overlap with either of the input ranges.

参考并感谢以下链接:

(20条消息) C++拾取——stl标准库中集合交集、并集、差集、对称差方法_方亮的专栏-优快云博客_c++ 取交集
https://blog.youkuaiyun.com/breaksoftware/article/details/88932820

C++中设计这样一个程序,可以分为以下几个步骤: 1. **导入所需库**:为了处理集合操作,需要使用标准模板库(STL),特别是`<set>`或`<unordered_set>`以及`<algorithm>`。 2. **定义数据结构**:创建`std::set`或`std::unordered_set`对象,用于存储用户输入的元素。 3. **获取用户输入**:使用`cin`从用户那里接收两个集合的元素,可能需要考虑错误处理,例如空值、非法字符等。 4. **创建集合**:将输入的元素添加到对应的集合中。 5. **计算差集**:对于两个集合A和B,差集(A - B)是指只属于A但不属于B的所有元素。可以使用`std::set`的`difference`成员函数来获得。 6. **计算对称差**:对称差(A Δ B)包含同时出现在A和B中的元素,以及仅在一个中出现的元素。可以用`std::set`的`symmetric_difference`成员函数来实现。 7. **计算笛卡尔积**:如果想计算两个集合的元素所有可能的配对组合,即笛卡尔积(A × B),可以使用`std::set`和`std::vector`的嵌套循环实现。 8. **输出结果**:遍历并打印出每个集合的操作结果。 9. **异常处理**:在整个过程中添加必要的异常处理,以防止因输入错误导致程序崩溃。 以下是部分伪代码示例: ```cpp #include <iostream> #include <set> #include <vector> std::set<int> setA, setB; std::vector<std::pair<int, int>> cartesianProduct; void getInput() { for (int i = 0; i < 2; ++i) { std::string input; // 获取用户输入并转换成整数 std::cin >> input; try { setA.insert(std::stoi(input)); setB.insert(std::stoi(input)); } catch (...) { // 处理输入错误 } } } // ... 其他函数如上面所述 ... int main() { getInput(); // 等待其他函数实现 std::cout << "差集: "; // 输出差集 // ... std::cout << "\n对称差: "; // 输出对称差 // ... std::cout << "\n笛卡尔积: "; // 输出笛卡尔积 // ... return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值