一、string概念

string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是用char*表示的。string与char*都可以用来表示字符串,那么二者有什么区别。

string和char*的比较:

  • string是一个类, char*是一个指向字符的指针。

    ​ string封装了char*,管理这个字符串,是一个char*型的容器。

  • string不用考虑内存释放和越界。

    ​ string管理char*所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

  • string提供了一系列的字符串操作函数

    ​ 查找find,拷贝copy,删除erase,替换replace,插入insert

//string 转 char*
string str_1="string";
const char* cstr_1=str.c_str();
//char* 转 string
char* cstr_2="char";
string str_2(cstr);

二、string的构造函数

  1. 默认构造函数:string(); //构造一个空的字符串string s1。

  2. 构造函数:string(const string &str); //构造一个与str一样的string。如string s1(s2)。

  3. 带参数的构造函数:

    string(const char *s); //用字符串s初始化

    string(int n,char c); //用n个字符c初始化

	string s1; //调用无参构造
string s2(10, 'a');
string s3("abcdefg");
string s4(s3); //拷贝构造 cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
/*
结果: aaaaaaaaaa
abcdefg
abcdefg
*/

三、string的存取字符操作

string类的字符操作:

  • const char &operator[] (int n) const; //通过[]方式取字符
  • const char &at(int n) const; //通过at方法获取字符
  • char &operator[] (int n);
  • char &at(int n);

operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。

主要区别在于at()在越界时会抛出异常,[]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。

	string s1 = "abcdefg";

	//重载[]操作符
for (int i = 0; i < s1.size(); i++) {
cout << s1[i] << " ";
}
cout << endl; //at成员函数
for (int i = 0; i < s1.size(); i++) {
cout << s1.at(i) << " ";
}
cout << endl; //区别:[]方式 如果访问越界,直接挂了
//at方式 访问越界 抛异常out_of_range try {
//cout << s1[100] << endl;
cout << s1.at(100) << endl;
}
catch (...) {
cout << "越界!" << endl;
}
/*
结果:
a b c d e f g
a b c d e f g
越界!
*/

四、从string取得const char*的操作

const char *c_str() const; //返回一个以'\0'结尾的字符串的首地址

//string 转 char*
string str_1="string";
const char *cstr_1=str.c_str();

五、把string拷贝到char*指向的内存空间的操作

int copy(char *s, int n, int pos=0) const;

把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目。注意要保证s所指向的空间足够大以容纳当前字符串,不然会越界。

	string s1 = "abcdefg";
int n = 5;pose
int pose = 3; char* s2 = (char*)malloc(n*sizeof(char));
if(s1.copy(s2, n, pose))
cout << s2 ;
else cout<<"error"<<endl;
/*
结果:
def
*/

六、string的长度

int length() const; //返回当前字符串的长度。长度不包括字符串结尾的'\0'。

bool empty() const; //当前字符串是否为空

	string s1 = "abcdefg";
string s2 = ""; int s1_length=s1.length();
bool s1_empty = s1.empty();
int s2_length = s2.length();
bool s2_empty = s2.empty(); cout << s1_length << " " << s1_empty << endl;
cout << s2_length << " " << s2_empty << endl;
/*
结果:
7 0
0 1
*/

七、string的赋值

string &operator=(const string &s);//把字符串s赋给当前的字符串

string &assign(const char *s); //把字符串s赋给当前的字符串

string &assign(const char *s, int n); //把字符串s的前n个字符赋给当前的字符串

string &assign(const string &s); //把字符串s赋给当前字符串

string &assign(int n,char c); //用n个字符c赋给当前字符串

string &assign(const string &s,int start, int n); //把字符串s中从start开始的n个字符赋给当前字符串

	string s1;
string s2("appp");
s1 = "abcdef";
cout << s1 << endl;
s1 = s2;
cout << s1 << endl;
s1 = 'a';
cout << s1 << endl; //成员方法assign
s1.assign("jkl");
cout << s1 << endl;
/*
结果:
abcdef
appp
a
jkl
*/

八、string字符串连接

string &operator+=(const string &s); //把字符串s连接到当前字符串结尾

