关于std::string
主要注意的一个问题是:std::string 实际是类似一个 vector<char>的结构。 它里面是可以存放 ascii为0 的字符
不算结尾 (否则 unicode方式的编码存放就有问题) 实际长度 用 length()获取
至于string赋值和构造有好几种方式,可以看文档。
举例说明:
char c[10]="wer|\t";
c[4] = 0;
std::string s =c;
由于c是char* 长度只能用strlen获取, strlen遇到0 就结束了,所以就丢失数据了。
构造和赋值,要把长度传进去:
std::string s(c,6); //构造
s.assign(c,6); //赋值, 这个是包括结尾符0的;;;不足就补0;
s.assign(c,5); //这个不包括结尾 0 , 比如protobuf 序列化出来的串,不用多加一个0。
关于std::string的更多相关文章
- QString 和std::string互转
std::string cstr; QString qstring; //****从std::string 到QString qstring = QString(QString::fromLocal8 ...
- std::string的split函数
刚刚要找个按空格分离std::string的函数, 结果发现了stackoverflow上的这个问题. 也没仔细看, 直接拿来一试, 靠, 不对啊, 怎么分离后多出个空字符串, 也就是 "a ...
- could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const std::string'
VS2008, 写一个简单的demo的时候出现了这个: 1>------ Build started: Project: GetExportTable, Configuration: Relea ...
- 源码阅读笔记 - 3 std::string 与 Short String Optimization
众所周知,大部分情况下,操作一个自动(栈)变量的速度是比操作一个堆上的值的速度快的.然而,栈数组的大小是在编译时确定的(不要说 C99 的VLA,那货的 sizeof 是运行时计算的),但是堆数组的大 ...
- CString std::string相互转换
CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); s ...
- 计算std:string的字节长度
如果项目本身是使用 Unicode 字符集和utf8编码,std::string的length(),size()甚至是c的strLen取到的都是字节长度了,比如三个汉字,就是9, 以上情况不满足的话, ...
- 【原】error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string'
今天遇到一个非常难以排查的BUG,谷歌度娘都问过了依旧无解,最后自己重新尝试之后找到解决方案: 先看一下报错信息: 1>.\lenz.cpp(2197) error C2679: binary ...
- 类型安全且自动管理内存的返回 std::string 的 sprintf 实现
在这篇博文里,我提到了一个例子,说的是使用C++实现类型安全的printf.这个例子很惊艳,但是在我写程序的时候,并非那么"迫切"地需要它出现在我的工具箱中,因为它并不比普通的pr ...
- VC++ 中使用 std::string 转换字符串编码
目录 第1章说明 1 1.1 代码 1 1.2 使用 4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...
- std::string::npos mean
std::string::npos 表示 no position, 没位置, 没找到
随机推荐
- Pain for friend
For a guy who has experienced his fair share of mysteries,on mystery,I still can't figure out is why ...
- poj 2533 Longest Ordered Subsequence(线性dp)
题目链接:http://poj.org/problem?id=2533 思路分析:该问题为经典的最长递增子序列问题,使用动态规划就可以解决: 1)状态定义:假设序列为A[0, 1, .., n],则定 ...
- config.json ajenti
{ "users": { "root": { "configs": { ...
- Curling 2.0(dfs回溯)
Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 15567 Accepted: 6434 Desc ...
- Proving Equivalences(加多少边使其强联通)
Proving Equivalences Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu1711(终于搞懂了KMP算法了。。)
题意:给你两个长度分别为n(1 <= N <= 1000000)和m(1 <= M <= 10000)的序列a[]和b[],求b[]序列在a[]序列中出现的首位置.如果没有请输 ...
- LeetCode: Sum Root to Leaf Numbers [129]
[题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...
- JavaScript算法描述(一)
function swap(arr,index1,index2){ var temp=arr[index1]; arr[index1]=arr[index2]; arr[index2]=temp; } ...
- jQuery 1.9+ ajaxStart事件无效,无法被触发的原因。
AJAX 事件需要绑定到document 在jQuery 1.9中, 全局的AJAX事件(ajaxStart, ajaxStop, ajaxSend, ajaxComplete, ajaxError, ...
- TextView实现多个TextView对象的走马灯效果
1:自定义一个控件继承TextView,重写isFocused方法,返回值为true; package com.example.helloandroid; import android.R.bool; ...