一、C++ vector::data()函数

返回值类型:vector的基类

返回值:Returns a pointer such that [data(), data() + size()] is a valid

range. For a non-empty %vector, data() == &front().

等价于:&vector::front()

例子

//基类型定义
class Token {
private:
int lineshow; //记录该单词在原程序中的行数
string lex;
string sem;
public:
Token(int, string, string);
~Token();
int getLineShow() {
return this->lineshow;
}
string getLex() {
return this->lex;
};
string getSem() {
return this->sem;
}
};
//主函数
int main() {
// initialising vector
vector<Token> vec;
vec.push_back(Token(1, "lex","word"));
vec.push_back(Token(2, "lex","word")); // memory pointer pointing to the
// first element
Token* pos = vec.data();
//Token* pos = &vec.front();
// prints the vector
cout << "The vector elements are: ";
for (int i = 0; i < vec.size(); ++i) {
cout <<pos->getLineShow()<< pos->getLex()<<pos->getSem()<<endl;
pos++;//为什么pos++可以定位到下一个数组?
}
return 0;
} output:The vector elements are:
1lexword
2lexword

在例子中通过data()获得该数组对应类型的指针,指向该数组的首元素

其作用等价于 Token* pos = &vec.front();

并通过该指针对数组进行了遍历

在过程中有一个困惑 为什么pos++ 可以定位到数组中的下一个元素?

于是对代码进行了调试并查看pos++执行前后指针的内容



可见pos++不等价于pos+=1

而等价于 pos+=基类型的大小

由此引出下一个标题

C++对于++运算符的默认重载

直接上源码(节选)

json.h:
Value::ObjectValues::iterator current_; SelfType& operator++() {
increment();
return *this;
} jsoncpp.cpp:
void ValueIteratorBase::increment() {
#ifndef JSON_VALUE_USE_INTERNAL_MAP
++current_;
#else
if (isArray_)
ValueInternalArray::increment(iterator_.array_);
ValueInternalMap::increment(iterator_.map_);
#endif
}

核心函数是incrument()

在其中是对current自增 而在json.h中定义了current是迭代器

在对基类型取值的时候调用的是类型的迭代器

而迭代器的自增则是增加基类的大小

总结:

基类型指针的++运算符的作用

不是使这个指针指向地址自增1

而是使这个指针指向的对象的迭代器自增

C++中vector::data()使用心得和对自定义类型指针运算符的默认重载的更多相关文章

  1. [转]STL中vector转数组(实际是数组的指针)

    感谢:http://topic.csdn.net/t/20050429/20/3976956.html 感谢:http://yzyanchao.blogbus.com/logs/47796444.ht ...

  2. c++中vector容器的功能及应用。

    vector基本操作:  1.头文件 #include<vector>. 注:一定要加上using namespace std;  2.vector对象的创建: vector<int ...

  3. C++的STL中vector内存分配方法的简单探索

    STL中vector什么时候会自动分配内存,又是怎么分配的呢? 环境:Linux  CentOS 5.2 1.代码 #include <vector> #include <stdio ...

  4. vector data() [c++11]

    Example 12345678910111213141516171819202122 // vector::data #include <iostream> #include <v ...

  5. [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)

    首先,vector 在VC 2008 中的实现比较复杂,虽然vector 的声明跟VC6.0 是一致的,如下:  C++ Code  1 2   template < class _Ty, cl ...

  6. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  7. excel 导入数据库 / SSIS 中 excel data source --64位excel 版本不支持-- solution

    当本地安装的excel(2013版) 是64-bit时:出现的以下两种错误 解决: 1. excel 导入数据库 , 如果文件是2007则会出现:“The 'Microsoft.ACE.OLEDB.1 ...

  8. C++ 中vector的基本用法

    //在网上看了好久,自己总结了一下下,第一篇博客,呼呼,学到不少 基本概念 vector容器是一个模板类,可以存放任何类型的对象).vector对象可以在运行时高效地添加元素,并且vector中元素是 ...

  9. 关于真机调试DDMS中的data文件夹打不开的解决方法

    关于真机调试DDMS中的data文件夹打不开的解决方法 今天在开发的时候需要导出程序中的数据库文件查看数据,数据库文件默认就在/data/data/应用包名/databases/数据库名 这个路径下, ...

随机推荐

  1. iOS应用性能调优的建议和技巧--中高级--王朋

    中级(这些是你可能在一些相对复杂情况下可能用到的) 9. 重用和延迟加载Views 10. Cache, Cache, 还是Cache! 11. 权衡渲染方法 12. 处理内存警告 13. 重用大开销 ...

  2. Centos7系统使用yum遇到的问题failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try.

    简单粗暴重新安装yum. 1.查看linux上所有的yum包 # rpm -qa|grep yum 2.逐个卸载,如 # rpm -e yum-plugin-fastestmirror-1.1.31- ...

  3. day2 -- 字符串常用方法、列表、字典

    1.字符串常用方法 name = "Convict abc" count(): print(name.count("c")) # 2 find(): print ...

  4. PyCharm编程软件详细安装教程

    PyCharm编程软件安装教程&破解 一.官网下载软件 1. 网页搜索进入PyCharm官网下载页面(https://www.jetbrains.com/pycharm/download/ ) ...

  5. Solution -「CF 1491H」Yuezheng Ling and Dynamic Tree

    \(\mathcal{Description}\)   Link. 做题原因:题目名.   给定一个长度 \(n-1\) 的序列 \(\{a_2,a_3,\cdots,a_n\}\),其描述了一棵 \ ...

  6. 最新版的Dubbo Admin 3.0 本地启动方式

    项目下载 项目地址:https://github.com/apache/dubbo-admin 如下图,使用git地址直接构建或者下载zip包构建源码都可以,我用的是下载的zip包, 项目架构说明 d ...

  7. shell脚本批量配置多台主机静态ip

    关于脚本 服务器使用之前,都需要先配置静态IP,那就将这种简单重复的工作,交给脚本来处理吧,让我们运维有更多的时间喝茶看报刷微博 脚本使用 sh ssh.sh ip.txt ssh.sh 为脚本的名称 ...

  8. python好用的函数或对象

    1.ljust.rjust "hello".ljust(10,"x") #将字符串hello做对齐,并且用字符'x'补到10个字符 #输出为:helloxxxx ...

  9. Failed to restart ssh.service: Unit not found.

    环境 操作系统:CentOS 7 问题 重启ssh服务,启动报错:Failed to restart ssh.service: Unit not found. 操作步骤 1. 编辑sshd_confi ...

  10. ios开发 将json格式数据上传服务器

    看了一些大小牛的资料其实就3步 1.使用post 请求 ,因为get是不能上传的 2.设置请求类型 , 讲你的json数据上传 3.向服务器发送数据按照下面示例代码,就差不多了 1 // 1.创建请求 ...