C++ 在声明中合并两个常量'std::set'(不在运行时)

C++ 在声明中合并两个常量'std::set'(不在运行时),c++,c++11,data-structures,c++-standard-library,stdset,C++,C++11,Data Structures,C++ Standard Library,Stdset,我试图优雅地声明一个常量std::set对象,它将是另外两个常量std::set对象的合并 #include <set> const std::set<int> set_one = { 1,2,3 }; const std::set<int> set_two = { 11,15 }; const std::set<int> set_all = { 1,2,3,11,15 }; // this is not very elegant, duplic

我试图优雅地声明一个常量
std::set
对象,它将是另外两个常量
std::set
对象的合并

#include <set>

const std::set<int> set_one = { 1,2,3 };
const std::set<int> set_two = { 11,15 };
const std::set<int> set_all = { 1,2,3,11,15 }; // this is not very elegant, duplication
  • 所有对象都是严格的常量
  • 这两个源集中没有重叠的值,因此唯一性不会成为问题
  • 我知道如何在运行时合并集合,这不是我想要的
  • 我真的试图避免使用这样的宏:
  • #包括
    #定义集合1、2、3
    #定义集合2 11、15
    const std::set_one={set_one};
    const std::set_two={set_two};
    const std::set set_all={set_ONE,set_TWO};
    
    您可以将它们打包到lambda中并立即调用(即

    const std::set_all=[&set_one,&set_two](){
    std::set set{set_one.cbegin(),set_one.cend()};
    set.insert(set_two.cbegin(),set_two.cend());
    返回集;
    }(); // ---> 叫lambda!
    
    但是,如果在中有集合,则应使用lambda,它将两个集合作为参数

    #include <set>
    
    using Set = std::set<int>;    // type alias
    const Set set_one = { 1,2,3 };
    const Set set_two = { 11,15 };
    
    const Set set_all = [](const Set& setOne, const Set& setTwo)
    {
       Set set{ setOne.cbegin(), setOne.cend() };
       set.insert(setTwo.cbegin(), setTwo.cend());
       return set;
    }(set_one, set_two); // ---> call the lambda with those two sets!
    
    #包括
    使用Set=std::Set;//类型别名
    常数集集合_one={1,2,3};
    常数集集合二={11,15};
    常量集集合\所有=[](常量集和集合一,常量集和集合二)
    {
    Set{setOne.cbegin(),setOne.cend()};
    set.insert(setTwo.cbegin(),setTwo.cend());
    返回集;
    }(一套,二套);//-->用这两套打电话给lambda!
    
    或者干脆

    const std::set<int> set_all = []()
    {
       std::set<int> set{ set_one.cbegin(),set_one.cend() };
       set.insert(set_two.cbegin(), set_two.cend());
       return set;
    }(); // ---> call the lambda!
    
    const std::set_all=[]()
    {
    std::set set{set_one.cbegin(),set_one.cend()};
    set.insert(set_two.cbegin(),set_two.cend());
    返回集;
    }(); // ---> 叫lambda!
    

    我知道如何在运行时合并集合,这不是我想要的 因为


    ,您不能在编译时创建它所使用的。因此,一切都在运行时发生。即使是上面的lambda。

    您的
    std::set
    都不是在编译时创建的(它们都是在运行时创建的,因为它们是分配的)。
    std::set
    使用动态分配。这意味着它是在运行时创建的,至少在可能的C++20之前是这样。删除捕获(
    &
    )如果这些集合是全局变量(就像OP的代码中的情况一样),那么您应该澄清这不是“非运行时”@Kevin您为什么会这样假设?这是一个罕见的极端案例。不值得列出它们中的每一个,如果我们不想列出它们,为什么要提到这一个呢?@Fureeish因为这个错误:
    错误:非本地lambda表达式不能有捕获默认值
    (如果您尝试单独捕获:
    'set_one'无法捕获,因为它没有自动存储持续时间
    )@Kevin OP只给了我们一部分代码,他们没有提到任何有关globals的内容。我想这应该解决了。
    const std::set<int> set_all = [&set_one, &set_two]() {
       std::set<int> set{ set_one.cbegin(),set_one.cend() };
       set.insert(set_two.cbegin(), set_two.cend());
       return set;
    }(); // ---> call the lambda!
    
    #include <set>
    
    using Set = std::set<int>;    // type alias
    const Set set_one = { 1,2,3 };
    const Set set_two = { 11,15 };
    
    const Set set_all = [](const Set& setOne, const Set& setTwo)
    {
       Set set{ setOne.cbegin(), setOne.cend() };
       set.insert(setTwo.cbegin(), setTwo.cend());
       return set;
    }(set_one, set_two); // ---> call the lambda with those two sets!
    
    const std::set<int> set_all = []()
    {
       std::set<int> set{ set_one.cbegin(),set_one.cend() };
       set.insert(set_two.cbegin(), set_two.cend());
       return set;
    }(); // ---> call the lambda!