因此,我们有set<string> a
和set<string> b
,我们希望得到std::set<string> c
这将包含将代表a - b
(意思是什么,从a
离开,如果我们从它b
删除所有项目,如果b
包含超过a
或项目中不存在的项目a
我们希望保持他们的数字都这样简单的数学:5-6 = 0
而3-2 = 1
)如何获取两个std :: set的元素之间的差异<string>?
3
A
回答
11
我想你想从<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;
}
1
这应该工作,我猜。
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)
}
}
3
相关问题
- 1. 获取两个NSStrings之间的差异
- 2. 如何获取两个JSON之间的差异?
- 3. 获得两个表之间的差异?
- 4. 两个内联块之间的差距<span>元素
- 5. 获取目标C中两个NSString之间的差异时间
- 6. Python - 列表元素之间的差异
- 7. 如何在iPhone中获取两天之间的差异
- 8. 从2个REF'd列获取两个日期之间的差异
- 9. Set()和RaisePropertyChanged()之间的差异
- 10. 获取两个子查询的计数之间的差异
- 11. 如何获得两个元素子列表中的两个列表之间的对称差异?
- 12. 从多个文件读取时元素之间的差异
- 13. 如何获得两个日期之间的星期差异
- 14. 如何获得java中两个zip文件之间的差异
- 15. 如何获得两个日期之间的差异?
- 16. 如何获得两个数据表之间的差异
- 17. 如何获得两个日期之间的差异?
- 18. 如何获得两个日期之间的差异?
- 19. 如何获得PHP中两个日期之间的差异?
- 20. 从DatePicker获取两个日期之间的差异
- 21. 获取两个标题之间的差异
- 22. 获取两个日期对象之间的小时差异?
- 23. 获取两个日期之间的差异
- 24. 使用LINQ获取两个表之间的差异
- 25. 获取两个查询之间的数据差异
- 26. 获取两个存储库之间的差异
- 27. Svnkit:远程获取两个提交之间的差异版本
- 28. 获取Microsoft Access报告中两个计数之间的差异
- 29. Mysql - 获取两个连续值之间的差异
- 30. 获取python中两个词典之间的差异
find找不到'NULL',当它找不到元素时,它返回你传入的“end”迭代器(在本例中为'b.end()')。 – Dawson
有人能解释我哪里出错了吗? – Mahesh
@Toolbox - 好的。纠正。 – Mahesh