铁盒 东莞网站建设,南京美容网站建设,重庆电商网站,做网站知名公司从逻辑上讲#xff0c;size()成员函数应该似乎返回整型数值#xff0c;但事实上#xff0c;size操作返回是string::size_type类型的值。string类类型和其他许多库类型都定义了一些配套类型(companion type)。通过这些配套类型#xff0c;库函数的使用就与机器无关(machine-… 从逻辑上讲size()成员函数应该似乎返回整型数值但事实上size操作返回是string::size_type类型的值。string类类型和其他许多库类型都定义了一些配套类型(companion type)。通过这些配套类型库函数的使用就与机器无关(machine-independent)。size_type就是这些配套类型中的一种。它定义为与unsigned型unsigned int获unsigned long具有相同含义而且保证足够大的能够存储任意的string对象的长度。string::size_type它在不同的机器上长度是可以不同的并非固定的长度。但只要你使用了这个类型就使得你的程序适合这个机器。与实际机器匹配。string对象的索引也应为size_type类型。 npos表示size_type的最大值用来表示不存在的位置。find()成员函数的返回值为size_type,平台编译器为32位机器为64位。 string s1 Hello;string::size_type count 5;int c 0;long k 0;counts1.find(w);c s1.find(w);bool flag1 (count string::npos);bool flag2 (c string::npos);coutflag1:flag1endlflag2:flag2endl;coutsize_type:countendlint:cendl;coutstring::pos值string::nposendl;coutsize of int:sizeof(c)endl;coutsize of size_type:sizeof(count)endl;coutsize of long:sizeof(k)endl; 运行结果 转载于:https://www.cnblogs.com/catpainter/p/8444751.html