使用std::find_if提取序列容器的子串

一个需求是这样的,一个vector容器中,我需要提取满足一定条件的元素的序列。就比如,一个树形结构,我把该接口拍扁成vector容器,每个节点都有一个惟一ID。

以下就是根据特定的ID查找节点下的子节点:

 1 NodeList OrgTreeParser::findChildsById(const std::string &id)
 2 {
 3     NodeList list;
 4
 5     auto iter = std::find_if(std::begin(m_list), std::end(m_list),
 6         [&](const OrgTreeNode & item) -> bool {
 7         return item.parent_id.compare(id) == 0;
 8     }
 9     );
10     while (iter != m_list.end())
11     {
12         OrgTreeNode node;
13         node.id = iter->id;
14         node.parent_id = iter->parent_id;
15         node.level_id = iter->level_id;
16         node.name = iter->name;
17         node.addr = iter->addr;
18         node.description = iter->description;
19         list.push_back(node);
20
21         iter = std::find_if(std::next(iter), std::end(m_list),
22             [&](const OrgTreeNode & item) -> bool {
23             return item.parent_id.compare(id) == 0;
24         }
25         );
26     }
27
28     return list;
29 }

references:

https://stackoverflow.com/questions/33226202/what-is-the-best-way-to-convert-a-stdfind-if-on-a-vector-to-a-loop

时间: 2024-08-25 04:48:04

使用std::find_if提取序列容器的子串的相关文章

第17章 string基本字符序列容器

/* 第17章 string基本字符序列容器 17.1 string技术原理 17.2 string应用基础 17.3 本章小结 */ // 第17章 string基本字符序列容器 // 17.1 string技术原理 -------------------------------------------------------------------------------------- // 17.2 string应用基础 ----------------------------------

string基本字符序列容器

C语言每天提供专门的字符串类型,需要通过字符数组才能对字符串进行存储和处理.在标准C++中,字符串类由C++ STL实现.string是一个基于字符的序列容器,具有vector向量一样的内部线性结构,字符逐一写入容器,最后以null字符结尾.跟传统的char*字符数组相比,string提供了丰富的函数用于字符的添加.删除.替换.查找和比较等. 创建string对象 主要有以下几种方式. (1)    string() string s; (2)    string(conststring&s,s

string基本字符序列容器(竞赛时常用的使用方法总结)

C语言只提供了一个char类型用来处理字符,而对于字符串,只能通过字符串数组来处理,而C++STL提供了string基本字符序列容器来处理字符串,可以将其理解为字符串类,它提供了添加,删除,替换.查找和比较等丰富.简洁的方法. 下面是在编写代码是的具体应用. 1 //关于C++ STL string基本字符系列容器的学习,看别人的代码一百遍,不如自己动手写一遍. 2 #include <string> 3 #include <vector> 4 #include <iostr

c++ stl algorithm: std::find, std::find_if

td::find: 查找容器元素, find只能查找容器元素为<基本数据类型> [cpp]  view plain copy #include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v; for (int i = 0; i < 10; ++i) v.push_back(i); std::vector<int>

C++线性序列容器&lt;vector&gt;简单总结

C++线性序列容器<vector>简单总结 vector是一个长度可变的数组,使用的时候无须声明上限,随着元素的增加,Vector的长度会自动增加:Vector类提供额外的方法来增加.删除元素,比数组操作高效. 头文件:#include <vector> 命名空间:using namespace std:vector 构造函数 vector<int>vec_int;         // 创建一个整形元素的vector vector<string>vec_s

STL 序列容器

转自时习之 STL中大家最耳熟能详的可能就是容器,容器大致可以分为两类,序列型容器(SequenceContainer)和关联型容器(AssociativeContainer)这里介绍STL中的各种序列型容器和相关的容器适配器.主要内容包括 std::vector std::array std::deque std::queue std::stack std::priority_queue std::list std::forward_list std::vector 1)初始化 int ini

STL源码剖析—序列容器

对于STL中的容器,存在一定的内含关系,例如,heap内含一个vector,priority-queue内含一个hep,stack和queue都含有一个deque,set/map/multiset/multimap都内含一个RB-tree,hash_x都内含一个hashtable. 对于序列容器来说,vector和list的插入都是在指向迭代器之前进行插入.同时需要注意的就是,对于vector来说,调用erase()函数也就是清除了几个连续空间上的元素,调用其析构函数,并没用释放原来元素占用的内

实战c++中的vector系列--对vector&amp;lt;自己定义类&amp;gt;使用std::find 和 std::find_if 算法

之前博客讲了一些关于std::find和std::find_ if的一些使用方法.可是没有讲述对于vector中存储的是自己定义的类.那么怎么样使用std::find和std::find_if进行查找呢? 先定义一个类: class Item { private: std::string m_ItemId; int m_Price; int m_Count; public: Item(std::string id, int price, int count): m_ItemId(id), m_C

实战c++中的vector系列--对vector&lt;自定义类&gt;使用std::find 和 std::find_if 算法

之前博客讲了一些关于std::find和std::find_ if的一些用法,但是没有讲述对于vector中存储的是自定义的类,那么怎么样使用std::find和std::find_if进行查找呢? 先定义一个类: class Item { private: std::string m_ItemId; int m_Price; int m_Count; public: Item(std::string id, int price, int count): m_ItemId(id), m_Coun