C++基础知识--size和length区别

时间:2025-02-11 18:55:15

C++标准库中的string中两者的源代码如下:

  size_type   __CLR_OR_THIS_CALL   length()   const   
  { //   return   length   of   sequence   
  return   (_Mysize);   
  }   
    
  size_type   __CLR_OR_THIS_CALL   size()   const   
  { //   return   length   of   sequence   
  return   (_Mysize);   
  }   

所以两者没有区别。

length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。

string类的size()/length()方法返回的是字节数,不管是否有汉字。


参考自:/lakeone/p/