
- 64位系统中
- long long unsigned int
- 定 义
- 在cstddef头文件中
- 中文名
- size_t
- 外文名
- unsigned int
-
size_t
2019-09-09 16:05:51size_t 是作为sizeof运算符的返回值 它并不是一个新类型,其实是由typedef来实现的。typedef double aaa;是对double类型声明了新别名叫做aaa;size_t也是一样,typedef unsigned int size_t;或typedef unsigned ...- size_t 是作为
sizeof
运算符的返回值 - 它并不是一个新类型,其实是由
typedef
来实现的。typedef double aaa;
是对double
类型声明了新别名叫做aaa
;size_t
也是一样,typedef unsigned int size_t;
或typedef unsigned long
表明对unsigned int
或unsigned long
起了一个叫size_t
的别名;在打印size_t
的时候,用%u
或%lu
来打印。 - 注:上述2中,
unsigned int
和unsigned long
的区别在于是32位机还是64位机,对于32位机size_t
占4个字节,而对于64位机size_t
占8个字节。
- size_t 是作为
-
C中int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t区别
2018-12-04 12:05:06工作中经常碰到int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t等数据类型,所以有必要对此进行梳理。 int_t同类 int_t 为一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义...工作中经常碰到int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t等数据类型,所以有必要对此进行梳理。
int_t同类
int_t 为一个结构的标注,可以理解为type/typedef的缩写,表示它是通过typedef定义的,而不是一种新的数据类型。因为跨平台,不同的平台会有不同的字长,所以利用预编译和typedef可以最有效的维护代码。
- int8_t : typedef signed char;
- uint8_t : typedef unsigned char;
- int16_t : typedef signed short ;
- uint16_t : typedef unsigned short ;
- int32_t : typedef signed int;
- uint32_t : typedef unsigned int;
- int64_t : typedef signed long long;
- uint64_t : typedef unsigned long long;
Specifier Common Equivalent Signing Bits Bytes Minimum Value Maximum Value int8_t
signed char
Signed 8 1 -128 127 uint8_t
unsigned char
Unsigned 8 1 0 255 int16_t
short
Signed 16 2 -32,768 32,767 uint16_t
unsigned short
Unsigned 16 2 0 65,535 int32_t
int
Signed 32 4 -2,147,483,648 2,147,483,647 uint32_t
unsigned int
Unsigned 32 4 0 4,294,967,295 int64_t
long long
Signed 64 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807 uint64_t
unsigned long long
Unsigned 64 8 0 18,446,744,073,709,551,615 size_t与ssize_t
size_t主要用于计数,如sizeof函数返回值类型即为size_t。在不同位的机器中所占的位数也不同,size_t是无符号数,ssize_t是有符号数。
- 在32位机器中定义为:typedef unsigned int size_t; (4个字节)
- 在64位机器中定义为:typedef unsigned long size_t;(8个字节)
由于size_t是无符号数,因此,当变量有可能为负数时,必须使用ssize_t。因为当有符号整型和无符号整型进行运算时,有符号整型会先自动转化成无符号。
int main() { unsigned short a; short int b = -1; a = b; cout << "b=" << b << endl; //b=-1 cout << "a=" << a << endl; //a=65535 }
此外,int 无论在32位还是64位机器中,都是4个字节, 且带符号,可见size_t与int 的区别之处。
-
size_t和size_type
2018-10-08 20:24:32为了使自己的程序有很好的移植性,C++程序员应该尽量使用size_t和size_type,而不是int,unsigned。 在标准C/C++的语法中,只有int float char bool等基本的数据类型,至于size_t,或size_type都是以后的编程人员...为了使自己的程序有很好的移植性,C++程序员应该尽量使用size_t和size_type,而不是int,unsigned。
在标准C/C++的语法中,只有int float char bool等基本的数据类型,至于size_t,或size_type都是以后的编程人员为了方便记忆所定义的一些便于理解的由基本数据类型的变体类型。
size_t是为了方便系统之间的移植而定义的,它是一个无符号整型,在32位系统上定义为:unsigned int;在64位系统上定义为unsigned long。size_t一般用来计数,sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。它的意义大致是“适用于内存中可容纳的数据项目的个数的无符号整数类型”所以,它在数组下标和内存管理函数之类的地方广泛使用。例如:typedef int size_t;定义了size_t为整型。因为size_t类型的数据其实是保存了一个整数,所以它也可以做加减乘除,也可以转化为int并赋值给int类型的变量。类似的还有wchar_t, ptrdiff_t等。
size_type是由string类类型和vector类类型定义的类型,用于保存任意string对象或vector对象的长度,标准库类型将size_type定义为unsigned类型。string::size_type它在不同的机器上,长度可以是不同的,并非固定的长度,但只要你使用了这个类型,就是的你的程序适这个机器,与实际机器匹配。
size_t和size_type的主要区别:
1. size_t是全局定义的类型;size_type是STL类中定义的类型属性。在使用STL中表明容器长度的时候,我们一般用size_type。
2. string::size_type 类型一般就是unsigned int, 但是不同机器环境长度可能不同 win32 和win64上长度差别; size_t一般也是unsigned int
3. size_t 使用的时候头文件需要 <cstddef> ;size_type 使用的时候需要<string>或者<vector>
4. 下述长度均相等,长度为 win32:4 win64:8
sizeof(string::size_type)
sizeof(vector<bool>::size_type)
sizeof(vector<char>::size_type)
sizeof(size_t)
5. 二者联系:在用下标访问元素时,vector使用vector::size_type作为下标类型(size_type是容器概念,没有容器不能使用),而数组下标的正确类型则是size_t
-
size_type、size_t、differentce_type以及ptrdiff_t
2014-09-25 11:28:06size_type、size_t、differentce_type以及ptrdiff_t 目录(?)[-] size_typesize_tdifferent_typeptrdiff_t size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,...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 = 0; 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_t,ssize_t和loff_t的区别
2020-03-11 13:56:09size_t 为 unsigned long/int 类型 ssize_t = signed size_t,为 long/int 类型 loff_t 为 long long 类型 其中,size_t和ssize_t为long还是int类型取决于操作系统,32位系统上为int,64位系统上为long。 ... -
size_t ssize_t loff_t 的区别
2016-02-19 10:46:16 转自:... Ssize_t 与size_t 跟踪linux源码得到以下宏: #ifndef _SIZE_T ...#define _SIZE_T ...typedef __kernel_size_t size_t; #endif #ifndef _S -
size_t size_type及wchar_t等类型
2014-08-16 20:09:24size_t在C语言中就有了。 它是一种“整型”类型,里面保存的是一个整数,就像int, long那样。这种整数用来记录一个大小(size)。size_t的全称应该是size type,就是说“一种用来记录大小的数据类型”。 通常我们用... -
size_t和int区别
2018-12-09 12:19:38size_t和int size_t是一些C/C++标准在stddef.h中定义的。这个类型足以用来表示对象的大小。size_t的真实类型与操作系统有关。 在32位架构中被普遍定义为: typedef unsigned int size_t; 而在64位架构中被定义... -
size_t,size_type,ptrdiff_t的区别
2013-03-12 11:37:41转自 : ... (1) size_t unsigned int ...size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t (2) ptrdiff_t signed int ptrdiff_t -
C++打印size_t和ssize_t和int64_t和uint64_t
2018-08-29 18:37:571.main.c //定义:typedef unsigned int size_t; typedef unsigned int uint32_t; typedef signed int ssize_t; int main(){ size_t a; ssize_t b; printf("a = %zu\n", a); // unsig... -
关于size_t, ptrdiff_t, size_type, difference_type
2013-04-29 10:53:16size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t. size_type是unsigned类型,表示... -
size_t、size_type、int
2015-10-29 22:11:11size_t和size_type 为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned 1. size_t是全局定义的类型;size_type是STL类中定义的类型属性,用以保存任意string和... -
size_t 与size_type
2012-12-12 18:54:31有时写代码的时候发现size_t和size_type都和整形有关 今天写一个strcpy_s()发现需要一个size_t的参数 我用了std::string 的size()函数, 返回的是一个size_type 这是就发现有趣的事情了 需要size_t的值 我传了... -
【c++】size_t 和 size_type的区别
2017-07-27 00:37:42【c++】size_t 和 size_type的区别 为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned 1. size_t是全局定义的类型;size_type是STL类中定义的类型属性,用以保存... -
C++ size_t
2019-10-31 11:27:45文章目录size_tssize_t和size_t比较size_t 和 int 比较 size_t size_t是一些C/C++标准在stddef.h中定义的,size_t类型表示C中任何对象所能达到的最大长度,它是无符号整数。 它是为了方便系统之间的移植而定义的,... -
C++ size_t 和size_type的区别
2017-03-03 20:10:55为了使自己的程序有很好的移植性,c++程序员应该尽量使用size_t和size_type而不是int, unsigned size_t是全局定义的类型;size_type是STL类中定义的类型属性,用以保存任意string和vector类对象的长度 string::size_... -
ssize_t 与 size_t
2013-11-14 20:36:10Ssize_t 与size_t 跟踪linux源码得到以下宏: #ifndef _SIZE_T #define _SIZE_T typedef __kernel_size_t size_t; #endif #ifndef _SSIZE_T #define _SSIZE_T typedef __kernel_ssize_t ssize_t; ... -
string::size_type和string::size_t和unsigned
2017-05-23 14:04:41三、值得注意的地方: 1、npos的类型 [cpp]view plaincopy intidx=str.find("abc");...上述代码中,idx的类型被定义为int,这是错误的,即使定义为 unsigned int 也是错的,它必须定义为 -
c++中size_t和size_type的区别
2015-03-20 16:57:17size_t: /* Define the size_t type in the std namespace if in C++ or globally if in C. If we're in C++, make the _SIZE_T macro expand to std::size_t */ #if !defined(_SIZE_T) && !defined(_SI