使用实例根据特定数据筛选std::set
Filtering a std::set based on specific data
我有一个类的std::集合,它存储了一些主数据。下面是我的集合的样子:
std::set<TBigClass, TBigClassComparer> sSet;
class TBigClassComparer
{
public:
bool operator()(const TBigClass s1, const TBigClass s2) const
{
//comparison logic goes here
}
};
现在我想根据TBigClass的一些字段过滤这个集合中的数据,并将其存储在另一个集合中以供操作。
std::set<int>::iterator it;
for (it=sSet.begin(); it!=sSet.end(); ++it)
{
//all the records with *it.some_integer_element == 1)
//needs to be put in another set for some data manipulation
}
谁能告诉我一个有效的方法来完成这个?我没有安装任何库,所以详细使用boost的解决方案没有帮助。
更新:我正在开发c++ 98环境。
感谢您的阅读!
您可以使用std::copy_if
struct Condition {
bool operator()(const T & value) {
// predicate here
}
};
std::set<T> oldSet, newSet;
std::copy_if(oldSet.begin(), oldSet.end(), std::inserter(newSet, newSet.end()), Condition());
// or
std::copy_if(oldSet.begin(), oldSet.end(), std::inserter(newSet, newSet.end()), [](const T & value){/*predicate here*/});
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 不需要更改筛选器算法中的数据
- 根据数据成员对对象数组进行排序或筛选
- 筛选实时数据时出现QSortFilterProxyModel问题
- 筛选器重复列表-列出重复数据删除
- 为什么启用 Windows 基本筛选引擎服务后,我的应用程序无法接收 UDP 数据包?
- 在C#/C++中创建数据包筛选器
- 使用实例根据特定数据筛选std::set