如何在C ++中使用静态转换技术更改此代码(以下代码正常工作) [英] How Do I Change This Code By Using Static Cast Technique In C++(Below Code Works Properly)

查看:74
本文介绍了如何在C ++中使用静态转换技术更改此代码(以下代码正常工作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for(int i = 0; i< 128; i ++)

{

string str =;

char chr = i;

stringstream chartostr;

chartostr<< CHR;

chartostr>> str;

}

i希望将char转换为字符串,上面的代码是正确的工作,但有没有专门的方法来做到这一点?

for(int i=0; i<128; i++)
{
string str = "";
char chr = i;
stringstream chartostr;
chartostr << chr;
chartostr >> str;
}
i want to convert char to string, above code is work corretly, but is there a pro way to do this ?

推荐答案

我会替换

I would replace
Quote:

string str =;

char chr = i;

stringstream chartostr;

chartostr<< CHR;

chartostr>> str;

string str = "";
char chr = i;
stringstream chartostr;
chartostr << chr;
chartostr >> str;

with



with

string str;
str += static_cast<char>(i);
</char>





我不明白的目的循环。你想做什么?


str 声明里面<$ c $的主体c> for 循环,当循环退出时,你没有任何东西可以显示它!这样的东西不会得到你想要的东西吗?

With str declared inside the body of your for loop, when the loop exits you have nothing to show for it! Wouldn't something like this get you what you want?
string Ascii7(128, '\0');
for (int i = 1; i < 128; ++i)
{
  Ascii7[i] = static_cast<char>(i);
}


这篇关于如何在C ++中使用静态转换技术更改此代码(以下代码正常工作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