zoukankan      html  css  js  c++  java
  • 关于string::size_type

    size_type其实是string模板类定义的一种类型之一,它与size_of的用法相似,只是它根据存储的类型返回字符串的长度.对于 string具体化,将根据char返回字符串的长度,在这种情况下,size_type与size_of等效.size_of是一种无符号类型.  
    你说的   size_type   其实就是   unsigned   int   类型   其实并不正确,它要看其存储的类型.

    size_type实际上是做为解决string类设计上的严重失误而引入的。

    size_type是在string类内定义的一个无符号整形类型,由于string类里很多方法的返回值都是size_type类型的,所以必须使用size_type进行类型声明。
    如下:
    string   a= "abcdefg ";
    string::size_type   idx;
    idx   =   a.find( "h ");   //not   found,return   string::npos
    if(   idx   ==   string::npos   )     //check   if   not   find
    {
      .....
    }

    由于npos为find返回的特殊值(-1),而不幸的是,size_type类型为unsigned   int,所以
    不同的编译器对npos的理解不同。
    试想一下,用8字节来unsigned   int   来表示-1和用4字节表示,值是完全不同的。
    这可能是string类设计时的一个致命缺陷,为了兼容和可移植性,
    特定义size_type类型,在不同编译器中,size_type的解释也不同,这样就做到了可移植。

    以上内容可见
    STL:11.2.12节。

  • 相关阅读:
    python 运行CMD和shell命令
    python 装饰器2 常用装饰器【@property,@x.setter,@x.deleter】
    正则表达式 python re正则模块
    Interesting Finds: 2008.01.06
    Interesting Finds: 2008.01.11
    Interesting Finds: 2007.12.25
    Interesting Finds: 2007.12.26
    Interesting Finds: 2008.01.04
    Interesting Finds: 2008.01.03
    Interesting Finds: 2008.01.09
  • 原文地址:https://www.cnblogs.com/debuging/p/2612090.html
Copyright © 2011-2022 走看看