如何获得两个std::set的元素之间的差异<string>?
How to get difference between elements of two std::set<string>?
我们有set<string> a
和set<string> b
,我们想要得到std::set<string> c
,它将包含代表a - b
的项目(意思是如果我们从b
中删除所有项目,则a
中剩下的项目,如果b
包含多于a
或a
中不存在的项目,我们希望保持它们类似于这样简单的数学数字:5-6 = 0
和3-2 = 1
)
我认为你想从<algorithm>
中得到std::set_difference()
。
#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <iterator>
using namespace std;
set<string> a;
set<string> b;
set<string> result;
int main()
{
a.insert("one");
a.insert("two");
a.insert("three");
b.insert("a");
b.insert("b");
b.insert("three");
set_difference( a.begin(), a.end(), b.begin(), b.end(), inserter(result, result.begin()));
cout << "Difference" << endl << "-------------" << endl;
for (set<string>::const_iterator i = result.begin(); i != result.end(); ++i) {
cout << *i << endl;
}
result.clear();
set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), inserter(result, result.begin()));
cout << "Symmetric Difference" << endl << "-------------" << endl;
for (set<string>::const_iterator i = result.begin(); i != result.end(); ++i) {
cout << *i << endl;
}
return 0;
}
假设您指的是集合之差:
set_difference
如果你指的是元素之间的比较,那么用一般或简单的方式来回答是不可能的。答案是非常具体的你的问题,这是没有指定或明确的。
我想这应该行得通。
for( set<string> :: iterator it = a.begin(); it != a.end(); ++it )
{
set<string>:: iterator iter = find( b.begin(), b.end(), *it );
if( iter == b.end() )
{ // ^^^^^^^ Note: find returns b.end() if it does not find anything.
c.insert(*iter)
}
}
相关文章:
- 正在查找文档以获得PS4平台的C++中的设备信息
- 如何从C++中的依赖类型中获得它所依赖的类型
- 欧拉项目#8答案是大以获得有效答案
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何使用C/C++在MacOSX中获得键盘布局
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 如何在不产生任何垃圾的情况下获得C中的像素
- 有没有一种方法可以在编译时获得作用域类名
- 如何在C++中获得"静态纯虚拟"功能?
- Python中的for循环与C++有何不同
- 如何防止clang格式在流运算符调用之间添加换行符<<
- 如何设置一个范围来提取我想要获得的信息
- 在多次运行中获得一致的callgrind输出
- 通过错误处理,在C++中可靠地获得用户十六进制输入
- <<操作员在下面的行中工作
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 在我的代码中,获得最大的Pair Wise产品C++和输出并不总是正确的
- 当用户在qtablewidget中输入单元格时,如何获得信号?C++
- 重载运算符<<用于模板类.即使使用好友关键字也无法获得私人会员