为什么推荐std::string而不是char*】的更多相关文章

例如如下: map<const char*, const char*> map_test; map_test["a"] = "a"; map_test["a"] = "a"; map_test["a"] = "a"; 实际上会将三个"a"插入到map中,因为三个key的指针不同,所以被认为是三个不同的key.而如果改为如下用法: map<string…
std::string stringf(const char* format, ...){ va_list arg_list; va_start(arg_list, format); // SUSv2 version doesn't work for buf NULL/size 0, so try printing // into a small buffer that avoids the double-rendering and alloca path too... char short_b…
How to convert a std::string to const char* or char*? 1. If you just want to pass a std::string to a function that needs const char* you can use std::string str; const char * c = str.c_str(); If you want to get a writable copy, like char *, you can d…
Qt 库中对字符串类型进行了封装,QString 类提供了所有字符串操作方法,给开发带来了便利. 由于第三方库的类型基本上都是标准的类型,即使用std::string或char *来表示字符 (串) 类型,因此在Qt框架下需要将QString转换成标准字符 (串) 类型.下面介绍QString, Std::string, char *相互转换转换方法. std::string和char *的相互转换 1.  将char *或char[]转换为std::string 可直接赋值 std::stri…
最近在给自己的服务器框架加上统计信息,其中一项就是统计创建的对象数,以及当前还存在的对象数,那么自然以对象名字作key.但写着写着,忽然纠结是用std::string还是const char *作key,哪个效率高些.由于这服务器框架业务逻辑全在lua脚本,在C++需要统计的对象没几个,其实用哪个没多大区别.我纠结的是,很久之前就知道这两者效率区别不大,但直到现在我都还没搞清楚为啥,于是写些代码来测试. V1版本的代码如下: #ifndef __MAP_H__ #define __MAP_H__…
std::string与char*之临时缓冲区 原文:https://blog.csdn.net/hsshh1988/article/details/80689330 c++文件读取流程如下: ifstream ifs(srcFile, ifstream::binary); if(ifs.is_open()) { ifs.seekg(, ifs.end); long filesize = ifs.tellg(); ifs.seekg (); char* fileBuffer = new char…
C-string(char* const char*) basic_string<>特化版本:string charwstring wchar_tu16string char16_tu32string char32_t Setlocale是一个计算机函数,功能是用来配置地域的信息,设置当前程序使用的本地化信息.若 locale 为零(NULL),则不会改变地域化配置,返回当前的地域值,若系统尚未实作则返回 false.使用系统默认的设置调用setlocale(LC_ALL,"&quo…
LPCWSTR是什么类型呢? 看看如何定义的: typedef const wchar_t* LPCWSTR; 顾名思义就是: LPCWSTR是一个指向unicode编码字符串的32位指针,所指向字符串是wchar型,而不是char型. 转换函数  直接可以使用 LPCWSTR stringToLPCWSTR(std::string orig){size_t origsize = orig.length() + 1;    const size_t newsize = 100;    size_…
在这篇博文里,我提到了一个例子,说的是使用C++实现类型安全的printf.这个例子很惊艳,但是在我写程序的时候,并非那么"迫切"地需要它出现在我的工具箱中,因为它并不比普通的printf方便,而且它没有出现的标准库中.所以自己也懒得整.相反,这个函数的兄弟,sprintf,倒是一个非常需要的函数.不仅仅是因为需要它类型安全,而是 sprintf 有比 printf 更多的麻烦: 首先它确实也不是类型安全的 使用sprintf之前,必须要先准备一段buffer,但这个buffer的大小…
目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下: std::string stringA2W(const char* pA,int nA,UINT uCodePage = CP_ACP); std::string stringW2A(const wchar_t*pW,int nW,UINT uCodePage = CP_ACP); std::s…