C++ string的那些坑,C++ string功能补充(类型互转,分割,合并,瘦身) ,c++ string的内存本质(简单明了的一个测试)
1. size_type find_first_of( const basic_string &str, size_type index = 0 );
查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,如果没找到就返回string::npos
2. string& replace (size_t pos, size_t len, const string& str);
从当前字符串的pos位置开始,长度为len的段落,替换成成str
3. int compare (const string& str)
结果为0,表示字符串相等,等价于字符串间的=
4. data()与c_str()的区别
data()是指返回字符数组,尾部可能有'\0',也可能没有.
c_str()是指返回C兼容的字符串,尾部肯定有'\0'
5. at()与[]的区别
[]没有检查越界,不会抛出异常,效率高
at()检查越界,抛出异常,安全度高
6.size_t copy (char* s, size_t len, size_t pos = 0) const;
int StringUtil::intFromString(string data)
{
//NOTE atoi是非标准C函数
return atoi(data.c_str());
}
string StringUtil::stringFromInt(int data)
{
char tmp[];
memset(tmp,,);
sprintf(tmp,"%10d",data);
return string(tmp);
}
double StringUtil::doubleFromString(string data)
{
double tmp;
sscanf(data.c_str(),"%lf",&tmp);
return tmp;
}
string StringUtil::stringFromDouble(double data)
{
char tmp[];
memset(tmp,,);
sprintf(tmp,"%20.3lf",data);
return string(tmp);
}
float StringUtil::floatFromString(std::string data)
{
float tmp;
sscanf(data.c_str(),"%f",&tmp);
return tmp;
}
std::string StringUtil::stringFromFloat(float data)
{
char tmp[];
memset(tmp,,);
sprintf(tmp,"%20.3f",data);
return string(tmp);
}
bool StringUtil::boolFromString(std::string data)
{
if(data.compare("true") == )
return true;
else
return false;
}
std::string StringUtil::stringFromBool(bool data)
{
if(data)
return string("true");
else
return string("false");
}
vector<std::string> StringUtil::splitStringToArray(std::string source, std::string seperator)
{
vector<string> result;
if(!source.empty())
{
string::size_type begin = ;
string::size_type end = ;
unsigned int sepSize = seperator.size();
while((end = source.find_first_of(seperator,begin))!=string::npos)
{
string item = source.substr(begin,end-begin);
result.push_back(item);
begin=end + sepSize;
}
//last item,注意如果最后是分割符,则最后的元素为空字符串
if(begin <= source.size())
{
string item = source.substr(begin,source.size() - begin);
result.push_back(item);
}
}
return result;
}
std::string StringUtil::linkArrayToString(vector<std::string> array, std::string seperator)
{
string result;
if(array.size() > )
{
unsigned int limit = array.size() - ;
for(unsigned int i=;i< limit;++i)
{
result+=array[i];
result+=seperator;
}
result += array[limit];
}
return result;
}
map<std::string, std::string> StringUtil::splitStringToMap(std::string source, std::string primarySep, std::string secondarySep)
{
vector<string> array = StringUtil::splitStringToArray(source,primarySep);
vector<string> tmpArray;
map<string,string> result;
for(unsigned int i = ; i< array.size();++i)
{
tmpArray = StringUtil::splitStringToArray(array[i],secondarySep);
if(tmpArray.size() >= )
{
result[tmpArray[]] = tmpArray[];
}
}
return result;
}
std::string StringUtil::linkMapToString(map<std::string, std::string> tmpMap, std::string primarySep, std::string secondarySep)
{
vector<string> tmpArray;
string tmpStr;
map<string,string>::iterator it = tmpMap.begin();
for(;it!=tmpMap.end();++it)
{
tmpStr = it->first+secondarySep+it->second;
tmpArray.push_back(tmpStr);
}
return StringUtil::linkArrayToString(tmpArray,primarySep);
}
std::string StringUtil::trimFront(std::string data)
{
unsigned int i = ;
for(;i<data.size()&&data.at(i)==' ';++i)
{
}
if(i<data.size())
return data.substr(i,data.size()-i);
else
return string("");
}
std::string StringUtil::trimBack(std::string data)
{
int i = data.size()-;
for(;i>=&&data.at(i)==' ';--i)
{
}
if(i>=)
return data.substr(,i+);
else
return string("");
}
std::string StringUtil::trim(std::string data)
{
string tmp = StringUtil::trimFront(data);
return StringUtil::trimBack(tmp);
}
http://www.cnblogs.com/guoxiaoqian/p/4113339.html
虽然没有研究过string的源代码,不过可以确定的是string的内存空间是在堆上开辟的,它自己负责释放空间,不用我们关系。
我们用一个动态分配的字符串指针初始化一个string对象retStr,它会做一个拷贝过程,将字符串考到retStr自己的内存空间里,之后retStr就跟ret没有任何关系了,因此我们要记得释放ret:
1 char* ret = (char*)malloc(len_str);
2 memset(ret,0,len_str);
3 //operate ret ...
4 string retStr(ret);
5 free(ret);
http://www.cnblogs.com/guoxiaoqian/p/3944805.html
C++ string的那些坑,C++ string功能补充(类型互转,分割,合并,瘦身) ,c++ string的内存本质(简单明了的一个测试)的更多相关文章
- 【记坑】Oracle数据库Date类型查询结果多出".0"的解决方法
oracle设置数据库某张表的字段类型为date,数据库存值为 2019-11-25 18:51:47 格式,但是从数据库查询出来之后格式为 String stopTime = map.get(&qu ...
- 个人永久性免费-Excel催化剂功能第28波-工作薄瘦身,安全地减少非必要冗余
Excel催化剂在完善了数据分析场景的插件需求后,决定再补充一些日常绝大多数Excel用户同样可以使用到的小功能,欢迎小白入场,在不违背太多Excel最佳实践的前提下,Excel催化剂乐意为广大Exc ...
- 【手记】小心在where中使用NEWID()的大坑 【手记】解决启动SQL Server Management Studio 17时报Cannot find one of more components...的问题 【C#】组件分享:FormDragger窗体拖拽器 【手记】注意BinaryWriter写string的小坑——会在string前加上长度前缀length-prefixed
[手记]小心在where中使用NEWID()的大坑 这个表达式: ABS(CHECKSUM(NEWID())) % 3 --把GUID弄成正整数,然后取模 是随机返回0.1.2这三个数,不可能返回其它 ...
- js扩展String.prototype.format字符串拼接的功能
1.题外话,有关概念理解:String.prototype 属性表示 String原型对象.所有 String 的实例都继承自 String.prototype. 任何String.prototype ...
- 【转】C#语言之“string格式的日期时间字符串转为DateTime类型”的方法
方法一:Convert.ToDateTime(string) string格式有要求,必须是yyyy-MM-dd hh:mm:ss ================================== ...
- [No00003B]string格式的日期时间字符串转为DateTime类型
新建console程序,复制粘贴直接运行: /**/ //using System.Globalization;//代码测试大致时间2015/11/3 15:09:05 //方法一:Convert.T ...
- C#语言之“string格式的日期时间字符串转为DateTime类型”的方法(转)
原文链接:http://www.cnblogs.com/Pickuper/articles/2058880.html 方法一:Convert.ToDateTime(string) string格式有要 ...
- Web Service接口返回泛型的问题(System.InvalidCastException: 无法将类型为“System.Collections.Generic.List`1[System.String]”的对象强制转换为类型“System.String[]”)
在使用C#写Web Service时遇到了个很奇怪的问题.返回值的类型是泛型(我用的是类似List<string>)的接口,测试时发现总是报什么无法转换为对象的错误,百思不得其解. 后来在 ...
- char,String,int类型互转
1.ascci码对应转换 字符,对应的ascii(其实是UTF-16)码: char c='a'; int k=(int) c; 结果k=97 数字,对应的字符: int k=9 ...
随机推荐
- [Recompose] Render Nothing in Place of a Component using Recompose
Learn how to use the ‘branch’ and ‘renderNothing’ higher-ordercomponents to render nothing when a ce ...
- [Angular] Zones and NgZone
NgZone, Angular uses it to profiling all the async actions such as setTimeout, http request and anim ...
- ASP.NET 的 ViewState Cookie Session 等的比較
类型 值保存在哪 值的有效范围 备注 View State client 不能跨页面传递.仅仅能在当前页面保存数据. 在HTML中能够看到ViewState值,只是是加密. 不是明文. ViewSta ...
- Python 标准库 —— glob
glob库是最简单的模块之一,内容非常少.用它可以查找符合特定规则的文件路径名.跟使用 windows 下的文件搜索差不多.查找文件只用到三个匹配符: "*", 匹配 0 个或多个 ...
- 正确使用pthread_create,防止内存泄漏
近日,听说pthread_create会造成内存泄漏,觉得不可思议,因此对posix(nptl)的线程创建和销毁进行了分析. 分析结果:如果使用不当,确实会造成内存泄漏. 产生根源:pthread ...
- Android多线程研究(9)——线程锁Lock
在前面我们在解决线程同步问题的时候使用了synchronized关键字,今天我们来看看Java 5.0以后提供的线程锁Lock. Lock接口的实现类提供了比使用synchronized关键字更加灵活 ...
- 【41.43%】【codeforces 560C】Gerald's Hexagon
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- hadoop集群安装(多机,非伪集群)
1. 创建用户 创建hadoop用户组:sudo addgroup hadoop 创建hadoop用户:sudo adduser -ingroup hadoop hadoop 为hadoop用户分配r ...
- AngularJS之基本指令(init、repeat)
<h3>ng-init初始化变量</h3> <div ng-init="name='aurora';age='18'"> <p ng-bi ...
- [Angular Form] ngModel and ngModelChange
When using Radio button for Tamplate driven form, we want to change to the value change and preform ...