STL array的data方法(9)

原文地址:http://www.cplusplus.com/reference/array/array/data/
public member function
<array>

std::array::data

      value_type* data() noexcept;
const value_type* data() const noexcept;
Get pointer to data
Returns a pointer to the first element in the array object.
返回一个指向array第一个元素位置的指针。
Because elements in the array are stored in contiguous storage locations, the pointer retrieved can be offset to access any element in the array.

因为array厘米的元素是连续存放在内存单元中,因此该指针可以通过偏移来访问array中其他任何元素。

例子:

#include <iostream>
#include <array>
using namespace std;
int main()
{
	array<double,5> ad={0.1,0.3,0.5,0.7,0.9};
	double *p=ad.data();
	cout<<"ad.data()="<<*p<<endl;
	cout<<"*(p+1)"<<*(p+1)<<endl;
	cout<<"*(p-1)"<<*(p-1)<<endl;
	cout<<"*(p+10)"<<*(p+10)<<endl;
	cout<<"*(p-10)"<<*(p-10)<<endl;
	


}
运行截图:


注意偏移的范围不要超过了array的范围

Parameters

none

Return Value

Pointer to the data contained by the array object.

返回指向array对象数据位置的指针。


If the array object is const-qualified, the function returns a pointer to const value_type. Otherwise, it returns a pointer to value_type.

如果array对象是const属性的,那么该方法返回一个const的值指针,否则,返回一个普通的指针。


Member type value_type is the type of the elements in the container, defined in array as an alias of its first template parameter (T).

指针的类型和元素的类型指针一致,由array的第一个模版参数决定。


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// array::data
#include <iostream>
#include <cstring>
#include <array>

int main ()
{
  const char* cstr = "Test string";
  std::array<char,12> charray;

  std::memcpy (charray.data(),cstr,12);

  std::cout << charray.data() << '\n';

  return 0;
}


Output:
Test string

Complexity

Constant.

Iterator validity

No changes.

Data races

No contained elements are directly accessed by the call, but the pointer returned can be used to access or modify elements. Concurrently accessing or modifying different elements is safe.

容器的元素不会被访问,但是返回的指针可以被用来访问或者修改元素,同时访问以及修改其他元素是安全的。


Exception safety

No-throw guarantee: this member function never throws exceptions.

该成员方法不会抛出异常。


——————————————————————————————————————————————————————————————————

//翻译的不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。

转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双

Email:coderguang@gmail.com

2014-8-27

于GDUT

——————————————————————————————————————————————————————————————————





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值