C++ Notes 1 - size_type - Accelerated Ch3
1. 为什么用string::size_type而不是int?
--Why use string::size_type ? int is supposed to work! it holds numbers!
--A short holds numbers too. So does a signed char.
But none of those types are guaranteed to be large enough to represent the sizes of any strings.
string::size_type guarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is.
2.size_type
在标准库string类型中,最容易令人产生误解就是size()成员函数的返回值了,如果不深入分析的话,大多人都会认为size()的返回值为int类型,其实不然。事实上,size操作返回的是string::size_type类型的值。
引用《C++ Primer》一段原文简单解释一下:
string类类型和许多其他库类型都定义了一些配套类型(companion type)。通过这些配套类型,库类型的使用就能和机器无关(machine-independent)。size_type就是这些配套类型中的一种。它定义为与unsigned型(unsigned int 或 unsigned long)具有相同的含义,而且可以保证足够大能够存储任意string对象的长度。为了使用由string类型定义的size_type类型,程序员必须加上作用域操作符来说明所使用的size_type类型是由string类定义的。
特别注意的是:任何存储string的size操作结果的变量必须为string::size_type类型,同时,使用size_type类型时,必须指出该类型是在哪里定义的。切记不要吧size的返回值赋给一个int变量。
不仅string类型定义了size_type,其他标准库类型如vector::size_type,list::size_type,deque::size_type,map::size_type,multimap::size_type,basic_string::size_type 等更多请查看MSDN详细介绍。
quote:
1. http://stackoverflow.com/questions/1181079/stringsize-type-instead-of-int
2. http://blog.sina.com.cn/s/blog_8184e03301016bts.html
C++ Notes 1 - size_type - Accelerated Ch3的更多相关文章
- XV6操作系统代码阅读心得(二):进程
1. 进程的基本概念 从抽象的意义来说,进程是指一个正在运行的程序的实例,而线程是一个CPU指令执行流的最小单位.进程是操作系统资源分配的最小单位,线程是操作系统中调度的最小单位.从实现的角度上讲,X ...
- 《数据结构与算法分析:C语言描述_原书第二版》CH3表、栈和队列_reading notes
表.栈和队列是最简单和最基本的三种数据结构.基本上,每一个有意义的程序都将明晰地至少使用一种这样的数据结构,比如栈在程序中总是要间接地用到,不管你在程序中是否做了声明. 本章学习重点: 理解抽象数据类 ...
- ASP.NET Core 1.1.0 Release Notes
ASP.NET Core 1.1.0 Release Notes We are pleased to announce the release of ASP.NET Core 1.1.0! Antif ...
- Android Weekly Notes Issue #237
Android Weekly Issue #237 December 25th, 2016 Android Weekly Issue #237 这是本年的最后一篇issue, 感谢大家. 本期内容包括 ...
- Android Weekly Notes Issue #230
Android Weekly Notes Issue #230 November 6th, 2016 Android Weekly Issue #230. Android Weekly笔记, 本期内容 ...
- Android Weekly Notes Issue #229
Android Weekly Issue #229 October 30th, 2016 Android Weekly Issue #229 Android Weekly笔记, 本期内容包括: 性能库 ...
- Android Weekly Notes Issue #227
Android Weekly Issue #227 October 16th, 2016 Android Weekly Issue #227. 本期内容包括: Google的Mobile Vision ...
- Android Weekly Notes Issue #221
Android Weekly Issue #221 September 4th, 2016 Android Weekly Issue #221 ARTICLES & TUTORIALS And ...
- Android Weekly Notes Issue #219
Android Weekly Issue #219 August 21st, 2016 Android Weekly Issue #219 ARTICLES & TUTORIALS Andro ...
随机推荐
- windows本地提权——2003笔记
windows主机 用户有三种:Users,Administrator,System 本地拥有administrator权限用户提权到system用户 win2003: at命令 系统调度任务的指令, ...
- iview tree 之如何获取已勾选的节点
1.记得添加 ref 2.用 this.$refs.tree.getCheckNodes()
- zabbix4.2 安装
官网https://www.zabbix.com/cn/download a. 安装 数据库rpm -Uvh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86 ...
- vue 打印
vue 方法 第一种方法:通过npm 安装插件 1,安装 npm install vue-print-nb --save 2,引入 安装好以后在main.js文件中引入 import Print ...
- Python练习 | WebServer
#-*- coding:utf-8 -*- import sys, os from http.server import BaseHTTPRequestHandler, HTTPServer #--- ...
- (转)MySQL5.6主从复制技术
原文:http://www.cnblogs.com/ilifeilong/category/1014799.html MySQL5.6复制技术(4)-MySQL主从复制过滤参数 摘要: 复制的过滤主要 ...
- (转)Python random模块
原文:https://my.oschina.net/cuffica/blog/33336 https://www.cnblogs.com/renpingsheng/p/7105296.html ran ...
- java POST 传值 加签 验证
话不多说,代码如下 package com.syl.test_key; import lombok.extern.slf4j.Slf4j; import org.apache.commons.code ...
- 使用 Python 管理 Azure:基础配置
Azure 提供了丰富的 Python SDK 来对 Azure 进行开发管理,包括使用 Azure 的开源框架在 Azure 上创建 web 应用程序,对 Azure 的虚拟机,存储等进行管理,本系 ...
- Angular4 step by step.2
1.使用CLI命令生成的项目,直接把css ,和 template 给独立到文件中了,看着 `` 扎眼 哇咔咔 2.项目效果图: