假設我有一個頂點列表:std::vector<vec3> vertices
,以及一系列索引:std::vector<unsigned int>
。然後,我想用std::sort
對這些索引進行排序,而不是基於索引的大小,而是基於它們指向的頂點座標。這就是我的意思:基於其他信息的C++排序索引
std::sort(indices.begin(), indices.end(),
[](unsigned int indexA, unsigned int indexB) {
return vertices[indexA].x < vertices[indexB].x;
});
在一個完美的世界中,以下將根據指向頂點的x座標對索引進行排序。但是,這不是lambda函數的工作方式,我無法訪問vertices
信息。
有沒有辦法使用std::sort
以上述方式?或者我最好使用鍵值數據結構/實現我自己的冒泡排序?
你不只是需要捕獲'vertices'? – jrok