STL vector中的data方法(21)

public member function
<vector>

std::vector::data

      value_type* data() noexcept;
const value_type* data() const noexcept;
Access data

Returns a direct pointer to the memory array used internally by the vector to store its owned elements.

返回一个直接指向内存中存储vector元素位置的指针。


Because elements in the vector are guaranteed to be stored in contiguous storage locations in the same order as represented by the vector, the pointer retrieved can be offset to access any element in the array.

因为vector里面的元素都是顺序连续存放的,该指针可以通过偏移量来访问数组内的所有元素。

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int> vi={1,20,30};
	cout<<"vi.capacity="<<vi.capacity()<<endl;
	cout<<"vi.data()="<<vi.data()<<endl;
	auto *p=vi.data();
	cout<<"p="<<p<<endl;
	cout<<"*p="<<*p<<endl;
	cout<<"*(p+2)="<<*(p+2)<<endl;
	cout<<"*(p+10)="<<*(p+10)<<endl;	
	cout<<"vi.capacity="<<vi.capacity()<<endl;


}
结果截图:


要注意的是,即便是超出了范围好像也不会报错!使用gdb调试才发现会直接访问相应的内存,但不会出现错误。


Parameters

none

Return value

A pointer to the first element in the array used internally by the vector.

返回一个指针指向数组第一个元素所在的内存。


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

如果vector是const的,那么返回的是一个const指针,否则,返回普通的指针。


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

value_type是容器内的元素类型,由vector模版参数决定。


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// vector::data
#include <iostream>
#include <vector>

int main ()
{
  std::vector<int> myvector (5);

  int* p = myvector.data();

  *p = 10;
  ++p;
  *p = 20;
  p[2] = 100;

  std::cout << "myvector contains:";
  for (unsigned i=0; i<myvector.size(); ++i)
    std::cout << ' ' << myvector[i];
  std::cout << '\n';

  return 0;
}


Output:
myvector contains: 10 20 0 100 0

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed (neither the const nor the non-const versions modify the container).

容器将被访问。

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.

该方法不会抛出异常。

要注意的是不要对一个空的数组返回的地址进行偏移操作,因为这可能导致错误。

#include <iostream>
#include <vector>
using namespace std;
int main()
{
	vector<int> vi;
	cout<<"vi.capacity="<<vi.capacity()<<endl;
	cout<<"vi.data()="<<vi.data()<<endl;
	auto *p=vi.data();
	cout<<"*(p+10)="<<*(p+10)<<endl;


}
结果截图:


使用gdb调试发现的情况似乎是,并没有给vector分配实际的内存空间,因为data返回的是0,而不是正常的内存空间!


可以看到,p所指针的内存0x0无法访问!


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

转载请注明出处:http://blog.csdn.net/qq844352155

2014-8-16

于GDUT






  • 8
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
STL vector是C++标准模板库的一个容器,它可以存储任意类型的元素,并提供了一系列的函数来操作这些元素。下面是几个常用的STL vector函数: 1. `size()`:返回vector元素的个数。 2. `capacity()`:返回vector能够容纳的元素的个数。 3. `push_back()`:将元素添加到vector的尾部。 4. `pop_back()`:删除vector的尾部元素。 5. `at()`:返回指定位置的元素。 6. `begin()`:返回指向第一个元素的迭代器。 7. `end()`:返回指向最后一个元素后面的位置的迭代器。 8. `clear()`:清空vector的所有元素。 9. `empty()`:检查vector是否为空。 10. `erase()`:删除指定位置或指定范围内的元素。 11. `insert()`:在指定位置插入元素。 12. `resize()`:改变vector的大小。 这些函数可以用于对vector进行增删改查操作,并且可以根据需要调整vector的大小。以上是一些常用的STL vector函数,你可以根据具体的需求选择适合的函数来操作vector。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [STL函数库的讲解(2)---vector](https://blog.csdn.net/lzx_xzl_______/article/details/128691773)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++STL---vector详解](https://blog.csdn.net/qq_44423388/article/details/125948896)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值