gpt4 book ai didi

c++ - 如何将参数列表存储到 vector ?

转载 作者:行者123 更新时间:2023-12-01 14:03:03 25 4
gpt4 key购买 nike

如何将可变参数构造函数参数存储到 vector ?
我失败的尝试示例:

class Combo 
{
public:
template <class... Args>
Combo(Args... args)
{
// this->keys_.push_back(args...);

// this->keys_.push_back(args)...;

// this->keys_.push_back(std::forward<Args>(args...));

//for (uint8_t arg : args...)
// this->keys_.push_back(arg);

// ???
}

private:
std::vector<uint8_t> keys_;
};

最佳答案

  • C++11

  • for(auto &&i: {args...}) keys.push_back(std::move(i));
  • C++17

  • (keys.push_back(args), ...);
  • 哦,对不起,我错过了显而易见的:

  • template<class... Args> Combo(Args... args): keys_{uint8_t(args)...} {}

    关于c++ - 如何将参数列表存储到 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63440181/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com