前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ 11字符数组/字符串/数字转换/字符串拼接

C++ 11字符数组/字符串/数字转换/字符串拼接

作者头像
SL_World
发布2021-09-18 15:25:16
3.1K0
发布2021-09-18 15:25:16
举报
文章被收录于专栏:X

文章目录

一、num转string

头文件

代码语言:javascript
复制
#include<string>
#include<typeinfo>

1.1 int型数字转字符串

代码语言:javascript
复制
int num = 123;
string num2str = to_string(num);
cout << typeid(to_string(num) == typeid(string) << endl;  // true

1.2 float/double型数字转字符串(不补0)

头文件

代码语言:javascript
复制
#include<sstream>
代码语言:javascript
复制
double num = 123.56;  // float同理
stringstream sstream;
sstream << num;
string num2str = sstream.str();  // num2str = "123.56"
cout << typeid(sstream.str() == typeid(string) << endl;  // true
sstream.clear();  // 若在用一个流中处理大量数据,则需手动清除缓存,小数据或不同流可忽略

缺点处理大量数据转换速度较慢stringstream不会主动释放内存,如果要在程序中用同一个流,需要适时地清除一下缓存,用stream.clear()

二、string转num

2.1 使用stringstream类处理

  • 字符串转int/float/double型数字(不补0)
代码语言:javascript
复制
string str = "456.78";
double num;        // float同理,int需要str为整数,否则报错
stringstream sstream(str);
sstream >> num;    // num = 456.78
cout << typeid(num == typeid(double) << endl;  // true

2.2 使用<string>处理

头文件

代码语言:javascript
复制
#include<string>
代码语言:javascript
复制
string str = "456.78";
double num = stod(str);   // num = 456.78    
cout << typeid(num == typeid(double) << endl;  // true

下面给出常用的转换方法,完整转换方法请见《C++中的字符串(String)和数值转换》

转换数字的类型

默认

完整参数

功能

全参例子

int

stoi(s)

stoi(s,p,b)

把字符串s从p开始转换成b进制的int

stoi(s, 0, 10)

float

stof(s)

stof(s,p)

把字符串s从p开始转换成float

double

stod(s)

stod(s,p)

把字符串s从p开始转换成double

long

stol(s)

stol(s,p,b)

把字符串s从p开始转换成b进制的long

stol(s, 0, 10)

三、char[]转num

头文件

代码语言:javascript
复制
#include<cstdio>
代码语言:javascript
复制
char ch[100] = "-456.78";
// 注:atof(ch)只返回double数字,因此需要float可以自行转换成float
double num = atof(ch);   // num = -456.78   
cout << typeid(num == typeid(double) << endl;  // true

下面给出常用的转换方法,完整转换方法请见《C++中的字符串(String)和数值转换》

转换数字的类型

默认

功能

int

atoi(s)

将字符串s[n]转换为整型值

double

atof(s)

将字符串s[n]转换为double

long

atol(s)

将字符串s[n]转换为long

四、char[]与string的相互转换

  • 4.1 字符数组char[]转换string(直接赋值即可)
代码语言:javascript
复制
char ch[100] = "Hellow World";
string str = ch;  // str = "Hellow World"
cout << typeid(str == typeid(string) << endl;  // true
  • 4.2 字符数组string转换char[]
代码语言:javascript
复制
string str = "Hellow World";  
char ch[100] = {0};
for (int i=0;i < str.length();i++)
	ch[i] = str[i];
cout << ch << endl;  // ch = "Hellow World"

五、字符串拼接

5.1 string + string

代码语言:javascript
复制
string str1 = "aaa";
strint str2 = "bbb";
cout << str1 + str2 << endl; // "aaabbb"
cout << str1 + "bbb" << endl; // "aaabbb"

5.1 string + char*

代码语言:javascript
复制
string str1 = "aaa";
char* str2 = "bbb";
cout << str1 + str2 << endl; // "aaabbb"

持续积累中~

参考文献

[1] C++ 字符串与字符数组详解 [2] C++中的字符串(String)和数值转换

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、num转string
    • 1.1 int型数字转字符串
      • 1.2 float/double型数字转字符串(不补0)
      • 二、string转num
        • 2.1 使用stringstream类处理
          • 2.2 使用<string>处理
          • 三、char[]转num
          • 四、char[]与string的相互转换
          • 五、字符串拼接
            • 5.1 string + string
              • 5.1 string + char*
              • 参考文献
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档