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. 【JS档案揭秘】第一集 内存泄漏与垃圾回收

    程序的运行需要内存,对于一些需要持续运行很久的程序,尤其是服务器进程,如果不及时释放掉不再需要的内存,就会导致内存堆中的占用持续走高,最终可能导致程序崩溃. 不再需要使用的内存,却一直占用着空间,得不 ...

  2. Android Studio 制作简单的App欢迎页面——基于Android 6.0

    在许多的Android App中,我们点击进入时,都可以看到一个欢迎页面,大概持续了几秒,然后跳转至主页面.以下是我开发过程中总结出的一些方法和例子. 一.创建一个新的Activity 首先,新建了一 ...

  3. request获取url链接和参数

            //Returns the part of this request's URL from the protocol name up to the query string in th ...

  4. maven-assembly-plugin 进行打包

    maven-assembly-plugin使用描述(拷自 maven-assembly-plugin 主页) The Assembly Plugin for Maven is primarily in ...

  5. Redis分布式锁实战

    什么是分布式锁 在单机部署的情况下,要想保证特定业务在顺序执行,通过JDK提供的synchronized关键字.Semaphore.ReentrantLock,或者我们也可以基于AQS定制化锁.单机部 ...

  6. str_replace导致的注入问题汇总

    研究了下replace的注入安全问题. 一般sql注入的过滤方式就是引用addslashes函数进行过滤. 他会把注入的单引号转换成\',把双引号转换成\",反斜杠会转换成\\等 写一段ph ...

  7. Java NIO学习系列七:Path、Files、AsynchronousFileChannel

    相对于标准Java IO中通过File来指向文件和目录,Java NIO中提供了更丰富的类来支持对文件和目录的操作,不仅仅支持更多操作,还支持诸如异步读写等特性,本文我们就来学习一些Java NIO提 ...

  8. HDP Hive性能调优

    (官方文档翻译整理及总结) 一.优化数据仓库 ① Hive LLAP  是一项接近实时结果查询的技术,可用于BI工具以及网络看板的应用,能够将数据仓库的查询时间缩短到15秒之内,这样的查询称之为Int ...

  9. JVM系列(3)- Java VisualVM使用

    前言 Java VisualVM是jdk自带一款工具,可以十分友好的监控java进程相关的应用服务及中间件. 工具位置 jdk的bin目录下,找到jvisualvm.exe,双击打开即可. 功能介绍 ...

  10. SpringBoot第二天

    一,SpringBoot 整合 jsp 技术 1,创建项目 2,修改 pom 文件,添加坐标 <project xmlns="http://maven.apache.org/POM/4 ...