是否可以获得指针std :: vector< char>中的连续内存片段。在C ++? [英] Is it possible to get the pointer the continous memory fragment in a std::vector<char> in C++?

查看:134
本文介绍了是否可以获得指针std :: vector< char>中的连续内存片段。在C ++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移动我的代码使用 std :: vector< char> 而不是 char * mem = malloc c $ c>但现在我面临一个问题,我只能通过 operator [] 而不是通过指针访问向量数据。



我不能写如下内容:

  std :: vector< char>数据; 
fill_data(data);
char * ptr = data;

之前我可以这样做:

  char * data = malloc(100); 
fill_data2(data);
char * ptr = data;

任何想法如果仍然可以访问向量中的数据

解决方案

p>访问向量数据的标准方法是使用

 & data [0] 


I moved my code to use std::vector<char> instead of char *mem = malloc(...) but now I am facing a problem that I can only access the vector data through operator [] but not via a pointer.

I can't write stuff like:

std::vector<char> data;
fill_data(data);
char *ptr = data;

Before I could do this:

char *data = malloc(100);
fill_data2(data);
char *ptr = data;

Any ideas if it's still possible to access data in a vector via pointer?

Thanks, Boda Cydo.

解决方案

The standard way to access the vector data is to use

&data[0]

这篇关于是否可以获得指针std :: vector&lt; char&gt;中的连续内存片段。在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