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的算法。

相关文章:

  • 2022-12-23
  • 2022-01-04
  • 2022-01-23
  • 2021-11-29
  • 2021-04-15
  • 2021-12-24
  • 2022-01-13
  • 2022-12-23
猜你喜欢
  • 2022-01-09
  • 2022-12-23
  • 2021-11-29
  • 2022-02-20
  • 2021-11-29
  • 2022-12-23
相关资源
相似解决方案