string &operator+=(const char *s);//把字符串s连接到当前字符串结尾

string &append(const char *s); //把字符串s连接到当前字符串结尾

string &append(const char *s,int n); //把字符串s的前n个字符连接到当前字符串结尾

string &append(const string &s); //同operator+=()

string &append(const string &s,int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾

string &append(int n, char c); //在当前字符串结尾添加n个字符c

	string s = "abcd";
string s2 = "1111";
s += "abcd";
s += s2;
cout << s << endl; string s3 = "2222";
s2.append(s3);
cout << s2 << endl; string s4 = s2 + s3;
cout << s4 << endl;
/*
结果:
abcdabcd1111
11112222
111122222222
*/

九、string的比较

int compare(const string &s) const; //与字符串s比较

int compare(const char *s) const; //与字符串s比较

compare函数在>时返回 1,<时返回 -1,==时返回 0。比较区分大小写,比较时参考字典顺序,排越前面的越小。大写的A比小写的a小。

	string s1 = "abcd";
string s2 = "abce";
if (s1.compare(s2)==0) {
cout << "字符串相等!" << endl;
}
else {
cout << "字符串不相等!" << endl;
}

十、string的子串

string substr(int pos=0, int n=npos) const; //返回由pos开始的n个字符组成的子字符串

	string s = "abcdefg";
string mysubstr = s.substr(1, 3);
cout << mysubstr << endl;
/*
结果:
bcd
*/

十一、string的查找和替换

查找

int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置

int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置

int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置

//find函数如果查找不到,就返回-1

int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置

int rfind(const char *s, int pos=npos) const;

int rfind(const string &s, int pos=npos) const;

//rfind是反向查找的意思,如果查找不到, 返回-1

替换

string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s

string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s

void swap(string &s2); //交换当前字符串与s2的值

	// 字符串的查找和替换
string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
size_t index = s1.find("wbm", 0);
cout << "index: " << index; //求itcast出现的次数
size_t offindex = s1.find("wbm", 0);
while (offindex != string::npos){ cout << "在下标index: " << offindex << "找到wbm\n";
offindex = offindex + 1;
offindex = s1.find("wbm", offindex);
} //替换
string s2 = "wbm hello wbm 111 wbm 222 wbm 333";
s2.replace(0, 3, "wbm");
cout << s2 << endl; //求itcast出现的次数
offindex = s2.find("wbm", 0);
while (offindex != string::npos){ cout << "在下标index: " << offindex << "找到wbm\n";
s2.replace(offindex, 3, "WBM");
offindex = offindex + 1;
offindex = s1.find("wbm", offindex);
}
cout << "替换以后的s2:" << s2 << endl;
/*
结果:
index: 0在下标index: 0找到wbm
在下标index: 10找到wbm
在下标index: 18找到wbm
在下标index: 26找到wbm
wbm hello wbm 111 wbm 222 wbm 333
在下标index: 0找到wbm
在下标index: 10找到wbm
在下标index: 18找到wbm
在下标index: 26找到wbm
替换以后的s2:WBM hello WBM 111 WBM 222 WBM 333
*/

十二、String的区间删除和插入

string &insert(int pos, const char *s);

string &insert(int pos, const string &s);//在pos位置插入字符串s

string &insert(int pos, int n, char c); //在pos位置 插入n个字符c

string &erase(int pos=0, int n=npos); //删除pos开始的n个字符,返回修改后的字符串

	string s = "abcdefg";
s.insert(3, "111");
cout << s << endl; s.erase(0, 2);
cout << s << endl;
/*
结果:
abc111defg
c111defg
*/

STL_string容器的更多相关文章

  1. docker——容器安装tomcat

    写在前面: 继续docker的学习,学习了docker的基本常用命令之后,我在docker上安装jdk,tomcat两个基本的java web工具,这里对操作流程记录一下. 软件准备: 1.jdk-7 ...

  2. 网页提交中文到WEB容器的经历了些什么过程....

    先准备一个网页 <html><meta http-equiv="Content-Type" content="text/html; charset=gb ...

  3. [Spring]IoC容器之进击的注解

    先啰嗦两句: 第一次在博客园使用markdown编辑,感觉渲染样式差强人意,还是github的样式比较顺眼. 概述 Spring2.5 引入了注解. 于是,一个问题产生了:使用注解方式注入 JavaB ...

  4. 深入理解DIP、IoC、DI以及IoC容器

    摘要 面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.其中,OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念.通过本文我们将一起学 ...

  5. Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

    首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...

  6. JS判断鼠标进入容器方向的方法和分析window.open新窗口被拦截的问题

    1.鼠标进入容器方向的判定 判断鼠标从哪个方向进入元素容器是一个经常碰到的问题,如何来判断呢?首先想到的是:获取鼠标的位置,然后经过一大堆的if..else逻辑来确定.这样的做法比较繁琐,下面介绍两种 ...

  7. docker4dotnet #2 容器化主机

    .NET 猿自从认识了小鲸鱼,感觉功力大增.上篇<docker4dotnet #1 前世今生&世界你好>中给大家介绍了如何在Windows上面配置Docker for Window ...

  8. 深入分析Spring 与 Spring MVC容器

    1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...

  9. Set容器--HashSet集合

    Set容器特点: ①   Set容器是一个不包含重复元素的Collection,并且最多包含一个null元素,它和List容器相反,Set容器不能保证其元素的顺序; ②   最常用的两个Set接口的实 ...

随机推荐

  1. 关于AES-CBC模式字节翻转攻击(python3)

    # coding:utf-8 from Crypto.Cipher import AES import base64 def encrypt(iv, plaintext): if len(plaint ...

  2. vs2012新特性

    VS2012的六大技术特点: 1.VS2012和VS2010相比,最大的新特性莫过于对Windows 8Metro开发的支持.Metro天生为云端而生,简洁.数字化.内容优于形式.强调交互的设计已经成 ...

  3. Redis 设计与实现:Redis 对象

    本文的分析都是基于 Redis 6.0 版本源码 redis 6.0 源码:https://github.com/redis/redis/tree/6.0 在 Redis 中,有五大数据类型,都统一封 ...

  4. 麦格理银行借助DataStax Enterprise (DSE) 驱动数字化转型

    在本文中,我们将介绍DataStax Enterprise是如何助力澳大利亚最大的投资银行麦格理银行的数字银行,实现了实时分析和自然语言搜索等多项功能,并为用户提供了个性化的用户体验. "D ...

  5. 操作系统微内核和Dubbo微内核,有何不同?

    你好,我是 yes. 在之前的文章已经提到了 RPC 的核心,想必一个 RPC 通信大致的流程和基本原理已经清晰了. 这篇文章借着 Dubbo 来说说微内核这种设计思想,不会扯到 Dubbo 某个具体 ...

  6. Omega System Trading and Development Club内部分享策略Easylanguage源码

    更多精彩内容,欢迎关注公众号:数量技术宅.关于本期分享的任何问题,请加技术宅微信:sljsz01 关于 Omega System Trading and Development Club " ...

  7. 高效实用linux命令之-history

    History(历史)命令用法 15 例 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个 ...

  8. AOP的姿势之 简化混用 MemoryCache 和 DistributedCache 的方式

    0. 前言 之前写了几篇文章介绍了一些AOP的知识, 但是还没有亮出来AOP的姿势, 也许姿势漂亮一点, 大家会对AOP有点兴趣 内容大致会分为如下几篇:(毕竟人懒,一下子写完太累了,没有动力) AO ...

  9. 笔记本使用网线连接可以进行ftp下载,但是通过wifi连接只能登陆不能下载的问题。

    环境: (1)服务器为阿里云服务器,有公网ip,有内网ip,公网和内网已经做了相关端口的映射,ftp服务器为FileZilla,ftp服务器被动模式已开启,防火墙已关闭 (2)ftp客户端为java写 ...

  10. linux based bottlerocket-os

    linux based bottlerocket-os 概要 aws开源,专注与运行容器的linux os 参看 https://github.com/bottlerocket-os