简体   繁体   English

指针上的运算符<(小于)是否一致?

[英]Is operator< (less than) on pointers consistent?

Note: This question is not about total order.注意:这个问题与总订单无关。 A total order on pointers of the same type can be obtained using std::less .可以使用std::less获得相同类型指针的总顺序。

According to this , comparing two pointers with operator< isn't allowed if they point for example into different allocations. 据此,如果两个指针指向不同的分配,则不允许用operator<比较两个指针。

In which sense isn't it allowed?在哪个意义上是不允许的? Is it implementation defined, unspecified or undefined behaviour?它是实现定义的、未指定的或未定义的行为吗?

I think I read somewhere that it's unspecified.我想我在某处读到它是未指定的。 An implementation isn't required to document what the behaviour is, but there must be some behaviour.实现不需要记录行为是什么,但必须有一些行为。 So that would mean, comparing any two pointers is still legal, but doesn't neccessarily yield a total order.所以这意味着,比较任何两个指针仍然是合法的,但不一定会产生总顺序。 Does that mean, that we still have to get a consistent result, when comparing the same two pointers twice?这是否意味着,当比较相同的两个指针两次时,我们仍然必须得到一致的结果? The general case would be: Does invoking the same unspecified behaviour twice within an application always yield the same result?一般情况是:在应用程序中两次调用相同的未指定行为是否总是产生相同的结果?

int i1, i2;
int* a = &i1;
int* b = &i2;
bool b1 = a < b; // unspecified, right?
bool b2 = a < b;
assert(b1 == b2); // Is this guaranteed to be true?

Comparing two unrelated pointers (ie pointers not pointing to the same memory, or not pointing to different parts of the same "array") can only be done using equality == and inequality != .比较两个不相关的指针(即指针不指向同一内存,或不指向同一“数组”的不同部分)只能使用等式==和不等式!=来完成。 All other comparison is unspecified .所有其他比较均未指定

If you have two pointers pointing to the same place, or inside the same array, then you can compare them using the relative operators.如果你有两个指针指向同一个地方,或者在同一个数组中,那么你可以使用相对运算符来比较它们。

So if you have eg所以如果你有例如

int* p1 = new int[10];
int* p2 = new int[5];

you can only use == and != to compare the pointers p1 and p2 .只能使用==!=来比较指针p1p2

But if you have但是如果你有

int a = new int[10];
int* p1 = &a[0];
int* p2 = &a[3];

then you can use also < and > (and of course <= and >= ) to compare p1 and p2那么你也可以使用<> (当然还有<=>= )来比较p1p2

  1. It is not possible to compare two unrelated pointers(except using == and !=), that is comparing two unrelated pointer is undefined.无法比较两个不相关的指针(使用 == 和 != 除外),即比较两个不相关的指针是未定义的。
  2. However, it is still possible to create a vector of pointers and then sort that vector in terms of the address of those pointers!但是,仍然可以创建一个指针向量,然后根据这些指针的地址对该向量进行排序! See example 2 below.请参见下面的示例 2。

Example 1:示例 1:

int a = 5,b = 6;
int *a_ptr = &a, *b_ptr = &b;
bool ans = a_ptr < b_ptr;//undefined behavior

Example 2:示例 2:

#include <iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    std::string firstName = "Anoop",middleName = "Singh", lastName = "Rana";
    std::vector<string *> storage_ptr = {&firstName,&middleName,&lastName};
    std::vector<string *>::iterator begin = storage_ptr.begin();
    while(begin!=storage_ptr.end()){
       std::cout<<(**begin)<<std::endl;
       begin++;
    }
    std::sort(storage_ptr.begin(),storage_ptr.end(),greater<string *>());
    std::vector<string *>::iterator begin2 = storage_ptr.begin();
    while(begin2!=storage_ptr.end()){
       std::cout<<(**begin2)<<std::endl;
       begin2++;
    }
    return 0;
}

As you can see that the first while loop prints the vector in descending order while the second while loop prints the vector in ascending order showing that using the new standard we can use the function object greater<string *> to sort the vector of pointers.如您所见,第一个while循环按降序打印向量,而第二个while循环按升序打印向量,这表明使用新标准我们可以使用函数对象greater<string *>对指针向量进行排序。

Comparing pointers as shown in your example makes little sense because "a" and "b" value depends on how is the data stored in memory, not on the content stored in that location.比较示例中显示的指针毫无意义,因为“a”和“b”值取决于数据在内存中的存储方式,而不是存储在该位置的内容。

Pointers are addresses in memory (usually stored as 32 bit integer).指针是内存中的地址(通常存储为 32 位整数)。 Unless you change any of them, their comparison will return the same result.除非您更改它们中的任何一个,否则它们的比较将返回相同的结果。 You might not know what that result is, but you'll know that it'll be the same every time.你可能不知道结果是什么,但你会知道每次都是一样的。

Therefore, in your case, you will get the same result for both b1 and b2, so the assertion will pass.因此,在您的情况下,您将获得 b1 和 b2 相同的结果,因此断言将通过。

Here is an example where comparing pointers does make sense:这是一个比较指针确实有意义的示例:

int data[1000];
int *end = data + 50;
for (int *c = data; c < end; c++)
     ... use *c

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM