C++中的下标或数组索引运算符

下标或数组索引运算符由“[]”表示。此运算符通常与数组一起使用,以检索和操作数组元素。 这是一个二进制或n元运算符,由两部分组成:

null
  1. 后缀/主表达式
  2. 表示

后缀表达式(也称为主表达式)是指针值,如数组或标识符,第二个表达式是整数值。在第二个表达式中,我们还可以包含枚举值。

语法:

postfix-expression[expression];

Example: Ramswarup[10];
Here the Ramswarup is an array and the above
statement print the value which is held by
Ramswarup at index position 10.

下标运算符后面的主表达式是指针,它可以是整数值,但必须记住,两个表达式中的一个表达式必须是指针值,第二个表达式是否为整数阶无关紧要。 例子:

// CPP program to demonstrate []
// operator
#include <iostream>
using namespace std;
int main()
{
char name[] = "Ramswarup Tushar Nilesh Subhash" ;
// Both of the statement prints same thing
cout << name[5] << endl;
cout << 5 [name] << endl;
return 0;
}


输出:

a
a

OUTPUT
a
a

说明: 在上面的示例中,由于下标运算符的独占属性,两个“cout”语句都提供了类似的输出。编译器以类似的方式读取这两条语句,因此 *(姓名+5) 还有 *(5+姓名) .

正负下标 数组的第一个元素存储在索引0处。C++数组的损坏范围是从数组[0 ]到数组[St- 1 ]。然而,C++支持正负下标。负下标必须位于数组边界内;如果他们不这样做,结果是不可预测的。以下代码显示了正数组下标和负数组下标:

// CPP program illustrating the
// positive and negative subscripts
#include <iostream>
using namespace std;
// Driver Method
int main()
{
int intArray[1024];
for ( int i = 0, j = 0; i < 1024; i++) {
intArray[i] = j++;
}
// 512
cout << intArray[512] << endl;
// 257
cout << 257 [intArray] << endl;
// pointer to the middle of the array
int * midArray = &intArray[512];
// 256
cout << midArray[-256] << endl;
// unpredictable, may crash
cout << intArray[-256] << endl;
}


输出:

512
257
256
0

最后一行中的负下标可能会产生运行时错误,因为它指向的地址是256个位置,内存中的位置可能低于数组的原点。指针midArray被初始化到intArray的中间;因此,可以(但不建议)同时使用正数组索引和负数组索引。数组下标错误不会生成编译时错误,但它们可能会产生不可预测的结果。

我们已经介绍了 运算符重载 .本文讨论了索引运算符[]的重载。 以下是一些关于[]重载的有用事实。 1) 当我们想要检查索引是否超出范围时,[]的重载可能很有用。 2) 我们必须在函数中通过引用返回,因为像“arr[i]”这样的表达式可以用作左值。

下面是C++程序演示数组索引操作符的重载[]。

// Overloading operators for Array class
#include <cstdlib>
#include <iostream>
using namespace std;
// A class to represent an integer array
class Array {
private :
int * ptr;
int size;
public :
Array( int *, int );
// Overloading [] operator to access elements in array style
int & operator[]( int );
// Utility function to print contents
void print() const ;
};
// Implementation of [] operator.  This function must return a
// reference as array element can be put on left side
int & Array::operator[]( int index)
{
if (index >= size) {
cout << "Array index out of bound, exiting" ;
exit (0);
}
return ptr[index];
}
// constructor for array class
Array::Array( int * p = NULL, int s = 0)
{
size = s;
ptr = NULL;
if (s != 0) {
ptr = new int [s];
for ( int i = 0; i < s; i++)
ptr[i] = p[i];
}
}
void Array::print() const
{
for ( int i = 0; i < size; i++)
cout << ptr[i] << " " ;
cout << endl;
}
// Driver program to test above methods
int main()
{
int a[] = { 1, 2, 4, 5 };
Array arr1(a, 4);
arr1[2] = 6;
arr1.print();
arr1[8] = 6;
return 0;
}


输出:

1 2 6 5 
Array index out of bound, exiting

输出:

1 2 6 5
Array index out of bound, exiting

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享