简体   繁体   English

填充结构的C ++向量的首选方法

[英]Preferred way of filling up a C++ vector of structs

Alternative 1, reusing a temporary variable: 备选方案1,重用临时变量:

Sticker sticker;
sticker.x = x + foreground.x;
sticker.y = foreground.y;
sticker.width = foreground.width;
sticker.height = foreground.height;
board.push_back(sticker);

sticker.x = x + outline.x;
sticker.y = outline.y;
sticker.width = outline.width;
sticker.height = outline.height;
board.push_back(sticker);

Alternative 2, scoping the temporary variable: 备选方案2,确定临时变量的范围:

{
 Sticker sticker;
 sticker.x = x + foreground.x;
 sticker.y = foreground.y;
 sticker.width = foreground.width;
 sticker.height = foreground.height;
 board.push_back(sticker);
}

{
 Sticker sticker;
 sticker.x = x + outline.x;
 sticker.y = outline.y;
 sticker.width = outline.width;
 sticker.height = outline.height;
 board.push_back(sticker);
}

Alternative 3, writing straight to the vector memory: 备选方案3,直接写入矢量存储器:

{
 board.push_back(Sticker());
 Sticker &sticker = board.back();
 sticker.x = x + foreground.x;
 sticker.y = foreground.y;
 sticker.width = foreground.width;
 sticker.height = foreground.height;
}

{
 board.push_back(Sticker());
 Sticker &sticker = board.back();
 sticker.x = x + outline.x;
 sticker.y = outline.y;
 sticker.width = outline.width;
 sticker.height = outline.height;
}

Which approach do you prefer? 你更喜欢哪种方法?

Edit: For the sake of this discussion, assume that the assignments have to be made one by one outside of a constructor 编辑:为了便于讨论,假设必须在构造函数之外逐个进行赋值

My option - give Sticker a constructor that takes the parameters. 我的选择 - 给Sticker一个带参数的构造函数。 then: 然后:

board.push_back( Sticker( outline.x, foo.bar, etc. ) );  

Edit: Code to illustrate constructor parameter names: 编辑:用于说明构造函数参数名称的代码:

#include <iostream>
using namespace std;

struct S {
    int a, b;
    S( int a, int b ) : a(a), b(b) {
    }
};

int main() {    
    S s( 1, 2);
    cout << s.a << " " << s.b << endl;
}

board.resize(sticker_count); board.resize(sticker_count);

Then iterate through all the vector and set parameters. 然后遍历所有向量并设置参数。

Alternative 1. Why create a scope just for a variable? 备选方案1.为什么仅为变量创建范围? There is usually an enclosing scope nearby (at the minimum, you should keep your functions/procedures small so that will scope it). 附近通常有一个封闭的范围(至少,你应该保持你的功能/程序小,以便它的范围)。

Why? 为什么? You can create a shorter variable name eg st in this case. 在这种情况下,您可以创建一个较短的变量名称,例如st。 Since the assignment will be nearby there should be no loss in clarity. 由于任务将在附近,因此不应有明显的损失。 Actually it will look simpler and cleaner. 实际上它看起来更简单,更清洁。

Also, if the vector needs to be dereferenced/accessed from several other levels of indirection, then it will also simplify the code. 此外,如果需要从几个其他间接级别取消引用/访问向量,那么它也将简化代码。

How about winforms style: winforms风格怎么样:

// Class members

Sticker sticker1;
Sticker sticker2;
Board board;

// Initialization 

void InitBoard()
{
    sticker1.x = x + foreground.x;
    sticker1.y = foreground.y;
    sticker1.width = foreground.width;
    sticker1.height = foreground.height;

    sticker2.x = x + outline.x;
    sticker2.y = outline.y;
    sticker2.width = outline.width;
    sticker2.height = outline.height;

    // Add to board
    board.push_back(sticker1);
    board.push_back(sticker2);
}

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

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