我有一个结构列表,我正在由其中一个成员排序.我正在使用std :: sort和我自己的比较函数,那部分很好.但是,当我从以下位置更改结构时,我注意到(非常)大的性能差距:
struct square
{
float x;
float y;
float z;
float scale;
float angle;
GLuint texture;
};
Run Code Online (Sandbox Code Playgroud)
至
struct square
{
float x;
float y;
float z;
float scale;
float angle;
GLuint texture;
std::vector <float> color;
};
Run Code Online (Sandbox Code Playgroud)
我已经使用了一种完全不同的方法,我意识到使用这样的矢量是一个坏主意(我知道数组的大小 - rgb),但我想知道为什么我的性能受到了影响.我正在比较z值以进行排序.
这是我的排序函数和结构列表:
std::vector <square> square_list;
//Then add a bunch of squares
bool sort (square a,square b)
{
return a.z < b.z;
}
//Here is the sort that is slow
std::sort (square_list.begin(),square_list.end(),sort);
Run Code Online (Sandbox Code Playgroud)
我想知道它是否与重新排序结构列表有关,因为它们的大小在第二种情况下要大得多?
感谢您的回复.
我正在尝试在以下代码中为类gridCoord和genDir重载运算符+ =:
struct gridCoord{
unsigned x;
unsigned y;
}
inline gridCoord operator +(const gridCoord l,const genDir& r){
// This operator is tested and works
}
inline void operator += (gridCoord l,const genDir& r){
l = l+r;
std::cout<<l;
}
Run Code Online (Sandbox Code Playgroud)
+运算符可以工作,但是当我运行以下代码时:
int main(){
gridCoord coord(1,0);
genDir gd;
coord += gd;
std::cout<< coord;
std::cin.get();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到了输出
x: 2 y: 0
x: 1 y: 0
Run Code Online (Sandbox Code Playgroud)
我期待
x: 2 y: 0
x: 2 y: 0
Run Code Online (Sandbox Code Playgroud)
如果它工作.似乎+ =运算符定义的实际上并没有按照预期修改左参数.任何想法为什么会这样?谢谢!