此向量按哪个值排序
By which value is this vector being sorted?
我对C没有太多了解,但我想了解这段代码是如何工作的。我的问题是我真的不明白在这种情况下如何对向量进行排序。它如何知道必须按哪个值排序?
#include "stdafx.h"
#include "LatticeLocation.h"
#include "Body.h"
#include "Region.h"
#include "Particle.h"
#include <queue>
void LatticeLocation::CalculateNeighborhood()
{
sort(neighborhood.begin(), neighborhood.end());
}
这是排序的向量:
std::vector<LatticeLocation*> neighborhood;
这里是LatticeLocation.h文件:
#pragma once
#include "Point3.h"
#include <vector>
class Summation;
class Body;
class Chunk;
class Region;
class Particle;
class Cell;
class LatticeLocation
{
public:
Body *body;
Point3 index;
Cell *cell; // Primarily for rendering... we keep it in the physics engine to make things simple for users
// Properties
bool regionExists; // Set to false if the region is identical to another region or otherwise turned off
bool edge; // Whether this is an edge - i.e., has less than the full number of immediateNeighbors
// The IMMEDIATE immediateNeighbors
std::vector<LatticeLocation*> immediateNeighbors;
LatticeLocation *immediateNeighborsGrid[3][3][3]; // Same as the pointers in the array, just indexed differently
// Generated
std::vector<LatticeLocation*> neighborhood; // All particles up to w links away
// The elements centered/living here
Particle *particle;
Region *region; // MAY BE NULL
std::vector<Summation*> sums[2]; // Sums that live here (not necc. centered). sums[0] = xSums, sums[1] = xySums
// Used in some algorithms
unsigned int touch;
float touchFloat;
void CalculateNeighborhood(); // Will use a BFS to fill in neighborhood. Also sets regionExists
};
stdafx.h:
#pragma once
#include <string>
#include <sstream>
#include <math.h>
#include <fvec.h> // SSE
#include <vector>
#include <set>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
我发现的所有排序示例都使用"std:sort"而不仅仅是"sort",所以也许它不是这里使用的标准排序函数。我搜索了所有源代码文件以查找这样的排序函数,但没有。或者是否有可能有一个标准的排序函数,它会自动发现LatticeLocation包含一个可排序的值(Point3索引,因为Point3重载了<运算符)并按该值对其进行排序?>
编辑:
事实证明,我从上面的剪断中遗漏了一些重要的代码。这应该更有意义
#include "stdafx.h"
#include "LatticeLocation.h"
#include "Body.h"
#include "Region.h"
#include "Particle.h"
#include <queue>
void LatticeLocation::CalculateNeighborhood()
{
neighborhood.clear();
neighborhood = immediateNeighbors;
sort(neighborhood.begin(), neighborhood.end());
}
如果你正在排序的向量是由指针LatticeLocation*
组成的,那么你得到的排序是没有意义的:你只是在对指针进行排序(即你根据它们在内存中占据的特定"随机"位置对对象进行排序),而不考虑LatticeLocation
类的语义。
您可能希望提供一个临时的自定义排序标准,例如使用 lambda 作为要std::sort()
的第三个参数:
sort(neighborhood.begin(), neighborhood.end(), [](const LatticeLocation* a,
const LatticeLocation* b) {
// Implement proper code to specify if '*a' is less than '*b'
....
});
它只是根据运算符<
比较指针LatticeLocation*
。事实上,如果向量中的指针不指向某个数组的元素,则此代码具有未定义的行为,因为您可能无法比较不指向同一数组元素的指针。
例如,在C标准中,写得足够清楚
在所有其他情况下,行为是未定义的
在C++标准中,运算符的定义如下
3 比较指针到对象的定义如下: — 如果两个 指针指向同一数组的不同元素,或指向 其子对象,指向具有较高元素的指针 下标比较更大。— 如果一个指针指向 一个数组,或其子对象,另一个指针指向一个 经过数组的最后一个元素,后一个指针比较 大。— 如果两个指针指向不同的非静态数据成员 对于同一对象,或对于此类成员的子对象,递归地, 指向后面声明的成员的指针比较更大,前提是两个 成员具有相同的访问控制(第 11 条),并提供他们的 阶级不是一个工会。
在使用指针的其他情况下,运算符是未定义的。
- 计算排序向量的向量中唯一值的计数
- 查找两个排序向量中共有的元素
- 对的排序向量 (std::vector<pair<int, int>>) 按对的第一个元素搜索并更新第二个元素值
- 如何从C++的对的排序向量中获取有关给定值的相应对
- 对象接收堆栈溢出异常 c++ 的排序向量
- "x"的所有元素都存在于"y"(排序向量)中吗?
- 指向指针排序向量的指针向量的 C++ 向量
- 未排序向量上的lower_bound/upper_bound
- 智能指针的排序向量:神秘崩溃
- 按类型排序向量并按类型或派生类型搜索
- 如何在不使用标准算法的情况下在排序向量中添加 c 元素?
- C++排序向量<double>与在<Object>双成员变量上键控的向量
- 字符串指针的排序向量
- 具有前导数字的字符串的排序向量
- 保证重新排序向量
- 结构解释的C 排序向量
- 更新排序向量的一个条目
- 通过在排序向量上使用二叉搜索来定位未排序向量中的元素
- 排序向量上 std::find_if 和 std::bind2nd 的替代品
- 使用QuickSort算法以降序排序向量