string中常用的函数
|
发现在string在处理这符串是很好用,就找了一篇文章放在这里了.. 用 string来代替char * 数组,使用sort排序算法来排序,用unique 函数来去重 #include <string>#include <iostream>using namespace std;int main(){ string strinfo="Please input your name:"; cout << strinfo ; cin >> strinfo; if( strinfo == "winter" ) cout << "you are winter!"<<endl; else if( strinfo != "wende" ) cout << "you are not wende!"<<endl; else if( strinfo < "winter") cout << "your name should be ahead of winter"<<endl; else cout << "your name should be after of winter"<<endl; strinfo += " , Welcome to China!"; cout << strinfo<<endl; cout <<"Your name is :"<<endl; string strtmp = "How are you? " + strinfo; for(int i = 0 ; i < strtmp.size(); i ++) cout<<strtmp[i]; return 0;}
5、find函数
以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数: size_type find_first_of(const basic_string& s, size_type pos = 0)size_type find_first_of(const charT* s, size_type pos, size_type n)size_type find_first_of(const charT* s, size_type pos = 0)size_type find_first_of(charT c, size_type pos = 0)
所有的查找函数都返回一个size_type类型,这个返回值一般都是所找到字符串的位置,如果没有找到,则返回string::npos。其实string::npos表示的是-1。即没找到就返回-1。例子如下:#include <string>#include <iostream>using namespace std;int main(){ string strinfo=" //*---Hello Word!......------"; string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; int first = strinfo.find_first_of(strset); if(first == string::npos) { cout<<"not find any characters"<<endl; return -1; } int last = strinfo.find_last_of(strset); if(last == string::npos) { cout<<"not find any characters"<<endl; return -1; } cout << strinfo.substr(first, last - first + 1)<<endl;//string.substr是子串 return 0;}6、insert函数, replace函数和erase函数
string只是提供了按照位置和区间的replace函数,而不能用一个string字串来替换指定string中的另一个字串。例子:#include <string>#include <iostream>using namespace std;int main() { string strinfo="This is Winter, Winter is a programmer. Do you know Winter?"; cout<<"Orign string is :\n"<<strinfo<<endl; string_replace(strinfo, "Winter", "wende"); cout<<"After replace Winter with wende, the string is :\n"<<strinfo<<endl; return 0;}
string.erase(pos,srclen);//srclen是删除的长度string.insert(pos,strdst); //pos是定位,strdst是插入的函数void string_replace(string & strBig, const string & strsrc, const string &strdst) { string::size_type pos=0; string::size_type srclen=strsrc.size(); string::size_type dstlen=strdst.size(); while( (pos=strBig.find(strsrc, pos)) != string::npos){ strBig.erase(pos, srclen); strBig.insert(pos, strdst); pos += dstlen; }}
|
string中常用的函数的更多相关文章
- jQuery中常用的函数方法
jQuery中常用的函数方法总结 Ajax处理 load(url,[data],[callback]) url (String) : 待装入 HTML 网页网址. data (Map) : (可选) ...
- SQL点滴30—SQL中常用的函数
原文:SQL点滴30-SQL中常用的函数 该文章转载自http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 别人的总结,很 ...
- Mysql中常用的函数汇总
Mysql中常用的函数汇总: 一.数学函数abs(x) 返回x的绝对值bin(x) 返回x的二进制(oct返回八进制,hex返回十六进制)ceiling(x) 返回大于x的最小整数值exp(x) 返回 ...
- string 中的 length函数 和size函数 返回值问题
string 中的 length函数 和 size函数 的返回值 ( 还有 char [ ] 中 测量字符串的 strlen 函数 ) 应该是 unsigned int 类型的 不可以 和 -1 ...
- jQuery中常用的函数方法总结
jQuery中为我们提供了很多有用的方法和属性,自己总结的一些常用的函数,方法.个人认为在开发中会比较常用的,仅供大家学习和参考. 事件处理 ready(fn) 代码: $(document).rea ...
- Sass中常用的函数
字符串函数 To-upper-case() 函数将字符串小写字母转换成大写字母 To-lower-case() 函数 与 To-upper-case() 刚好相反,将字符串转换成小写字母 数字函数 S ...
- C++ string中的find()函数
1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...
- MYSQL初级学习笔记八:MySQL中常用的函数!(视频序号:初级_45-50)
知识点十:MySQL中的函数(45-50) 数学函数: 名称 描述 CEIL() 进一取整 FLOOR() 舍一取整 MOD 取余数(取摸) POWER() 幂运算 ROUND() 四舍五入 TRUN ...
- PHP中常用的函数
1.php 字符串截取函数 2.php取得当前时间函数 3.php 字符串长度函数 4.几种php 删除数组元素方法 5.php中var_dump()函数的详解说明 6.PHP preg_match正 ...
随机推荐
- 浅谈css中的position
什么是position,根据css 2.1中的描述,position和float的值决定了浏览器要采用那种定位算法来计算元素盒子的具体位置.先避开float不谈,本文主要介绍position属性的不同 ...
- IIS Express 及 vs2008下使用IIS Express
介绍 IIS Express 开发 ASP.NET 的应用程序是我的主要工作.当然我会选择最适合的开发环境.客户多属于企业用户,我的开发的选择,多半是 ASP.NET Web Application ...
- try-catch语句讲解
try-catch 语句由一个 try 块后跟一个或多个 catch 子句构成,这些子句指定不同的异常处理程序. 引发异常时,公共语言运行时 (CLR) 会查找处理此异常的 catch 语句. 如果当 ...
- 表单中<form>的enctype属性
application/x-www-form-urlencoded.multipart/form-data.text/plain 上传文件的表单中<form>要加属性enctype=&qu ...
- 从CMO到龙泉寺弟子:佛法改变人生
今天,移动精英开发社为大家请来了佛家弟子孫濟初师兄给大家分享学佛心得.濟初师兄根据自己的亲身经历,讲述了佛法对他人生的影响,以及他所获得的启示.本文系国内ITOM管理平台OneAPM授权转载. 孫濟初 ...
- 利用GBDT模型构造新特征具体方法
利用GBDT模型构造新特征具体方法 数据挖掘入门与实战 公众号: datadw 实际问题中,可直接用于机器学**模型的特征往往并不多.能否从"混乱"的原始log中挖掘到有用的 ...
- 评论 ”[实例] 设计基于JQM的WebApp“
点这里 DEMO 先上最近做的一个WebApp小应用,http://iwxy.me/m.html,大家可以先去玩玩儿,在移动终端访问查看最佳效果 实现的功能是微博上偶然看到的一个小测试,动物认识真实的 ...
- 全注解的SSH框架
基于struts2.23 + spring2.5.6 + hibernate3.6.4 + hibernate-generic-dao1.0(除了spring,我整合的都是最新的GA包,hiberna ...
- 安装DirectX SDK (June 2010) 失败(Error Code S1023)(转)
解决: 在控制面板里面搜索“Microsoft Visual C++*”,然后卸载,这个工具的名字大概是: Microsoft Visual C++ 2010 x86/x64 redistribuab ...
- poj 1724(有限制的最短路)
题目链接:http://poj.org/problem?id=1724 思路: 有限制的最短路,或者说是二维状态吧,在求最短路的时候记录一下花费即可.一开始用SPFA写的,900MS险过啊,然后改成D ...