string、int 常见类型之间相互转换

int & string 之间的转换

  • C++中更多的是使用流对象来实现类型转换

针对流对象 sstream实现

int,float 类型都可以实现

#include <sstream>
//int convert to string
void int2str(const int &int_temp,string &string_temp){
stringstream s_stream;
s_stream<<int_temp;
string_temp=s_stream.str();
//s_stream>>string_temp; // 也可以实现
} //string convert to int
void str2int(const string &string_temp,int &int_temp){
stringstream s_stream(string_temp);
s_stream>>int_temp;
}
  • 其他的方法

c_str()函数

string.c_str() 可以将string字符串转换成一个指向与string相同字符数组的头指针

// atoi
void str2int(string &string_temp,int &int_temp){
int_temp=atoi(string_temp.c_str());
} // stoi实现
void str2int_stoi_version(string& string_temp,int &int_temp){
int_temp=stoi(string_temp);
}

字符数组char* 与string之间的转换

  • 字符数组转为string
char ch [] = "ABCDEFGHIJKL";
string str(ch);//也可string str = ch; // other way char ch [] = "ABCDEFGHIJKL";
string str;
str = ch;//在原有基础上添加可以用str += ch;
  • string转为字符数组
char buf[8];
string str("ABCDEFG");
length = str.copy(buf,8); //str.copy() return number of character copied
buf[length] = '\0'; //末尾置0 char buf[8];
string str("ABCDEFG");
strcpy(buf, str.c_str());//strncpy(buf, str.c_str(), 8);

strcpy()函数

//char* strcpy( char* dest, const char* src );
#include <iostream>
#include <cstring>
#include <memory>
int main()
{
const char* src = "Take the test.";
// src[0] = 'M'; // can't modify string literal
auto dst = std::make_unique<char[]>(std::strlen(src)+1); // +1 for the null terminator
std::strcpy(dst.get(), src);
dst[0] = 'M';
std::cout << src << '\n' << dst.get() << '\n';
}
// Take the test.
// Make the test.

strncpy()函数

// char *strncpy( char *dest, const char *src, std::size_t count );

#include <iostream>
#include <cstring>
int main()
{
const char* src = "hi";
char dest[6] = {'a', 'b', 'c', 'd', 'e', 'f'};
std::strncpy(dest, src, 5); std::cout << "The contents of dest are: ";
for (char c : dest) {
if (c) {
std::cout << c << ' ';
} else {
std::cout << "\\0" << ' ';
}
}
std::cout << '\n';
}
//The contents of dest are: h i \0 \0 \0 f

int 和 char

int a=1;
char c=a+'0'; //c的值就是'1'的ASCII码值

C++string,char* 字符数组,int类型之间的转换的更多相关文章

  1. char类型和int类型之间的转换

    在视屏课程第二章里,我们已经学习了一些常用的数据类型转换.然而,有一些时候我们会经常会遇到将char类型转换成int类型,或者需要将int类型转换为char类型的情况. 这里,我们来探讨一下这种不常用 ...

  2. 03.枚举和string以及int类型之间的转换

    练习1:   将枚举类型强转成int类型 namespace _04.枚举类型的练习01 { //声明一个QQState类型的枚举 public enum QQState { OnLine, OffL ...

  3. java中字符数组与字符串之间互相转换的方法

    public static void main(String[] args) { //1.字符数组 转换成 字符串 //(1)直接在构造String时转换 char[] array = new cha ...

  4. string类型和int类型之间的转换

    一.string转int 1. 使用string流 /* 字符串转整型 */ /* * istringstream:从 string 读取数据 * ostringstream:向 string 写入数 ...

  5. Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换

    public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ publ ...

  6. c#中RGB与int类型之间的转换

    Color color = Color.FromArgb(0, 0, 255);int colorInt = ParseRGB(color); --------------------- int Pa ...

  7. NSString / NSData / char* 类型之间的转换

    转自网络: NSString / NSData / char* 类型之间的转换 1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; ch ...

  8. DB2中字符、数字和日期类型之间的转换

    DB2中字符.数字和日期类型之间的转换 一般我们在使用DB2或Oracle的过程中,经常会在数字<->字符<->日期三种类 型之间做转换,那么在DB2和Oracle中,他们分别 ...

  9. int([x[, base]]) : 将一个字符转换为int类型,base表示进制

    int([x[, base]]) : 将一个字符转换为int类型,base表示进制 >>> int(-12) -12 >>> int(-12.00) -12 > ...

随机推荐

  1. Oracle 数据库登录、用户解锁、改密码、创建用户授权操作

    一.数据库登录1.常用账户: 管理员: sys主要练习操作用户: scott2.测试环境是否配置成功: 1.命令窗口 win+R -> cmd(以管理员身份运行) - > sqlplus ...

  2. Spring WebClient vs. RestTemplate

    1. 简介 本教程中,我们将对比 Spring 的两种 Web 客户端实现 -- RestTemplate 和 Spring 5 中全新的 Reactive 替代方案 WebClient. 2. 阻塞 ...

  3. 【iOS】The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods install

    从 github 下载的项目经常会遇到这个问题, 如图所示: 参考: iOS 'The sandbox is not sync with the Podfile.lock'问题解决 尚未解决…………

  4. Ubuntu+VMWare 学习中遇到的问题

    1. 虚拟机中Ubuntu分辨率 / 设置分辨率出现Unknown Display VMware中Ubuntu 出现Unknown Display问题解决 1.1 命令无法保存分辨率设置: xrand ...

  5. Echarts图表插件(4.x版本)使用(二、带分类筛选的多个图表/实例化多个ECharts,以关系图/force为例)

    导读 如果想在一个页面里实例化带分类筛选的多个Echarts该怎么做呢? 曾探讨了带分类选择的关系图显示为自定义图片的需求实现,传送门ECharts图表插件(4.x版本)使用(一.关系图force节点 ...

  6. kube-proxy源码解析

    kubernetes离线安装包,仅需三步 kube-proxy源码解析 ipvs相对于iptables模式具备较高的性能与稳定性, 本文讲以此模式的源码解析为主,如果想去了解iptables模式的原理 ...

  7. 使用 OpenSSL为WindowsServer远程桌面(RDP)创建自签名证书 (Self-signed SSL certificate)

    前言 笔者查阅很多资料,才写成此文章,如有错误,请读者们及时提出. 一般大家使用远程桌面(Remote Desktop)连接Windows Server时,总会有一个警告提示,如图1 图1 出现此警告 ...

  8. ZDog:简单便捷好玩的的3D设计和动画制作库

    各位老铁,我灰太狼又又又回来了,嘿嘿!!!!最近在忙所以有日子没写博客了,今天带大家看个好玩的东西 这个东西是今天偶尔看到的,是啥呢,难道是漂亮的小姐姐吗?当然是......不可能的了,这个东西其实就 ...

  9. java并发编程(九)----(JUC)CyclicBarrier

    上一篇我们介绍了CountDownlatch,我们知道CountDownlatch是"在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待",即CountDownL ...

  10. java并发编程(十九)----(JUC集合)总体框架介绍

    本节我们将继续学习JUC包中的集合类,我们知道jdk中本身自带了一套非线程安全的集合类,我们先温习一下java集合包里面的集合类,然后系统的看一下JUC包里面的集合类到底有什么不同. java集合类 ...