C++ 删除向量中的重复项?

C++ 删除向量中的重复项?,c++,C++,我有一个定义如下的顶点: struct VERTEX { XMFLOAT3 Pos XMFLOAT2 UV }; 我定义了一个索引,纹理协调索引,UV坐标和顶点 vector <int> indices ; vector <int> Texcoordindex ; vector <XMFLOAT2> UVCoinate ; vector <XMFLOAT3> Verices; 我希望结果是这样的 # This file

我有一个定义如下的顶点:

struct VERTEX
{
  XMFLOAT3 Pos  
  XMFLOAT2 UV    

}; 
我定义了一个索引,纹理协调索引,UV坐标和顶点

vector <int> indices ;
vector <int> Texcoordindex ; 
vector <XMFLOAT2> UVCoinate ;
vector <XMFLOAT3> Verices;
我希望结果是这样的

# This file uses centimeters as units for non-parametric coordinates.

v -12.830015 0.000000 12.061520
v 12.027525 0.000000 12.061520
v -12.830015 15.862570 12.061520
v 12.027525 15.862570 12.061520
v -12.830015 15.862570 -12.622024
v 12.027525 15.862570 -12.622024
v -12.830015 0.000000 -12.622024
v 12.027525 0.000000 -12.622024

vt 0.375000 0.000000
vt 0.625000 0.000000
vt 0.375000 0.250000
vt 0.625000 0.250000
vt 0.375000 0.500000
vt 0.625000 0.500000
vt 0.375000 0.750000
vt 0.625000 0.750000
vt 0.375000 1.000000
vt 0.625000 1.000000
vt 0.875000 0.000000
vt 0.875000 0.250000
vt 0.125000 0.000000
vt 0.125000 0.250000
删除复制后的新顶点 我尝试使用sort和unique,但出现编译错误:

error C2784: 'bool std::operator <(const std::list<_Ty,_Ax> &,const std::list<_Ty,_Ax> &)' : could not deduce template argument for 'const std::list<_Ty,_Ax> &' from 'VERTEX'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'VERTEX' c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'VERTEX'   c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const 
std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2784: 'bool std::operator <(const std::vector<_Ty,_Ax> &,const std::vector<_Ty,_Ax> &)' : could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)' : could not deduce template argument for 'const std::unique_ptr<_Ty,_Dx> &' from 'VERTEX'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'VERTEX'  c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test


error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'VERTEX'    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C2676: binary '<' : 'VERTEX' does not define this operator or a conversion to a type acceptable to the predefined operator    c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

error C1903: unable to recover from previous error(s); stopping compilation c:\program files (x86)\microsoft visual studio 10.0\vc\include\algorithm    3559    1   Mesh_test

如果没有代码和编译器错误,就不可能诊断问题。但使用std::sort和std::unique从索引向量中删除重复项的示例如下:

std::sort(indices.begin(), indices.end());
vector<int>::iterator it = std::unique(indices.begin(), indices.end());
indices.erase(it,indices.end());

希望这有帮助。

如果没有代码和编译器错误,就不可能诊断问题。但使用std::sort和std::unique从索引向量中删除重复项的示例如下:

std::sort(indices.begin(), indices.end());
vector<int>::iterator it = std::unique(indices.begin(), indices.end());
indices.erase(it,indices.end());

希望这有帮助。

如果内存使用没有任何限制,您可以声明一个新的向量保留大小与您的相同,并使用std::find函数仅输入此向量中尚未存在的值,以确定值是否已存在于新向量中

std::vector newVector;
newVector.reserve( oldVector.size() );
对于旧向量的所有元素:

if( newVector.find(element))
{
  if( find( newVector.begin(), newVector.end(), newVector.size() != newVector.end() )
  {
    newVector.push_back(element);
   }
}

另一种算法是将向量中的所有元素移回预览位置,以防重复。此方法仅适用于排序数组-列表

如果在内存使用方面没有任何限制,则可以声明一个新的向量保留大小与您的向量保留大小相同,并仅输入尚未存在的值此向量使用std::find函数确定值是否已在新向量中

std::vector newVector;
newVector.reserve( oldVector.size() );
对于旧向量的所有元素:

if( newVector.find(element))
{
  if( find( newVector.begin(), newVector.end(), newVector.size() != newVector.end() )
  {
    newVector.push_back(element);
   }
}

另一种算法是将向量中的所有元素移回预览位置,以防重复。这种方法仅适用于排序数组-列表。我知道编译器的输出令人困惑,但它试图告诉您的是,它不知道如何比较两个顶点对象。您可以在类中定义运算符<:

struct VERTEX
{
  XMFLOAT3 Pos;  
  XMFLOAT2 UV;

  bool operator<(const VERTEX &that) const
  {
    // return true if you consider this vertex to be less than that vertex.
  }
};
或者作为一个全局函数

bool operator<(const VERTEX &v1,const VERTEX &v2)
{
  // return true if you consider v1 to be less than v2
}

我知道编译器的输出令人困惑,但它试图告诉您的是,它不知道如何比较两个顶点对象。您可以在类中定义运算符<:

struct VERTEX
{
  XMFLOAT3 Pos;  
  XMFLOAT2 UV;

  bool operator<(const VERTEX &that) const
  {
    // return true if you consider this vertex to be less than that vertex.
  }
};
或者作为一个全局函数

bool operator<(const VERTEX &v1,const VERTEX &v2)
{
  // return true if you consider v1 to be less than v2
}


也许向我们展示代码和编译器错误是个好主意。你有重载相等运算符吗?不,我没有重载相等运算符如果我使用vector,我可以删除重复,我认为XMFLOAT3中的问题!!或者可能在我的结构中,也许向我们展示代码和编译器错误是个好主意。你有重载的相等运算符吗?不,我没有重载相等运算符如果我使用vector,我可以删除重复项,我认为XMFLOAT3中的问题!!或者可能在我的structWhy索引中。调整大小。。而不是std::remove..?对于XMFLOAT3,请检查谢谢我尝试使用您的想法,但我有问题,但我使用向量我可以删除重复项,我认为XMFLOAT3中的问题!!或者可能在我的结构中@西蒙,没有强烈的偏好。我想std::remove是更清晰的意图。@hmjd:谢谢你的回答。当我再次阅读我的抱怨时,我注意到它是错误的。我的意思是:std::vector::resize。。vs.标准::向量::擦除。。正如您现在在代码中所写的:-为什么是索引。调整大小。。而不是std::remove..?对于XMFLOAT3,请检查谢谢我尝试使用您的想法,但我有问题,但我使用向量我可以删除重复项,我认为XMFLOAT3中的问题!!或者可能在我的结构中@西蒙,没有强烈的偏好。我想std::remove是更清晰的意图。@hmjd:谢谢你的回答。当我再次阅读我的抱怨时,我注意到它是错误的。我的意思是:std::vector::resize。。vs.标准::向量::擦除。。正如您现在在代码中所写的:-你是希腊人吗很好,我想我是唯一一个:谢谢你的回复,我会记住你的想法。我想我会在代码的另一部分需要它,再次感谢!!你是希腊人吗很好,我想我是唯一一个:谢谢你的回复,我会记住你的想法。我想我会在代码的另一部分需要它,再次感谢!!