size_type、size_t、differentce_type以及ptrdiff_t
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t. size_type是unsigned类型,表示容器中元素长度或者下标,vector<int>::size_type i = ; difference_type是signed类型,表示迭代器差距,vector<int>:: difference_type = iter1-iter2. 前二者位于标准类库std内,后二者专为STL对象所拥有。
size_type
在标准库string类型中,最容易令人产生误解就是size()成员函数的返回值了,如果不深入分析的话,大多人都会认为size()的返回值为int类型,其实不然。事实上,size操作返回的是string::size_type类型的值。 那怎样理解size_type这一类型呢,我引用《C++ Primer》一段原文简单解释一下:
string类类型和许多其他库类型都定义了一些配套类型(companion type)。通过这些配套类型,库类型的使用就能和机器无关(machine-independent)。size_type就是这些配套类型中的一种。它定义为与unsigned型(unsigned int 或 unsigned long)具有相同的含义,而且可以保证足够大能够存储任意string对象的长度。为了使用由string类型定义的size_type类型,程序员必须加上作用域操作符来说明所使用的size_type类型是由string类定义的。
- /*******************************************
- * this is a simple demo to test size_type
- *
- * Auther : Jerry.Jiang
- * Date : 2011/08/20
- * http://blog.csdn.net/jerryjbiao
- *
- *********************************************/
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- string str("This is a simple demo !");
- for (string::size_type index = 0; index != str.size(); ++index)
- {
- cout << str[index];
- }
- cout << endl;
- return 0;
- }
这里特别注意的是:任何存储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详细介绍。下面是几个常用的Demo:
- /*******************************************
- * this is a simple demo to test vector::size_type
- *
- * Auther : Jerry.Jiang
- * Date : 2011/08/20
- * http://blog.csdn.net/jerryjbiao
- *
- *********************************************/
- #include <iostream>
- #include <vector>
- using namespace std;
- int main()
- {
- vector<int> ivec;
- //vector::size_type
- for (vector<int>::size_type ix = 0 ; ix != 10; ++ix)
- {
- ivec.push_back(ix+1);
- }
- //vector::iterator
- for (vector<int>::iterator iter = ivec.begin();
- iter != ivec.end(); ++iter)
- {
- cout << *iter << " ";
- }
- cout << endl;
- return 0;
- }
- /*******************************************
- * this is a simple demo to test list::size_type
- *
- * Auther : Jerry.Jiang
- * Date : 2011/08/20
- * http://blog.csdn.net/jerryjbiao
- *
- *********************************************/
- #include <list>
- #include <iostream>
- using namespace std;
- int main( )
- {
- list <int> c1;
- list <int>::size_type i;
- c1.push_back( 1 );
- i = c1.size( );
- cout << "List length is " << i << "." << endl;
- c1.push_back( 2 );
- i = c1.size( );
- cout << "List length is now " << i << "." << endl;
- return 0;
- }
- /*******************************************
- * this is a simple demo to test map::size_type
- *
- * Auther : Jerry.Jiang
- * Date : 2011/08/20
- * http://blog.csdn.net/jerryjbiao
- *
- *********************************************/
- #include <map>
- #include <iostream>
- int main()
- {
- using namespace std;
- map<int, int> m1, m2;
- map<int, int>::size_type i;
- typedef pair<int, int> Int_Pair;
- m1.insert(Int_Pair(1, 1));
- i = m1.size();
- cout << "The map length is " << i << "." << endl;
- m1.insert(Int_Pair(2, 4));
- i = m1.size();
- cout << "The map length is now " << i << "." << endl;
- return 0;
- }
size_t
size_t类型定义在cstddef头文件中,该文件是C标准库中的头文件 stddef.h 的C++版本。它是一个与机器相关的unsigned类型,其大小足以存储内存中对象的大小。
与前面Demo中vector和string中的size操作类似,在标准库类型bitset中的size操作和count操作的返回值类型为size_t 。
- /***********************************************
- * this is a simple demo to test bitset::size_t
- *
- * Auther : Jerry.Jiang
- * Date : 2011/08/20
- * http://blog.csdn.net/jerryjbiao
- *
- *********************************************/
- #include <iostream>
- #include <bitset>
- using namespace std;
- int main()
- {
- //bitvec有32位,每位都是0
- bitset<32> bitvec;
- cout << " bitvec : " << bitvec << endl;
- //count()统计bitvec中置1的个数
- size_t bitcount = bitvec.count();
- cout << "bitvec.count() :" << bitcount << endl;
- //size()统计bitvec二进制位的个数
- size_t bitsize = bitvec.size();
- cout << "bitvec.size() :" << bitsize << endl;
- return 0;
- }
differentce_type
一种由vector类型定义的signed整型,用于存储任意两个迭代器间的距离。
ptrdiff_t
与size_t一样,定义在cstddef头文件中定义的与机器相关的有符号整型,该类型具有足够的大小存储两个指针的差值,这两个指针指向同一个可能的最大数组。
size_type、size_t、differentce_type以及ptrdiff_t的更多相关文章
- size_t 、wchar_t和 ptrdiff_t
size_t在C语言中就有了. 它是一种"整型"类型,里面保存的是一个整数,就像int, long那样.这种整数用来记录一个大小(size).size_t的全称应该是size ty ...
- c++ ptrdiff_t 类型
ptrdiff_t是C/C++标准库中定义的一个与机器相关的数据类型.ptrdiff_t类型变量通常用来保存两个指针减法操作的结果.ptrdiff_t定义在stddef.h(cstddef)这个文件内 ...
- size_t类型
size_t在C语言中就有了.它是一种“整型”类型,里面保存的是一个整数,就像int, long那样.这种整数用来记录一个大小(size).size_t的全称应该是size type,就是说“一种用来 ...
- ptrdiff_t类型
一.特性 1. 这是一种标准库类型 2. 是两个指针相减的结果的类型(因为差值可能为负值,所以是一种带符号类型) 3. 和size_t一样,ptrdiff_t也是一种定义在<cstddef> ...
- C++ std::array
std::array template < class T, size_t N > class array; Code Example #include <iostream> ...
- 源码阅读笔记 - 2 std::vector (1)
vector的源码真是太长了,今天用了一个下午和一个晚上看和注释了前面的一千行左右 p.s.博客园的代码高亮真是太垃圾, 如果想要阅读带注释的源码,推荐粘贴到VS2015里,然后按ctrl+z取消自动 ...
- C++基础内容复习
下列语句定义了5个变量: int count; double sales_price,sum; std::string title; Sales_item bookItem; 每个定义都是以类型说明符 ...
- BitSet
前几天干了一件比较无聊的事儿——抄了一遍C++ STL bitset的源代码,把不懂的宏定义去掉了,发现(暂时)还能用,嘿嘿. #ifndef BITSET_H #define BITSET_H #i ...
- [翻译] C++ STL容器参考手册(第一章 <array>)
返回总册 本章节原文:http://www.cplusplus.com/reference/array/array/ 1. std::array (C++11支持) template < cla ...
随机推荐
- Centos环境下部署游戏服务器-权限
部署Web服务器的时候,在"DocumentRoot"指向的根目录新建一个文件夹,然后将网页和资源放在这个文件夹里,通过地址http://192.168.0.100/Res/ind ...
- 8天学通MongoDB
随笔分类 - MongoDB 双十一来了,别让你的mongodb宕机了 摘要: 好久没过来吹牛了,前段时间一直赶项目,没有时间来更新博客,项目也终于赶完了,接下来就要面临双十一这场惊心动魄的处女秀考验 ...
- AdventureWorksDW2008R2 attach: Unable to open the physical file. Operating system error 5: "5(Access is denied.)
http://stackoverflow.com/questions/26014133/adventureworksdw2008r2-attach-unable-to-open-the-physica ...
- springmvc在web.xml中的配置
<!-- SpringMVC核心分发器 --> <servlet> <servlet-name>dispatcherServlet</servlet-name ...
- hdu - 1728逃离迷宫 && hdu - 1175 连连看 (普通bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1728 这两道题花了一下午的时候调试,因为以前做过类似的题,但是判断方向的方法是错的,一直没发现啊,真无语. 每个 ...
- cookie使用
知识拷贝. 理论很简单,而且模式也和大多请求 http://blog.csdn.net/lanmao100/article/details/2328491(源地址).返回状态的SSO差不多.但是有几个 ...
- MongoDB 学习笔记(三) MongoDB (replica set) 集群配置
MongoDB Replica Sets的结构类似于以集群,完全可以把他当成一个集群,因为他确实与集群实现的作用是一样的:如果其中一个节点出现故障,其他的节点会马上将业务接管过来.而无需停机操作 Mo ...
- 非常非常好!写了好久 k-th-smallest-in-lexicographical-order
https://leetcode.com/problems/k-th-smallest-in-lexicographical-order/ 我没做出来.还是脑子不够清醒. 下面这个解法真的很棒很棒. ...
- 转:最值得学习阅读的10个C语言开源项目代码
阅读优秀代码是提高开发人员修为的一种捷径…… 1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试 ...
- Innodb物理存储结构系列2 行记录格式
前一篇讨论了Innodb system,表空间,文件的关系及数据结构,这一篇记录下Innodb行记录的格式. 前提: 1. server层和innodb层都有自己对于record的记录格式,需要进行转 ...