STL 对比较 - 第一个元素
STL pair comparison - first elements
谁能解释一下这一段的意思
The great advantage of pairs is that they have built-in operations to compare themselves. Pairs are compared first-to-second element. If the first elements are not equal, the result will be based on the comparison of the first elements only; the second elements will be compared only if the first ones are equal. The array (or vector) of pairs can easily be sorted by STL internal functions.
因此这个
For example, if you want to sort the array of integer points so that they form a polygon, it’s a good idea to put them to the vector< pair<double, pair<int,int> >
, where each element of vector is { polar angle, { x, y } }
. One call to the STL sorting function will give you the desired order of points.
为了理解这一点,我苦苦挣扎了一个小时。
Source
考虑为 pair<A,B>
查看 operator<
,这是一个 class,看起来像:
struct pairAB {
A a;
B b;
};
您可以将该段直接翻译成代码:
bool operator<(const pairAB& lhs, const pairAB& rhs) {
if (lhs.a != rhs.a) { // If the first elements are not equal
return lhs.a < rhs.a; // the result will be based on
} // the comparison of the first elements only
return lhs.b < rhs.b; // the second elements will be compared
// only if the first ones are equal.
}
或者,更抽象地思考,这就是字典排序的工作原理。想一想您将如何排列两个词。你会比较他们的第一个字母 - 如果他们不同,你可以停下来看看哪个更少。如果它们相同,那么你进入第二个字母。
第一段说对的顺序如下:如果你有 (x, y) 和 (z, w),然后你比较它们,然后它会首先检查 x 是否小于(或大于)z:如果是,则比第一对小于(或大于)第二个。但是,如果 x = z,那么它将比较 y 和 w。如果成对的第一个元素比第二个元素对顺序更重要,这使得对成对的向量排序之类的事情变得非常方便。
第二段给出了一个有趣的应用。假设你站在平面上的某个点,有一个多边形包围着你。然后每个点都有一个角度和一个距离。但是考虑到这些点,你怎么知道它们应该以什么顺序形成一个多边形(而不是相互交叉)?如果您以这种格式(角度、距离)存储点,那么您将免费获得盘旋方向。这实际上相当整洁。
STL 对是将两个对象放在一起的容器。例如考虑这个,
对a,b;
第一个元素可以通过 a.first 访问,第二个元素可以通过 a.second 访问。
第一段告诉我们,STL 提供了内置的操作来比较两个对。例如,您需要比较'a'和'b',那么首先使用a.first和b.first进行比较。如果两个值相同,则使用 a.second 和 b.second 进行比较。由于这是一个内置功能,您可以轻松地将它与 STL 的内部功能一起使用,例如排序、b_search 等
第二段是如何使用它的示例。考虑一种情况,您希望对多边形中的点进行排序。您首先要根据它们的极角对它们进行排序,然后是 x 坐标,然后是 y 坐标。因此我们使用对 {angle, {x,y}}。所以任何比较都会首先在角度上进行,然后前进到 x 值,然后是 y 值。
实际上是当你有 vector/arrays 对时,当你使用 sort() 函数时你不必关心排序,你只需要使用 sort(v.begin(),v.end())-> 它将根据第一个元素自动排序,当第一个元素相等时,它们将使用第二个元素进行比较。看link里面的代码和输出,就一目了然了。 https://ideone.com/Ad2yVG.see code in link
比较一个姓氏和名字对的简单例子会更容易理解。
例如,如果你有一对
{ Tomson, Ann }
{ Smith, Tony }
{ Smith, John }
并想按升序对它们进行排序,您必须相互比较这些对。
如果比较前两对
{ Tomson, Ann }
{ Smith, Tony }
那么第一对的姓氏大于第二对的姓氏。所以也没有必要比较名字。已经很清楚 pair
{ Smith, Tony }
必须在对
之前
{ Tomson, Ann }
另一方面,如果你比较对
{ Smith, Tony }
{ Smith, John }
那么这对的姓氏是相等的。所以你需要比较成对的名字。由于 John 小于 Tony,因此很明显 pair
{ Smith, John }
将先于一对
{ Smith, Tony }
尽管姓氏(对的第一个元素)相等。
对于这对{ polar angle, { x, y } }
那么如果两个不同对的极角相等那么就会比较{ x, y }
那又是一对。因此,如果第一个元素 ( x ) 等于,则将比较 y(s).
谁能解释一下这一段的意思
The great advantage of pairs is that they have built-in operations to compare themselves. Pairs are compared first-to-second element. If the first elements are not equal, the result will be based on the comparison of the first elements only; the second elements will be compared only if the first ones are equal. The array (or vector) of pairs can easily be sorted by STL internal functions.
因此这个
For example, if you want to sort the array of integer points so that they form a polygon, it’s a good idea to put them to the
vector< pair<double, pair<int,int> >
, where each element of vector is{ polar angle, { x, y } }
. One call to the STL sorting function will give you the desired order of points.
为了理解这一点,我苦苦挣扎了一个小时。
Source
考虑为 pair<A,B>
查看 operator<
,这是一个 class,看起来像:
struct pairAB {
A a;
B b;
};
您可以将该段直接翻译成代码:
bool operator<(const pairAB& lhs, const pairAB& rhs) {
if (lhs.a != rhs.a) { // If the first elements are not equal
return lhs.a < rhs.a; // the result will be based on
} // the comparison of the first elements only
return lhs.b < rhs.b; // the second elements will be compared
// only if the first ones are equal.
}
或者,更抽象地思考,这就是字典排序的工作原理。想一想您将如何排列两个词。你会比较他们的第一个字母 - 如果他们不同,你可以停下来看看哪个更少。如果它们相同,那么你进入第二个字母。
第一段说对的顺序如下:如果你有 (x, y) 和 (z, w),然后你比较它们,然后它会首先检查 x 是否小于(或大于)z:如果是,则比第一对小于(或大于)第二个。但是,如果 x = z,那么它将比较 y 和 w。如果成对的第一个元素比第二个元素对顺序更重要,这使得对成对的向量排序之类的事情变得非常方便。
第二段给出了一个有趣的应用。假设你站在平面上的某个点,有一个多边形包围着你。然后每个点都有一个角度和一个距离。但是考虑到这些点,你怎么知道它们应该以什么顺序形成一个多边形(而不是相互交叉)?如果您以这种格式(角度、距离)存储点,那么您将免费获得盘旋方向。这实际上相当整洁。
STL 对是将两个对象放在一起的容器。例如考虑这个,
对a,b;
第一个元素可以通过 a.first 访问,第二个元素可以通过 a.second 访问。
第一段告诉我们,STL 提供了内置的操作来比较两个对。例如,您需要比较'a'和'b',那么首先使用a.first和b.first进行比较。如果两个值相同,则使用 a.second 和 b.second 进行比较。由于这是一个内置功能,您可以轻松地将它与 STL 的内部功能一起使用,例如排序、b_search 等
第二段是如何使用它的示例。考虑一种情况,您希望对多边形中的点进行排序。您首先要根据它们的极角对它们进行排序,然后是 x 坐标,然后是 y 坐标。因此我们使用对 {angle, {x,y}}。所以任何比较都会首先在角度上进行,然后前进到 x 值,然后是 y 值。
实际上是当你有 vector/arrays 对时,当你使用 sort() 函数时你不必关心排序,你只需要使用 sort(v.begin(),v.end())-> 它将根据第一个元素自动排序,当第一个元素相等时,它们将使用第二个元素进行比较。看link里面的代码和输出,就一目了然了。 https://ideone.com/Ad2yVG.see code in link
比较一个姓氏和名字对的简单例子会更容易理解。
例如,如果你有一对
{ Tomson, Ann }
{ Smith, Tony }
{ Smith, John }
并想按升序对它们进行排序,您必须相互比较这些对。
如果比较前两对
{ Tomson, Ann }
{ Smith, Tony }
那么第一对的姓氏大于第二对的姓氏。所以也没有必要比较名字。已经很清楚 pair
{ Smith, Tony }
必须在对
之前{ Tomson, Ann }
另一方面,如果你比较对
{ Smith, Tony }
{ Smith, John }
那么这对的姓氏是相等的。所以你需要比较成对的名字。由于 John 小于 Tony,因此很明显 pair
{ Smith, John }
将先于一对
{ Smith, Tony }
尽管姓氏(对的第一个元素)相等。
对于这对{ polar angle, { x, y } }
那么如果两个不同对的极角相等那么就会比较{ x, y }
那又是一对。因此,如果第一个元素 ( x ) 等于,则将比较 y(s).