我正在使用一些从四元元组创建集合的 C++ 代码。
我想概括代码,以便可以将任何量级的元组添加到集合中。
有没有办法为不确定数量的元素的元组创建 typedef?
为了与我所从事的项目兼容,我希望通过C++11功能实现这一点。如果可能的话,我希望保留 typedef,以便它与编程的其余部分保持一致。
我查看了很多关于可变参数函数和模板的信息,但我仍然不确定如何继续。
#include <set>
#include <functional>
#include <iostream>
typedef std::set<std::tuple<int, int, int, int>> QuaternarySet;
typedef std::tuple<int, int, int, int> QuaternaryTuple;
int main () {
QuaternaryTuple x = std::make_tuple(1, 2, 3, 4);
QuaternaryTuple y = std::make_tuple(5, 6, 7, 8);
QuaternaryTuple z = std::make_tuple(1, 2, 3, 4);
QuaternarySet s;
s.insert(x);
s.insert(y);
s.insert(z);
std::cout << s.size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我正在寻找一些允许我定义类似内容的代码,但我不确定用什么来替换省略号。
typedef std::set<std::tuple<int ...>> IndefiniteSet;
typedef std::tuple<int ...> IndefiniteTuple;
Run Code Online (Sandbox Code Playgroud)
也就是说,我需要一个单一类型的运行时可变大小容器,int.
我需要一个单一类型的运行时可变大小容器,int。
If it's "run-time variable size", use std::vector<int>. If it's known in compile time, use std::array<int, n>, where n is your size.
std::tuple is meant for a few of values of possibly different types, especially in a generic context; you can think about it more as an unnamed struct than a collection.
| 归档时间: |
|
| 查看次数: |
672 次 |
| 最近记录: |