zoukankan      html  css  js  c++  java
  • C++ Notes 1

    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

  • 相关阅读:
    html页面原生video标签隐藏下载按钮
    css解决多行溢出显示省略号
    移动端轮播图vue-awesome-swiper
    日常踩坑 — 相邻元素之间的margin合并问题。
    (a ==1 && a== 2 && a==3) 有可能是 true 吗?
    如何生成SSH key及查看SSH key
    端口号被占用报错解决方法。
    基于vue开发的element-ui树形控件报错问题解决
    Win10 桌面 通知中心 无法打开
    SolidWorks 杂
  • 原文地址:https://www.cnblogs.com/forcheryl/p/3875784.html
Copyright © 2011-2022 走看看