简体   繁体   English

在每次迭代中创建一个结构

[英]Create a struct in each iteration

I want to store queues in a queue structure from the stl library. 我想将队列存储在stl库的队列结构中。 For some reasons I have to store a queue in each iteration of my program, but I think that create a new queue over and over again is too expensive. 由于某些原因,我必须在程序的每次迭代中存储一个队列,但是我认为一遍又一遍地创建一个新队列太昂贵了。

I only know two ways to do this. 我只知道两种方法。 The first one: 第一个:

#include <iostream>
#include <deque>
using namespace std;
int main () {
    unsigned int limit, i = 0;
    deque<deque<int> > container;
    cin >> limit;
    for ( ; i < limit; i++ ) {
        deque<int> values;
        //set some values in the values structure.
        setValues(values, i);
        container.push(values);
    }
}

the second one: 第二个:

#include <iostream>
#include <deque>
using namespace std;
int main () {
    unsigned int limit, i = 0;
    deque<deque<int> > container;
    deque<int> values;
    cin >> limit;
    for ( ; i < limit; i++ ) {
        //reset the structure, i.e. delete al the previous values.
        reset(values);
        //set some values in the values structure.
        setValues(values, i);
        container.push(values);
    }
}

the problem here is that I don't know any function for reset my queue, or maybe I have to do values=NULL ? 这里的问题是我不知道有什么功能可以重置队列,或者我必须做values=NULL

How can I do this in an efficient way? 如何有效地做到这一点?

Thanks! 谢谢! :D :d

You can push an empty deque in the loop, get a reference to it, and then add items to it. 您可以在循环中推送一个空的deque ,获取对它的引用,然后向其中添加项目。

#include <iostream>
#include <deque>

using namespace std;

int main () {
    unsigned int limit, i = 0;
    deque<deque<int> > container;
    cin >> limit;
    for ( ; i < limit; i++ ) {
        container.push_back(deque<int>());
        deque<int>& values = container.back();
        //set some values in the values structure.
        setValues(values, i);    }
}

You should check in debugger what your compiler is actually doing when you make copies of deque. 制作双端队列副本时,应在调试器中检查编译器的实际作用。 I have checked in VS2013 and its all move semantics - as expected. 我已经检查了VS2013及其所有动作的语义-符合预期。 This is a test code: 这是一个测试代码:

std::deque<int> getValues() {
    std::deque<int> res;
    res.push_back(1);
    return res; // deque(_Myt&& _Right) called, also other compilers might use RVO
}

std::deque<int> ff;
std::deque<std::deque<int>> aff;
aff.push_back(getValues()); // void push_back(value_type&& _Val) called

at first it looks like a lot of copying, but actually in both problematic places move semantics are used, and only pointers of temporary objects are copied, so its all super fast. 最初看起来很多复制,但是实际上在两个有问题的地方都使用了移动语义,并且仅复制了临时对象的指针,因此所有操作都非常快。

But maybe you are stuct in pre c++11 world? 但是也许您在c ++ 11之前的世界中感到困惑? At least this fragment 至少这个片段

  deque<deque<int> > container; 
                 ^^^

gives such a hint. 给出了这样的提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM