stl取出字符串中的字符
将字符转换为字符串 (Converting character to string)
Sometimes, It may be needed to convert a character variable to a string variable. In C++, we can do this by calling the constructor of the string class. For example, 'a' will be converted to "a".
有时,可能需要将字符变量转换为字符串变量 。 在C ++中,我们可以通过调用字符串类的构造函数来实现。 例如, 'a'将被转换为“ a” 。
'a' is a character, whereas "a" is a string value,
char c = 'a';
string s(1,c); //s = "c"
“ a”是字符,而“ a”是字符串值,
char c ='a';
字符串s(1,c); // s =“ c”
C ++字符串构造器详细信息 (C++ String Constructor details)
Prototype:
原型:
string(int, char);
Parameter:
参数:
int n;//n=1
char c;
Return type: Conceptually it's returning (constructing actually) the string. But actually since it's constructor it has no return type.
返回类型:从概念上讲,它正在返回(实际上是在构造)字符串。 但是实际上,由于它是构造函数,因此没有返回类型。
Example:
例:
Like we define and declare,
char c='I';
string s(1,c); //s="I"
if(s=="I")
cout<
Remember, a string variable (literal) need to be defined under "". 'a' is a character whereas "a" is a string.
Header file needed:
C++ program to convert a character to the string
#include <bits/stdc++.h>
using namespace std;
int main(){
char c;
cout<<"Input character to convert\n";
cin>>c;
string s(1,c);
cout<<"Converted to string: "<<s<<endl;
return 0;
}
Output
Like we define and declare,
char c='I';
string s(1,c); //s="I"
if(s=="I")
cout<
Remember, a string variable (literal) need to be defined under "" . 'a' is a character whereas "a" is a string.
Header file needed:
C++ program to convert a character to the string
# include < bits/stdc++.h >
using namespace std ;
int main ( ) {
char c ;
cout < < " Input character to convert \n " ;
cin > > c ;
string s ( 1 , c ) ;
cout < < " Converted to string: " < < s < < endl ;
return 0 ;
}
Output
翻译自: https://www.includehelp.com/stl/convert-a-character-to-string-in-cpp-stl.aspx
stl取出字符串中的字符