参考:

std::string to char*

C++ 将 std::string 转换为 char*

目前没有直接进行转换的方法。必须通过string对象的c_str()方法,获取C-style的字符串:

std::string str = "string";
const char *cstr = str.c_str();

注意,该方法返回的类型为const char *,不能直接修改返回的C-style字符串,若需要修改则必须先拷贝该字符串:

std::string str = "string";
char *cstr = new char[str.length() + 1];
strcpy(cstr, str.c_str());
// 对cstr的处理
delete [] cstr;

另一种方法是将string转换得到的C-style的字符串存储于vector中:

std::vector<char> cstr(str.c_str(), str.c_str()+str.size()+1);

2018.7.2

C++ 将 std::string 转换为 char*的更多相关文章

  1. C++不存在从std::string转换为LPCWSTR的适当函数

    LPCWSTR是什么类型呢? 看看如何定义的: typedef const wchar_t* LPCWSTR; 顾名思义就是: LPCWSTR是一个指向unicode编码字符串的32位指针,所指向字符 ...

  2. 高德地图引入库错误std::string::find_first_of(char const*, unsigned long, unsigned long) const"

    一:std:编译器错误解决 二:错误提示 "std::string::find_first_of(char const*, unsigned long, unsigned long) con ...

  3. std::string与char*之临时缓冲区

    std::string与char*之临时缓冲区 原文:https://blog.csdn.net/hsshh1988/article/details/80689330 c++文件读取流程如下: ifs ...

  4. C++ std::unordered_map使用std::string和char *作key对比

    最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作ke ...

  5. 托管C++中System::String^ 转换为 char*

    https://docs.microsoft.com/en-us/cpp/dotnet/how-to-convert-system-string-to-standard-string?view=vs- ...

  6. C++中string转换为char*类型返回后乱码问题

    问题来源: 在写二叉树序列化与反序列化时发现序列化函数为char* Serialize1(TreeNode *root)  其函数返回类型为char*,但是我在实现的过程中为了更方便的操作添加字符串使 ...

  7. C++Builder RAD Studio XE, UTF-8 String 转换为 char * 字符串的最简单方式, 常用于sqlite3开发

    前段时间突然使用sqlite3开发,中间需要用中文,XE的缺省char*直接使用中文,在sqlite *.db3的数据库表格中显示是乱码,用数据库管理器来浏览等管理时非常不便. 于是决定还是使用utf ...

  8. QString, Std::string, char *相互转换

    Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) ...

  9. c++ string类型转换为char *类型

    string 是c++标准库里面其中一个,封装了对字符串的操作 把string转换为char* 有3中方法: 1.data 如: string str="abc"; char *p ...

随机推荐

  1. Spring AOP 随记

    本周经历各种面试失败后,最后一站张建飞老大的阿里,感觉有着这般年纪不该有的垃圾履历而忧伤中,不过还是要继续加油的,毕竟他说的好,都是经历,无愧初心. 所以为了更加深入理解Spring AOP我又翻起了 ...

  2. 剑指offer——python【第29题】最小的K个数

    题目描述 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 思路 先排序后取数,排序可以用冒泡,插入,选择,快排,二分法等等, ...

  3. 树状数组 || 线段树 || Luogu P5200 [USACO19JAN]Sleepy Cow Sorting

    题面:P5200 [USACO19JAN]Sleepy Cow Sorting 题解: 最小操作次数(记为k)即为将序列倒着找第一个P[i]>P[i+1]的下标,然后将序列分成三部分:前缀部分( ...

  4. 常用邮箱的 IMAP/POP3/SMTP 设置

    通过网上查找的资料和自己的总结完成了下面的文章,看完之后相信大家对这三种协议会有更深入的理解.如有错误的地方望指正. POP3 POP3是Post Office Protocol 3的简称,即邮局协议 ...

  5. 线程同步-Barrier类

    Barrier类:用于组织多个线程及时在某一个时刻碰面.并提供了一个回调函数,每次线程调用了SignalAndWait方法后该回调函数会被执行. 代码Demo: using System;using ...

  6. linux+nginx+phpfpm 访问出现Access denied错误解决方案

    linux上安装nginx,php-fpm后访问页面一直出现Access denied错误. 网上搜原因大概如下图: 我试了第一个方案,然后就好了.

  7. Python基础(二)自定义函数

    1.判断字符串,内容是否为数字 我们用python:xlrd读Excel内容时,本来只是输入的整数字,经常读出来的是float类型 我们需要自动转成整型,意思就是说,读出来的和我们输入的一样,但是,我 ...

  8. eclispe集成Scalas环境后,导入外部Spark包报错:object apache is not a member of package org

    在Eclipse中集成scala环境后,发现导入的Spark包报错,提示是:object apache is not a member of package org,网上说了一大推,其实问题很简单: ...

  9. python工具的选择

    自己喜欢用pycharm,下载地址:https://www.jetbrains.com/products.html#lang=python 补丁地址:http://idea.lanyus.com/

  10. lua语言中的假

    [1]测试及结论 (1)代码 local var_false = false local var_nil = nil if var_zero then print('var_zero : true') ...