• 标准库类型string表示可变长的字符序列,为了在程序中使用string类型,我们必须包含头文件: #include <string> 
  • 声明一个字符串
    • 声明一个字符串有很多种方式,具体如下:
 string s;//调用默认构造函数,s为一个空字符串
string s(str);//等价于string s = str;调用拷贝构造函数,s是str的备份
string s(str,strindex);//将字符串str内始于strindex位置的部分当作s的初始值
eg.string str = "";
string s(str,);//s的初值为str由位置3开始的字符串,即456789
string s(str,stridx,strlen); // 将字符串str由stridx位置起始且长度为strlen的部分最为s的初值,如果strlen大于最大长度,则只截取字符串最大长度
eg.string s(str,,);//s=456789,由位置3开始,截取长度为10的部分,由于str剩余部分长度小于10,则截取str剩余最大长度
string s(cstr);//将C风格字符串作为s的初值
eg.string s("hello");//s的初值为hello
string s(cstr,length);//将C风格字符串的length长度部分作为s的初值
eg.string s("hello",);//s="he"
string s(num,c);//生成一个字符串,包含num个c字符
eg.string s(,'c');//s的初值为“cccccccccc”
  • 字符串操作函数

    • c++字符串的操作函数很多,这里把常用的罗列出来
 =、assign()//用于赋予新值,assign函数用于将一个字符串的部分内容赋值给另一个string对象
eg.string s1 = "hello";
string s2;
s2.assign(s1,,);//s2的值为“hel” swap() //交换两个字符串的内容
eg.string s1 = "hello";
string s2 = "world";
swap(s1,s2);//swap函数将s1和s2的内容交换,现在s1="world",s2="hello" +=、append()、push_back()//在字符串尾部追加内容,"+="可追加string对象,字符以及C风格字符串,append函数则可以追加string对象和C风格字符串,push_back函数则只能追加字符
eg.string s1 = "hello";
string s2 = " world";
s1 += s2;//正确,s1的值为”hello world“
s1 +="world";// 正确,s1的值为"hello world"
s1 +='c'; //正确,s1的值为"helloc" s1.append(s2);//正确,s1的值为"hello world"
s1.append(" world");//正确,s1的值为"hello world"
s1.append('c');//错误,append函数不支持追加字符 s1.push_back(s2);//错误
s1.push_back("world");//错误
s1.push_back('c');//正确 insert()//用于插入字符串
eg.string s1 = "hello";
s1.insert(,"world ");//s1的值为world hello erase()//用于删除字符的
eg.string str("This is an example phrase.");
string::iterator it;//迭代器 str.erase(,);//str的值为"This is an phrase.",删除了从位置10开始的8个字符 it = str.begin()+;//迭代器位置为9
str.erase(it);//删除了从it迭代器位置处的一个字符,str="This is a phrase." str.erase(str.begin()+,str.end()-);//删除两个参数之间的所有字符,str="This phrase." clear()函数和~string()//都是用来删除全部字符的
eg.str.clear();//删除str的全部字符,此时str为一个空串
str.~string();//销毁所有字符,释放内存 replace()函数,用于替换字符
eg..string line = "this@ is@ a test string!";
line = line.replace(line.find("@"),,"");//将line中从find的@位置开始替换一个长度的字符为"" 结果为this is@ a test string! ==、!=、<、<=、>、>=、compare()//比较字符串
eg.string s1 = "haha";
string s2 = "haha";
if(s1.compare(s2) == ){
cout << "相等" << endl;
} size()函数和length()函数,返回字符串的字符数
eg.string str = "haha";
str.size() 等于 str.length(),值均为4 empty()//判断字符串是否为空 下标法str[index]或者str.at(index)获取字符串内指定位置的字符 data()函数,将内容以字符数组的形式返回
  • C++字符串和C字符串的转换

    • C++提供的由C++字符串得到对应的C_string的方法是使用data()、c_str()和copy(),其中,data()以字符数组的形式返回字符串内容,但并不添加'\0'.
    • c_str()返回一个以'\0'结尾的字符数组
    • copy()则把字符串的内容复制或写入已有的c_string或字符数组内
  • 元素存取
    • 我们可以使用下标操作符[]和函数at()来对字符串的字符进行访问,但是应该注意的是下标操作符并不会检查索引是否有效,如果索引失效,会引起未定义的行为
    • at()函数则会检查索引,如果索引失效会抛出out_of_range异常
    • 注意,操作符可取到字符串尾部的'\0'字符

已知类string的原型为:

 class String
{
public:
String(const char *str = NULL);//普通构造函数
String(const String &other);//拷贝构造函数
~String(void);//析构函数 private:
char *m_data;//用于保存字符串
};

编写上述三个函数的实现:

 //普通构造函数
