字符串类string的求串长函数是

字符串类string的求串长函数是

string类有两个函数可以求长度。这两个函数完全等价。

size_t size() const noexcept;

size_t length() const noexcept;

这里返回类型size_t是一个无符号整形,具体实现由编译器决定,但应保证可以存放下理论上可能存在的对象的最大大小,该对象可以是任何类型,包括数组。const表示作为常量的string对象(const string)也可以调用这个函数。noexcept表示这个函数不会抛出异常。

样例:(摘自cplusplus.com)

// string::length
#include <iostream>
#include <string>

int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.length() << " bytes.\n"; //length()换成size()也可以
  return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-07
length();

#include <iostream>
using namespace std;
int main()
{
string a="123";
cout<<a.length();
}

运行结果:
3追问

size()行吗?

追答

可以的,是一样的

追问

谢谢

追答

麻烦你点一下采纳吧>_<

本回答被提问者和网友采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网