一个天天跟c#奋斗的苦逼c++程序猿 改自己曾经代码的时候发现有例如以下几行.

char szPath[MAX_PATH] = {0};

GetModuleFileNameA(NULL,szPath,sizeof(szPath));

std::string strPath = szPath;

std::string strDir = strPath.substr(0,strPath.find_last_of('\\'));

感觉不爽了,怎么又有char数组 又有string,都用string岂不是更好.

于是改为例如以下代码:

std:: strPath;

strPath.reserve(MAX_PATH);

GetModuleFileNameA(NULL,&strPath[0],MAX_PATH);

std::string strDir = strPath.substr(0,strPath.find_last_of('\\'));

OK 改完了. 一跑.........尼玛功能变了.你懂的.......

strDir为空.

为啥捏,strPath里面尽管有内容了,但API不会给string的size赋值啊.

假设想正确的用string取代char数组,仅仅能採用以下的方式.

std:: strPath;

strPath.resize(MAX_PATH,0);

GetModuleFileNameA(NULL,&strPath[0],strPath.size());

strPath.asign(strPath.c_str()); //又一次赋值.

这..........不是拖了裤子放屁吗?



脱了裤子放屁之std::string的更多相关文章

  1. QString 和std::string互转

    std::string cstr; QString qstring; //****从std::string 到QString qstring = QString(QString::fromLocal8 ...

  2. std::string的split函数

    刚刚要找个按空格分离std::string的函数, 结果发现了stackoverflow上的这个问题. 也没仔细看, 直接拿来一试, 靠, 不对啊, 怎么分离后多出个空字符串, 也就是 "a ...

  3. could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'

    VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...

  4. 源码阅读笔记 - 3 std::string 与 Short String Optimization

    众所周知,大部分情况下,操作一个自动(栈)变量的速度是比操作一个堆上的值的速度快的.然而,栈数组的大小是在编译时确定的(不要说 C99 的VLA,那货的 sizeof 是运行时计算的),但是堆数组的大 ...

  5. CString std::string相互转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...

  6. 计算std:string的字节长度

    如果项目本身是使用 Unicode 字符集和utf8编码,std::string的length(),size()甚至是c的strLen取到的都是字节长度了,比如三个汉字,就是9, 以上情况不满足的话, ...

  7. 【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'

    今天遇到一个非常难以排查的BUG,谷歌度娘都问过了依旧无解,最后自己重新尝试之后找到解决方案: 先看一下报错信息: 1>.\lenz.cpp(2197)  error C2679: binary ...

  8. 类型安全且自动管理内存的返回 std::string 的 sprintf 实现

    在这篇博文里,我提到了一个例子,说的是使用C++实现类型安全的printf.这个例子很惊艳,但是在我写程序的时候,并非那么"迫切"地需要它出现在我的工具箱中,因为它并不比普通的pr ...

  9. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

随机推荐

  1. bzoj 2803 [POI2012]prefixuffix hsh+性质

    题目大意 bzoj 2803 对于两个串S1.S2,如果能够将S1的一个后缀移动到开头后变成S2,就称S1和S2循环相同.例如串ababba和串abbaab是循环相同的. 给出一个长度为n的串S,求满 ...

  2. C语言标准库 qsort bsearch 源码实现

    C语言是简洁的强大的,当然也有很多坑.C语言也是有点业界良心的,至少它实现了2个最最常用的算法:快速排序和二分查找. 我们知道,对于C语言标准库 qsort和 bsearch: a. 它是“泛型”的, ...

  3. luogu 3708 koishi的数学题 递推 线性筛

    题目链接 题意 输入一个整数\(n\)\((n\leq 1e6)\),设\(f(x)=\sum_{i=1}^n x\mod i\),你需要输出\(f(1),f(2)...,f(n)\). 输入输出格式 ...

  4. du 查看 資料夾 佔用空間

    查看 目前目錄使用的空間大小 du -h --max-depth=0 -h, --human-readable 查看 目前及下一屠的目錄 使用的空間大小 du -h --max-depth=1

  5. upper_bound()和lower_bound()

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

  6. iOS上实现圆角图片

    UIImageView自带 //圆角设置 imageView.layer.cornerRadius = ;(值越大,角就越圆) imageView.layer.masksToBounds = YES; ...

  7. python 中各种数据类型的排序问题

    list #按照list的第二键值排序 disP2P = [[1,2,3],[2,3,4],[4,5,6]] disP2P = sorted(disP2P,key = lambda x:x[2]) s ...

  8. 快速掌握RabbitMQ(五)——搭建高可用的RabbitMQ集群

    RabbitMQ的集群是依赖erlang集群的,而erlang集群是通过.erlang.cookie文件进行通信认证的,所以我们使用RabbitMQ集群时只需要配置一下.erlang.cookie文件 ...

  9. BZOJ——2134: 单选错位

    http://www.lydsy.com/JudgeOnline/problem.php?id=2134 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: ...

  10. codevs——2548 自然数积分解

    2548 自然数积分解  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 把自然数N分解为若干个自然数之积,输出 ...