stl string 函数
字符串作为数据类型 (String as datatype)
In C, we know string basically a character array terminated by ‘\0’. Thus to operate with the string we define character array. But in C++, the standard library gives us the facility to use the string as a basic data type as an integer. We can easily find the length of the string using length() function.
在C语言中,我们知道字符串基本上是一个以'\ 0'结尾的字符数组。 因此,要对字符串进行操作,我们定义了字符数组。 但是在C ++中,标准库为我们提供了将字符串用作基本数据类型(作为整数)的便利。 我们可以使用length()函数轻松地找到字符串的长度。
Prototype:
原型:
size_t string.length();
Parameter: None
参数:无
Return type: size_t
返回类型: size_t
Example:
例:
Like we define and declare,
string s1="Include", s2="Help";
int i=s1.length(); //7
int j=s2.length(); //4
After concatenating:
string s3=s1+s2;
(s3 is "IcludeHelp")
int k=s3.length(); //11
string s4=s2+s1;
(s4 is "HelpInclude")
int r=s4.length(); //11
Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.
请记住,需要在“”下定义一个字符串变量(文字)。 “ a”是字符,而“ a”是字符串。
Header file needed:
所需的头文件:
#include <string>
Or
#include <bits/stdc++.h>
C++ program to demonstrate example of string::length() function
C ++程序演示string :: length()函数的示例
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cout<<"enter string\n";
cin>>s;
cout<<"length of the string is: "<<s.length();
return 0;
}
Output
输出量
enter string
IncludeHelp
length of the string is: 11
翻译自: https://www.includehelp.com/stl/string-length-function-with-example-in-cpp-stl.aspx
stl string 函数