std::string length() and size() member functions(std::string length() 和 size() 成员函数)
问题描述
我正在阅读这个问题的答案,发现实际上有一个方法叫做length()
用于 std::string
(我一直使用 size()
).在 std::string
类中使用此方法是否有任何特定原因?我阅读了 MSDN 和 CppRefernce,它们似乎表明 size()
和 length()
之间没有区别.如果是这样,是不是让课程的用户更加困惑?
I was reading the answers for this question and found that there is actually a method called length()
for std::string
(I always used size()
). Is there any specific reason for having this method in std::string
class? I read both MSDN and CppRefernce, and they seem to indicate that there is no difference between size()
and length()
. If that is so, isn't it making more confusing for the user of the class?
推荐答案
根据 文档,这些只是同义词.size()
是否与其他 STL 容器(如vector
、map
、等)保持一致,并且length()
是要符合大多数人对字符串的直观概念.人们通常谈论一个单词、句子或段落的长度,而不是它的大小,所以 length()
是为了让事情更具可读性.
As per the documentation, these are just synonyms. size()
is there to be consistent with other STL containers (like vector
, map
, etc.) and length()
is to be consistent with most peoples' intuitive notion of character strings. People usually talk about a word, sentence or paragraph's length, not its size, so length()
is there to make things more readable.
这篇关于std::string length() 和 size() 成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:std::string length() 和 size() 成员函数


基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01