class block
{
private:
int xPos;
int yPos;
};
vector<block> stoneBlocks;
int stoneBlockXpos[] = {1, 2, 3, 4, 5, 6};
int stoneBlockYpos[] = {1, 2, 3, 4, 5, 6};
Basically, what I want to do is to assign the xPos
and yPos
the values of the arrays. What is the easiest way of doing this? I have these arrays in several if()
statements and they're pretty much the x and y coordinates for the level data of the game I'm making.
基本上,我想要做的是为xPos和yPos分配数组的值。这样做最简单的方法是什么?我在几个if()语句中有这些数组,它们几乎就是我正在制作的游戏关卡数据的x和y坐标。
The only thinkable solution I can come up with is to put a for()
loop in each if()
statement, and then assign the values that way. But, that is not going to be very efficient.
我能想到的唯一可想到的解决方案是在每个if()语句中放置一个for()循环,然后以这种方式分配值。但是,这不会非常有效。
Yes, it's all going to happen in the same method if that's neccessary to know. I'm creating these arrays in a "map creator" I made. It's a separate thing. The only way to write the things done in the creator is by printing the vector numbers to a text document in the format of arrays (one each for x and y) and pasting them in the game project's code.
是的,如果需要知道的话,这一切都会以同样的方式发生。我正在我制作的“地图创建者”中创建这些数组。这是一个单独的事情。在创建者中编写完成内容的唯一方法是将矢量编号以数组格式(x和y各一个)打印到文本文档中,并将其粘贴到游戏项目的代码中。
Thanks! :)
2 个解决方案
#1
1
Assuming your block
has a constructor taking two int
s, you could do something like this:
假设你的块有一个构造函数有两个整数,你可以做这样的事情:
std::transform(std::begin(stoneBlockXpos), std::end(stoneBlockXpos),
std::begin(stoneBlockYpos),
std::back_inserter(stoneBlocks),
[](int x, int y) { return block(x, y); });
(the same can be done with C++03 but it requires a few auxiliary functions).
(同样可以用C ++ 03完成,但它需要一些辅助功能)。
Assuming you need to do this often, you can create a small wrapper which does the same operations but also makes sure that both arrays have the correct size, e.g.:
假设您需要经常这样做,您可以创建一个小包装器,它执行相同的操作,但也确保两个数组都具有正确的大小,例如:
template <int Size>
std::vector<block>
makeBlocks(int (&xpos)[Size], int (&ypos)[Size])
{
std::vector<block> rc;
std::transform(std::begin(xpos), std::end(xpos),
std::begin(ypos),
std::back_inserter(rc),
[](int x, int y) { return block(x, y); });
return rc;
}
stoneBlocks = makeBlocks(stoneBlockXpos, stoneBlockYpos);
#2
1
If you just want to initialize the vector ONCE (and you have a C++11 compatible compiler), you can use this type of method:
如果您只想初始化向量ONCE(并且您有一个C ++ 11兼容的编译器),则可以使用以下类型的方法:
#include <vector>
#include <initializer_list>
using namespace std;
class block
{
public:
block(int x, int y) : xPos(x), yPos(y) {}
private:
int xPos;
int yPos;
};
int main()
{
static vector<block> stoneBlocks1 { {1, 1}, {2, 2}, {3, 3} };
static vector<block> stoneBlocks2 { {5, 6}, {7, 8}, {9, 10} };
...
}
#1
1
Assuming your block
has a constructor taking two int
s, you could do something like this:
假设你的块有一个构造函数有两个整数,你可以做这样的事情:
std::transform(std::begin(stoneBlockXpos), std::end(stoneBlockXpos),
std::begin(stoneBlockYpos),
std::back_inserter(stoneBlocks),
[](int x, int y) { return block(x, y); });
(the same can be done with C++03 but it requires a few auxiliary functions).
(同样可以用C ++ 03完成,但它需要一些辅助功能)。
Assuming you need to do this often, you can create a small wrapper which does the same operations but also makes sure that both arrays have the correct size, e.g.:
假设您需要经常这样做,您可以创建一个小包装器,它执行相同的操作,但也确保两个数组都具有正确的大小,例如:
template <int Size>
std::vector<block>
makeBlocks(int (&xpos)[Size], int (&ypos)[Size])
{
std::vector<block> rc;
std::transform(std::begin(xpos), std::end(xpos),
std::begin(ypos),
std::back_inserter(rc),
[](int x, int y) { return block(x, y); });
return rc;
}
stoneBlocks = makeBlocks(stoneBlockXpos, stoneBlockYpos);
#2
1
If you just want to initialize the vector ONCE (and you have a C++11 compatible compiler), you can use this type of method:
如果您只想初始化向量ONCE(并且您有一个C ++ 11兼容的编译器),则可以使用以下类型的方法:
#include <vector>
#include <initializer_list>
using namespace std;
class block
{
public:
block(int x, int y) : xPos(x), yPos(y) {}
private:
int xPos;
int yPos;
};
int main()
{
static vector<block> stoneBlocks1 { {1, 1}, {2, 2}, {3, 3} };
static vector<block> stoneBlocks2 { {5, 6}, {7, 8}, {9, 10} };
...
}