String:String(const char *str)
{
if(str == NULL){
m_data = new char[];
*m_data = '\0';
}else{
int length = strlen(str);
m_data = new char[length+];
strcpy(m_data,str);
}
} //析构函数
String::~String(void)
{
delete []m_data;
} //拷贝构造函数
String::String(const String &other)
{
int length = strlen(other.m_data);
m_data = new char[length+];
strcpy(m_data,other.m_data);
}

C++中的string详解的更多相关文章

  1. Android中Service(服务)详解

    http://blog.csdn.net/ryantang03/article/details/7770939 Android中Service(服务)详解 标签: serviceandroidappl ...

  2. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

  3. WCF中队列服务详解

    WCF中队列服务详解 一.引言 在前面的WCF服务中,它都要求服务与客户端两端都必须启动并且运行,从而实现彼此间的交互.然而,还有相当多的情况希望一个面向服务的应用中拥有离线交互的能力.WCF通过服务 ...

  4. Python中time模块详解

    Python中time模块详解 在平常的代码中,我们常常需要与时间打交道.在Python中,与时间处理有关的模块就包括:time,datetime以及calendar.这篇文章,主要讲解time模块. ...

  5. DevExpress控件GridControl中的布局详解 【转】

    DevExpress控件GridControl中的布局详解 [转] 2012-10-24 13:27:28|  分类: devexpress |  标签:devexpress  |举报|字号 订阅   ...

  6. JDK中Unsafe类详解

    Java中Unsafe类详解 在openjdk8下看Unsafe源码 浅析Java中的原子操作 Java并发编程之LockSupport http://hg.openjdk.java.net/jdk7 ...

  7. Asp.net中GridView使用详解(很全,很经典 转来的)

    Asp.net中GridView使用详解 效果图参考:http://hi.baidu.com/hello%5Fworld%5Fws/album/asp%2Enet中以gv开头的图片 l         ...

  8. springcloud中Feign配置详解

    Spring Cloud中Feign配置详解 到目前为止,小伙伴们对Feign的使用已经掌握的差不多了,我们在前文也提到Feign是对Ribbon和Hystrix的整合,那么在Feign中,我们要如何 ...

  9. Asp.net中GridView使用详解(引)【转】

    Asp.net中GridView使用详解(引) GridView无代码分页排序 GridView选中,编辑,取消,删除 GridView正反双向排序 GridView和下拉菜单DropDownList ...

随机推荐

  1. matlab安装matconvnet出错总结

    在安装过程中出现两种错误: mex -settup C 出错 mex -setup C 之后, vl_complilenn出错 matconv库需要autoNN和mcnExtraLayers两个库支持 ...

  2. 转《Python爬虫学习系列教程》学习笔记

    http://www.cnblogs.com/xin-xin/p/4297852.html

  3. Java 实现 POS 打印机无驱打印

    来源:https://www.ibm.com/developerworks/cn/java/j-lo-pos/index.html 行业需求 我们是一家专业做酒店餐饮软件的公司,餐饮软件一个重要的功能 ...

  4. HTML学习第七天(一)

    HTML学习第七天(一) aside元素用来表示当前或文章的附属信息部分,它可以包含与当前页面或主要内容的相关引用.侧边栏.广告.导航条,以及其他类似的有区别于主要内容的部分 <!DOCTYPE ...

  5. 5G/NR 帧结构

    原文链接:http://www.sharetechnote.com/html/5G/5G_FrameStructure.html 在学术界和3GPP中对帧结构进行了长时间的讨论,现在我们就NR(5G) ...

  6. 旧iPhone遭禁,会让苹果产业链迎来新转机吗?

    过去几个月,苹果的日子并不好过,先是新iPhone定价过高导致销售疲软,股价连续下跌,万亿市值失守,被微软和亚马逊超越:手机销量上则被华为赶超,整个iPhone产业链都有点儿"哀鸿遍野&qu ...

  7. Linux-10Year

    主流Linux发行版近10年排行曲线 10个主流的发行版概述(distrowatch挑选) 搜索特定的发行 选择建议     初级用户选择:开箱即用     中级用户组装:适合你自己的系统     高 ...

  8. 《新标准C++程序设计》4.6(C++学习笔记16)

    重载流插入运算符和流提取运算符 流插入运算符:“<<” 流提取运算符:“>>” cout 是在 iostream 中定义的,ostream 类的对象. “<<” 能 ...

  9. 7.1 Varnish VCL

    根据以上的配置增加集群,修改default.vcl # This ) # man page for details on VCL syntax and semantics. # # Default b ...

  10. 埃及分数问题 迭代加深搜索/IDA*

    输入整数a,b (0<a<b<500) ,输出最佳表达式 使得加数个数尽量小,如果加数个数相同,则最小的分数越大越好 ,输出表达式 考虑从小到大枚举深度上限maxd,每次执行只考虑深 ...