operator c+=_array :: operator []与C ++ STL中的示例

operator c+=

C ++ STL array :: operator [] (C++ STL array::operator[])

Operator [] is used to get/set the element of an array in C++ STL, it returns a reference to an element at given index.

运算符[]用于获取/设置C ++ STL中数组的元素,它返回给定索引处元素的引用。

Syntax:

句法:

    array_name[index];

Parameters: index - position of an element.

参数: index-元素的位置。

Return value: It returns a reference to the element at given index.

返回值:返回给定索引处元素的引用。

Example:

例:

    Input or array declaration:
    array<int,5> values {10, 20, 30, 40, 50};

    Output:
    values[0]	:	10
    values[1]	:	20

C ++ STL程序演示array:operator []的示例 (C++ STL program to demonstrate example of array:operator[])

#include <array>
#include <iostream>
using namespace std;

int main()
{
	array<int,5> values {10, 20, 30, 40, 50};

	//printing elements
	cout<<"element at index 0: "<<values[0]<<endl;
	cout<<"element at index 1: "<<values[1]<<endl;
	cout<<"element at index 2: "<<values[2]<<endl;
	cout<<"element at index 3: "<<values[3]<<endl;
	cout<<"element at index 4: "<<values[4]<<endl;

	//changing some of the values
	values[0] = 100;
	values[4] = 500;

	//printing all elements
	cout << "All elements:"<<endl;
	for (int i : values) {
		cout<<i<<" ";
	}
	cout<<endl;

	return 0;
}

Output

输出量

element at index 0: 10
element at index 1: 20
element at index 2: 30
element at index 3: 40
element at index 4: 50
All elements:
100 20 30 40 500


翻译自: https://www.includehelp.com/stl/array-operator-with-example-in-cpp-stl.aspx

operator c+=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值