我有一个3分量矢量结构,Vector3用3 int表示X,Y和Z.对于每个3D点(我有或多或少200-300个不同的3D点),我有一个string.
我想要做的是建立一个数据结构来检查string该位置是否存在.我想用a std::map而且我做了这个代码而没有很好的结果:
它有的错误是它只运行else一次部分,并一直string反复返回.
我的Vector3课程是Ogre3D中的课程:http://www.ogre3d.org/docs/api/html/classOgre_1_1Vector3.html
String WorldGenerator::createPlatformBorder(Vector3 size)
{
static std::map<Vector3, String> generatedBorders;
if (generatedBorders.find(size) != generatedBorders.end())
{
return generatedBorders[size];
}
else
{
String blockName = requestNewPlatformBorderName();
generatedBorders.insert(std::pair<Vector3, String>(size, blockName));
// some logic
return blockName;
}
}
Run Code Online (Sandbox Code Playgroud)
你能帮帮我吗?
请注意,该功能requestNewPlatformBorderName()完全正常,因此错误不存在.这是它的代码:
String requestNewPlatformBorderName()
{
static int counter = 0;
return StringConverter::toString(++counter) + "-platform-border";
}
Run Code Online (Sandbox Code Playgroud)
你有两个选择:
<类的运算符Vector3,或Vector3秒的函数,并在声明地图时指定它.当没有自然(直观,常见,默认等)排序作为键的类,或者您想要按照不同于它的标准排序/映射时,这个特别有用.恕我直言,第一个就是你的例子,所以我会倾向于它.<运营商bool operator < (const Vector3 &that) const {
if( this.x != that.x )
return this.x < that.x ;
else if( this.y != that.y )
return this.y < that.y ;
else if( this.z != that.z )
return this.z < that.z ;
else
return false ;
}
Run Code Online (Sandbox Code Playgroud)
class Vector3Comparator {
public:
bool operator () (const Vector3 &a,const Vector3 &b) const {
if( a.x != b.x )
return a.x < b.x ;
else if( a.y != b.y )
return a.y < b.y ;
else if( a.z != b.z )
return a.z < b.z ;
else
return false ;
}
}
...
static std::map<Vector3,string,Vector3Comparator> generatedBorders;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1349 次 |
| 最近记录: |