stl取出字符串中的字符_如何在C ++ STL中将字符转换为字符串?

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.


    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





评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛


翻译自: https://www.includehelp.com/stl/convert-a-character-to-string-in-cpp-stl.aspx

stl取出字符串中的字符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值