Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用于迭代多个集合的数据结构_C++_For Loop_Templates_Iterator - Fatal编程技术网

C++ 用于迭代多个集合的数据结构

C++ 用于迭代多个集合的数据结构,c++,for-loop,templates,iterator,C++,For Loop,Templates,Iterator,我有一些有时间戳的对象,比如: class data_point { public: data_point(time_t _ts, char _data):timestamp(_ts), data(_data) {} time_t timestamp; char data; } 假设这些数据点有N个集合。对于每个集合,您知道它们是按时间顺序排列的。我想做的是迭代这些集合,这样我将为每个数据点获得一次回调,但顺序正确。基本上,我正在寻找一个可以用N个

我有一些有时间戳的对象,比如:

class data_point
{
     public:
     data_point(time_t _ts, char _data):timestamp(_ts), data(_data) {}
     time_t timestamp;
     char    data;
}
假设这些数据点有N个集合。对于每个集合,您知道它们是按时间顺序排列的。我想做的是迭代这些集合,这样我将为每个数据点获得一次回调,但顺序正确。基本上,我正在寻找一个可以用N个集合初始化的数据结构,并返回一个迭代器,该迭代器将允许遍历这些集合,选择具有“最低”时间戳的下一项,优先于首先添加到集合中的集合。理想情况下,代码可以如下所示:

std::vector<data_point> a1 = { data_point(1, 'a'), data_point(6, 'b'), data_point(11, 'c') };
std::vector<data_point> a2 = { data_point(1, 'd'), data_point(8, 'e'), data_point(12, 'f') };
std::vector<data_point> a3 = { data_point(1, 'g'), data_point(9, 'h'), data_point(11, 'i') };

SomeCollection c = { a1, a2, a3 }; 
// or possibly if needed
// SomeCollection c; c.add(a1); c.add(a2); c.add(a3);
for(auto &d: c)
{
     std::cout << d.data << std::endl;
}

我可以编写自己的类,但不知道是否已经存在类似的类。

为什么不创建一个集合集合,即
std::vector dataPointCollectors


将数据添加到这些集合中,然后迭代并打印内容

为什么不创建集合集合,即
std::vector dataPointCollectors


将数据添加到这些文件中,然后迭代并打印内容

您可以从每个输入重复插入和
std::inplace\u merge

std::vector<data_point> c;

for (auto * a : { &a1, &a2, &a3 })
{
    auto mid = c.size();
    c.insert(c.end(), a->begin(), a->end());
    std::inplace_merge(c.begin(), c.begin() + mid, c.end());
}
std::向量c;
对于(自动*a:{&a1,&a2,&a3})
{
自动中间=c.尺寸();
c、 插入(c.end(),a->begin(),a->end());
std::in place_merge(c.begin(),c.begin()+mid,c.end());
}

您可以从每个输入重复插入和
std::inplace\u merge

std::vector<data_point> c;

for (auto * a : { &a1, &a2, &a3 })
{
    auto mid = c.size();
    c.insert(c.end(), a->begin(), a->end());
    std::inplace_merge(c.begin(), c.begin() + mid, c.end());
}
std::向量c;
对于(自动*a:{&a1,&a2,&a3})
{
自动中间=c.尺寸();
c、 插入(c.end(),a->begin(),a->end());
std::in place_merge(c.begin(),c.begin()+mid,c.end());
}

是否打算保持N个集合不变?或者你只是想按顺序访问它们的值以便(比如)打印?是的,保留初始集合,但有一个迭代器将它们视为队列,其中++将你移动到队列最低的所有集合中的下一项。输入是否保证有序?@Caleth-是的,假设每个集合,在本例中为向量,已经准备好了。理想情况下,这也适用于其他集合。是否打算保持N个集合不变?或者你只是想按顺序访问它们的值以便(比如)打印?是的,保留初始集合,但有一个迭代器将它们视为队列,其中++将你移动到队列最低的所有集合中的下一项。输入是否保证有序?@Caleth-是的,假设每个集合,在本例中为向量,已经准备好了。理想情况下,这也适用于其他集合。这似乎连接了集合。我想把它们都当作队列,但是有一个迭代器,它可以在所有时间戳最低的集合中移动到下一个项目。我想把它们都当作队列,但是有一个迭代器,它可以在所有集合中以最低的时间戳移动到下一个项目。这并没有给我一个迭代器,它可以像原始帖子中所描述的那样工作。这并没有给我一个迭代器,它可以像原始帖子中所描述的那样工作。