lower_bound(a+j,a+n,temp)-a;
lower_bound(a+j,a+n,temp)-a中为什么要“-a”?
lower_bound(...)返回一个和a类型一样的指针比如a1
a1-a得到a1在a后面第几个
Pointer Arithmetic
Additive operations involving a pointer and an integer give meaningful results only if the pointer operand addresses an array member and the integer value produces an offset within the bounds of the same array. When the integer value is converted to an address offset, the compiler assumes that only memory positions of the same size lie between the original address and the address plus the offset.
This assumption is valid for array members. By definition, an array is a series of values of the same type; its elements reside in contiguous memory locations. However, storage for any types except array elements is not guaranteed to be filled by the same type of identifiers. That is, blanks can appear between memory positions, even positions of the same type. Therefore, the results of adding to or subtracting from the addresses of any values but array elements are undefined.
Similarly, when two pointer values are subtracted, the conversion assumes that only values of the same type, with no blanks, lie between the addresses given by the operands.
以下是上面文字用百度自动翻译的结果:(^_^)
指针算法
只有当指针操作数寻址数组成员且整数值在同一数组的边界内产生偏移量时,涉及指针和整数的加法运算才会给出有意义的结果。当整数值转换为地址偏移量时,编译器假定只有大小相同的内存位置位于原始地址和地址加上偏移量之间。
此假设对数组成员有效。根据定义,数组是一系列相同类型的值;它的元素位于连续的内存位置。然而,除了数组元素之外的任何类型的存储都不能保证由相同类型的标识符填充。也就是说,空白可以出现在内存位置之间,甚至是相同类型的位置。因此,除了数组元素之外的任何值的地址加或减的结果都是未定义的。
类似地,当两个指针值相减时,转换假定只有相同类型的值(不带空格)位于操作数给定的地址之间。
-a
就是相对于a
的值,没有上下文,不好分析具体含义