首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从std::vector<std::function<...>>中删除std::函数的C++

从std::vector<std::function<...>>中删除std::函数的C++

在C++中,std::vector是一个动态数组容器,std::function是一个通用的函数封装器。要从std::vector<std::function<...>>中删除std::函数,可以使用std::remove_if函数结合lambda表达式来实现。

下面是一个示例代码:

代码语言:txt
复制
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>

int main() {
    std::vector<std::function<void()>> functions;

    // 添加一些函数到vector中
    functions.push_back([]() { std::cout << "函数1" << std::endl; });
    functions.push_back([]() { std::cout << "函数2" << std::endl; });
    functions.push_back([]() { std::cout << "函数3" << std::endl; });

    // 删除特定的函数
    std::function<void()> functionToRemove = []() { std::cout << "函数2" << std::endl; };
    functions.erase(std::remove_if(functions.begin(), functions.end(),
                                   [&](const std::function<void()>& func) {
                                       return func.target<void()>() == functionToRemove.target<void()>();
                                   }),
                    functions.end());

    // 执行剩余的函数
    for (const auto& func : functions) {
        func();
    }

    return 0;
}

在上面的示例代码中,我们首先创建了一个std::vector<std::function<void()>>,并向其中添加了三个函数。然后,我们定义了一个要删除的函数functionToRemove,并使用std::remove_if函数结合lambda表达式来删除vector中与functionToRemove相等的函数。最后,我们遍历剩余的函数并执行它们。

这是一个简单的示例,你可以根据实际需求进行修改和扩展。关于std::vector、std::function和std::remove_if等的更多详细信息,你可以参考C++标准库的相关文档。

腾讯云相关产品和产品介绍链接地址:

  • 腾讯云C++ SDK:https://cloud.tencent.com/document/product/876
  • 云函数(Serverless):https://cloud.tencent.com/product/scf
  • 云服务器(CVM):https://cloud.tencent.com/product/cvm
  • 云数据库MySQL版:https://cloud.tencent.com/product/cdb_mysql
  • 人工智能平台(AI Lab):https://cloud.tencent.com/product/ailab
  • 物联网平台(IoT Hub):https://cloud.tencent.com/product/iothub
  • 移动开发平台(MTP):https://cloud.tencent.com/product/mtp
  • 云存储(COS):https://cloud.tencent.com/product/cos
  • 区块链服务(BCS):https://cloud.tencent.com/product/bcs
  • 腾讯云元宇宙:https://cloud.tencent.com/product/tencent-meta-universe
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

学了C++不会STL,简直少了左膀右臂

容器(Container): 是一种数据结构,如list,vector,和deques ,以模板类的方法提供。为了访问容器中的数据,可以使用由容器类输出的迭代器; 迭代器(Iterator): 提供了访问容器中对象的方法。例如,可以使用一对迭代器指定list或vector中的一定范围的对象。迭代器就如同一个指针。事实上,C++的指针也是一种迭代器。但是,迭代器也可以是那些定了operator*()以及其他类似于指针的操作符地方法的类对象; 算法(Algorithm): 是用来操作容器中的数据的模板函数。例如,STL用sort()来对一个vector中的数据进行排序,用find()来搜索一个list中的对象,函数本身与他们操作的数据的结构和类型无关,因此他们可以在从简单数组到高度复杂容器的任何数据结构上使用; 仿函数(Functor) 适配器(Adaptor) 分配器(allocator) 仿函数、适配器、与分配器用的比较少,甚至没用过!在这里不做说明,有兴趣可以自己学习一下,那个东西C++软件工程可能用的比较多。

02

ACM竞赛常用STL(二)之STL--algorithm

<algorithm>无疑是STL 中最大的一个头文件,它是由一大堆模板函数组成的。 下面列举出<algorithm>中的模板函数: adjacent_find / binary_search / copy / copy_backward / count / count_if / equal / equal_range / fill / fill_n / find / find_end / find_first_of / find_if / for_each / generate / generate_n / includes / inplace_merge / iter_swap / lexicographical_compare / lower_bound / make_heap / max / max_element / merge / min / min_element / mismatch / next_permutation / nth_element / partial_sort / partial_sort_copy / partition / pop_heap / prev_permutation / push_heap / random_shuffle / remove / remove_copy / remove_copy_if / remove_if / replace / replace_copy / replace_copy_if / replace_if / reverse / reverse_copy / rotate / rotate_copy / search / search_n / set_difference / set_intersection / set_symmetric_difference / set_union / sort / sort_heap / stable_partition / stable_sort / swap / swap_ranges / transform / unique / unique_copy / upper_bound 如果详细叙述每一个模板函数的使用,足够写一本书的了。还是来看几个简单 的示例程序吧。 示例程序之一,for_each 遍历容器:

03
领券