C++ C++;:检查向量中所有对象特性的值
我认为最好用一些伪代码来解释这一点:C++ C++;:检查向量中所有对象特性的值,c++,vector,C++,Vector,我认为最好用一些伪代码来解释这一点: std::vector<Yes> objs; //The Yes Class constructor: Yes(x,y) //Let's imagine I instantiated this vector with one object somewhere appropriately void inSomeFunction() { for(int i = 0; i < 20; ++i) { int randX =
std::vector<Yes> objs;
//The Yes Class constructor: Yes(x,y)
//Let's imagine I instantiated this vector with one object somewhere appropriately
void inSomeFunction()
{
for(int i = 0; i < 20; ++i)
{
int randX = rand() % mapWidth;
int randY = rand() % mapHeight;
for(int j = 0; j < objs.size(); ++j)
{
if(randX > x + threshold, for all objects in my vector && randY > y + threshold, for all objects in my vector)
{
objs.push_back(Yes(randX,randY));
}
}
}
}
std::vector objs;
//Yes类构造函数:Yes(x,y)
//让我们想象一下,我在某个地方用一个对象实例化了这个向量
void inSomeFunction()
{
对于(int i=0;i<20;++i)
{
int randX=rand()%mapWidth;
int randY=rand()%mapHeight;
对于(int j=0;j我想知道如何做到这一点?为了更清楚,这是一个游戏。我试图在2D地图中生成多个建筑,但我显然不希望它们重叠或彼此靠近。我该怎么做呢?这是如何迭代向量。您可以自己将元素与希望的值进行比较
std::vector<Object> objs;
void inSomeFunction()
{
for (int i = 0; i < 20; ++i)
{
int randX = rand() % mapWidth;
int randY = rand() % mapHeight;
for (std::vector<Object>::iterator itrObj = objs.begin(); itrObj != objs.end(); ++itrObj)
{
//Here you can access with operator -> the members of the class
//itrObj->x
//Or
//(*itrObj).x
//This is how you iterate through a std::vector<T>
//
//Do stuff with the values here
//
//If you do not wish to change the members
//of the class Object stored in this vector and only compare them
//use a const_iterator instead of iterator
}
}
std::vector objs;
void inSomeFunction()
{
对于(int i=0;i<20;++i)
{
int randX=rand()%mapWidth;
int randY=rand()%mapHeight;
对于(std::vector::迭代器itrObj=objs.begin();itrObj!=objs.end();++itrObj)
{
//在这里,您可以使用operator->类的成员进行访问
//itrObj->x
//或
//(*itrObj).x
//这就是迭代std::vector的方式
//
//用这里的值做一些事情
//
//如果您不想更改成员
//存储在此向量中的类对象的
//使用常量迭代器而不是迭代器
}
}
这是一种检查其是否在范围内的方法:
struct Window
{
Window (int x, int y, int width, int height)
: x(x)
, y(y)
, width(width)
, height(height)
{}
int x;
int y;
int width;
int height;
};
struct Position
{
Position (int x, int y)
: x(x)
, y(y)
{}
int x;
int y;
};
bool IsWithinRange (const Window &Range, const Position &Check)
{
int r_x = Range.x,
r_y = Range.y,
r_width = Range.width,
r_height = Range.height,
r_max_x = r_width + r_x,
r_max_y = r_height + r_y;
if (Check.x >= r_x && Check.x <= r_max_x)
if (Check.y >= r_y && Check.y <= r_max_y)
return true;
return false;
}
struct窗口
{
窗口(整数x,整数y,整数宽度,整数高度)
:x(x)
,y(y)
,宽度(宽度)
,高度
{}
int x;
int-y;
整数宽度;
内部高度;
};
结构位置
{
位置(整数x,整数y)
:x(x)
,y(y)
{}
int x;
int-y;
};
布尔值在范围内(常数窗口和范围、常数位置和检查)
{
int r_x=范围.x,
r_y=范围.y,
r_宽度=范围宽度,
r_高度=范围高度,
r_max_x=r_宽度+r_x,
r_max_y=r_高度+r_y;
如果(Check.x>=r_x&&Check.x=r_y&&Check.y根据我所读的内容,我将在这里冒险。这可能不是最好的方法,但很简单。如果不完全了解你的项目,很难说。但是如果你给我们完整的项目,我们会告诉你像你已经做的那样缩短。因此,我假设你会得到specula这个问题的答案充其量是否定的
尽管如此,一个想法:
创建一个包含max-x和max-y的二维布尔数组。对于每个对象,通过将值设置为true,它将占用该数组中的x,y空间。然后,您可以通过扫描远离给定随机值的距离来检查某个对象是否太近,或者是否已经在使用该对象。我会将其分解为更小的函数。
大概是这样的:
bool overlaps(const Yes& thing, int x, int y)
{
// See if (x, y) overlaps 'thing' in whichever way is appropriate.
}
bool overlaps_any(const std::vector<Yes>& things, int x, int y)
{
for (const Yes& thing : things)
{
if (overlaps(thing, x, y))
{
return true;
}
}
return false;
}
void inSomeFunction(std::vector<Yes>& objs)
{
for(int i = 0; i < 20; ++i)
{
int x = 0;
int y = 0;
do {
x = rand() % mapWidth;
y = rand() % mapHeight;
} while (overlaps_any(objs, x, y));
objs.push_back(Yes(x,y));
}
}
bool重叠(const Yes&thing,int x,int y)
{
//看(x,y)是否以适当的方式重叠“事物”。
}
布尔重叠任意(常量std::vector&things,int x,int y)
{
for(const Yes&thing:things)
{
if(重叠(事物,x,y))
{
返回true;
}
}
返回false;
}
void inSomeFunction(std::vector和objs)
{
对于(int i=0;i<20;++i)
{
int x=0;
int y=0;
做{
x=rand()%mapWidth;
y=rand()%mapHeight;
}while(重叠任意(objs,x,y));
对象推回(是(x,y));
}
}
可能有更有效的方法,但由于您只生成一次地图,我现在不担心效率。您的问题是什么?我看不到任何问号。抱歉,最后两段是我的问题,因为我想知道如何完成这些段落中描述的内容。这甚至不接近于OPHmm要求的答案,那么什么可能是更有效的方法呢?因为我不打算只创建20个对象,它很可能会出现在千千万万个对象中。那么您介意再解释一下上面的语法吗?为什么重叠会包含“Yes&thing”作为一个参数?我也完全不理解任何函数的重叠。为什么还要将所有这些输入参数设置为常量?