简体   繁体   English

C ++:在循环内部或外部声明向量

[英]C++: Declare a vector inside or outside of a loop

I would like to loop over several random combinations. 我想遍历几种随机组合。 Currently, I define a vector v with the numbers 1 to n outside the loop, shuffle v inside the loop and define a new vector combination inside the loop. 目前,我在循环外定义了一个向量v ,其编号为1n在循环内随机排列了v ,并在循环内定义了一个新的向量combination

int k = 50;
int n = 100;
int sampleSize=100;
std::vector<int> v(n);
//std::vector<int> combination(k); //Would it be better to declare here?
std::iota(v.begin(), v.end(), 0);
unsigned seed = 42;

for (int i=0; i<sampleSize; i++) {
    std::shuffle (v.begin(), v.end(), std::default_random_engine(seed));
    std::vector<int> combination(v.begin(), v.begin() + k);
};

It seems weird to me that I define combination again in every iteration of the for loop. 在我看来,在for循环的每次迭代中再次定义combination都是很奇怪的。 Would it make sense to declare combination outside of the for loop and then assign new values to it in every iteration? 在for循环外声明combination ,然后在每次迭代中为其分配新值是否有意义? If so, what would be a good way to assign those new values to combination ? 如果是这样,将这些新值分配给combination的好方法是什么? So far, I have only used push_back() to append new values to a vector. 到目前为止,我仅使用push_back()将新值附加到向量。

There are multiple ways of assigning values in a vector, besides push_back : 除了push_back之外,向量中还有多种分配值的方法:

  • The [] operator gives you read/write access to an individual element of the vector, such that you can do v[5] = 10 . []运算符使您可以对向量的单个元素进行读/写访问,以便可以执行v[5] = 10 Put this in a for loop, access the elements based on the index of the loop. 将其放在for循环中,根据循环索引访问元素。
  • The = operator copies all the elements from one vector to another. =运算符将所有元素从一个向量复制到另一向量。
  • std::copy copies a range of elements. std::copy复制一系列元素。

There are probably much more, these are some of the ways I could think of. 可能还有更多,这些是我能想到的一些方法。

Going back to the initial question, what your loop does now is: 回到最初的问题,您的循环现在要做的是:

  • Create a new vector, which involves allocating memory for it and copying the elements 创建一个新的向量,这涉及为其分配内存并复制元素
  • Release the memory of the vector 释放向量的记忆

This happens at each iteration. 这在每次迭代时发生。 Now, even if you declare it outside the loop, you still have to copy the elements (you have to use something like std::copy probably. So the penalty you get is to allocate and release the memory at each iteration. 现在,即使您在循环外声明它,您仍然必须复制元素(可能必须使用类似std::copy类的东西。因此,您要付出的代价是在每次迭代时分配和释放内存。

Technically, it would be more efficient to define it outside the loop. 从技术上讲,在循环外定义它会更有效。 However, the decision on whether to actually place it outside the loop has to consider the tradeoff between the performance improvement you get and the readability penalty you get by defining it outside the loop. 但是,是否将其实际放置在循环之外的决定必须考虑您在性能改进和通过在循环外部进行定义而获得的可读性损失之间的权衡。

In general, you want the scope of the variables (ie the part of the program where the variables can be accessed) to be as small as possible. 通常,您希望变量的范围(即程序中可以访问变量的部分)尽可能小。 In this specific case, unless it is a performance-critical section and it makes sense to be this way (from your snippet, is not very clear what you want to do with that std::vector inside the loop) and the vectors are reasonably small such that the memory allocation/releasing is not very slow, I would leave it in the loop. 在这种特定情况下,除非它是对性能至关重要的部分,并且这样做是有意义的(从您的代码段中,您并不清楚要对循环内的std::vector做什么),并且这些向量是合理的很小,以至于内存分配/释放不是很慢,我会将其留在循环中。

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

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