3.1 string容器

3.1.1 string基本概念

本质:

  • string是C++风格的字符串,而string本质上是一个类

string和char * 区别:

  • char * 是一个指针
  • string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。

特点:

string 类内部封装了很多成员方法

例如:查找find,拷贝copy,删除delete 替换replace,插入insert

string管理char*所分配的内存,不用担心复制越界和取值越界等,由类内部进行负责

3.1.2 string构造函数

构造函数原型:

  • string(); //创建一个空的字符串 例如: string str;
    string(const char* s); //使用字符串s初始化
  • string(const string& str); //使用一个string对象初始化另一个string对象
  • string(int n, char c); //使用n个字符c初始化

示例:

#include <string>
//string构造
void test01()
{
string s1; //创建空字符串,调用无参构造函数
cout << "str1 = " << s1 << endl; const char* str = "hello world";
string s2(str); //把c_string转换成了string cout << "str2 = " << s2 << endl; string s3(s2); //调用拷贝构造函数
cout << "str3 = " << s3 << endl; string s4(10, 'a');
cout << "str3 = " << s3 << endl;
} int main() { test01(); system("pause"); return 0;
}

总结:string的多种构造方式没有可比性,灵活使用即可

3.1.3 string赋值操作

功能描述:

  • 给string字符串进行赋值

赋值的函数原型:

  • string& operator=(const char* s); //char*类型字符串 赋值给当前的字符串
  • string& operator=(const string &s); //把字符串s赋给当前的字符串
  • string& operator=(char c); //字符赋值给当前的字符串
  • 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赋给当前字符串

示例:

//赋值
void test01()
{
string str1;
str1 = "hello world";
cout << "str1 = " << str1 << endl; string str2;
str2 = str1;
cout << "str2 = " << str2 << endl; string str3;
str3 = 'a';
cout << "str3 = " << str3 << endl; string str4;
str4.assign("hello c++");
cout << "str4 = " << str4 << endl; string str5;
str5.assign("hello c++",5);
cout << "str5 = " << str5 << endl; string str6;
str6.assign(str5);
cout << "str6 = " << str6 << endl; string str7;
str7.assign(5, 'x');
cout << "str7 = " << str7 << endl;
} int main() { test01(); system("pause"); return 0;
}

总结:

​ string的赋值方式很多,operator= 这种方式是比较实用的

3.1.4 string字符串拼接

功能描述:

  • 实现在字符串末尾拼接字符串

函数原型:

  • string& operator+=(const char* str); //重载+=操作符
  • string& operator+=(const char c); //重载+=操作符
  • string& operator+=(const string& str); //重载+=操作符
  • string& append(const char *s); //把字符串s连接到当前字符串结尾
  • string& append(const char *s, int n); //把字符串s的前n个字符连接到当前字符串结尾
  • string& append(const string &s); //同operator+=(const string& str)
  • string& append(const string &s, int pos, int n);//字符串s中从pos开始的n个字符连接到字符串结尾

示例:

//字符串拼接
void test01()
{
string str1 = "我"; str1 += "爱玩游戏"; cout << "str1 = " << str1 << endl; str1 += ':'; cout << "str1 = " << str1 << endl; string str2 = "LOL DNF"; str1 += str2; cout << "str1 = " << str1 << endl; string str3 = "I";
str3.append(" love ");
str3.append("game abcde", 4);
//str3.append(str2);
str3.append(str2, 4, 3); // 从下标4位置开始 ,截取3个字符,拼接到字符串末尾
cout << "str3 = " << str3 << endl;
}
int main() { test01(); system("pause"); return 0;
}

总结:字符串拼接的重载版本很多,初学阶段记住几种即可

3.1.5 string查找和替换

功能描述:

  • 查找:查找指定字符串是否存在
  • 替换:在指定的位置替换字符串

函数原型:

  • int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
  • int find(const char* s, int pos = 0) const; //查找s第一次出现位置,从pos开始查找
  • int find(const char* s, int pos, int n) const; //从pos位置查找s的前n个字符第一次位置
  • int find(const char c, int pos = 0) const; //查找字符c第一次出现位置
  • int rfind(const string& str, int pos = npos) const; //查找str最后一次位置,从pos开始查找
  • int rfind(const char* s, int pos = npos) const; //查找s最后一次出现位置,从pos开始查找
  • int rfind(const char* s, int pos, int n) const; //从pos查找s的前n个字符最后一次位置
  • int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
  • string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
  • string& replace(int pos, int n,const char* s); //替换从pos开始的n个字符为字符串s

示例:

//查找和替换
void test01()
{
//查找
string str1 = "abcdefgde"; int pos = str1.find("de"); if (pos == -1)
{
cout << "未找到" << endl;
}
else
{
cout << "pos = " << pos << endl;
} pos = str1.rfind("de"); cout << "pos = " << pos << endl; } void test02()
{
//替换
string str1 = "abcdefgde";
str1.replace(1, 3, "1111"); cout << "str1 = " << str1 << endl;
} int main() { //test01();
//test02(); system("pause"); return 0;
}

总结:

  • find查找是从左往后,rfind从右往左
  • find找到字符串后返回查找的第一个字符位置,找不到返回-1
  • replace在替换时,要指定从哪个位置起,多少个字符,替换成什么样的字符串

3.1.6 string字符串比较

功能描述:

  • 字符串之间的比较

比较方式:

  • 字符串比较是按字符的ASCII码进行对比

= 返回 0

> 返回 1

< 返回 -1

函数原型:

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

示例:

//字符串比较
void test01()
{ string s1 = "hello";
string s2 = "aello"; int ret = s1.compare(s2); if (ret == 0) {
cout << "s1 等于 s2" << endl;
}
else if (ret > 0)
{
cout << "s1 大于 s2" << endl;
}
else
{
cout << "s1 小于 s2" << endl;
} } int main() { test01(); system("pause"); return 0;
}

总结:字符串对比主要是用于比较两个字符串是否相等,判断谁大谁小的意义并不是很大

3.1.7 string字符存取

string中单个字符存取方式有两种

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

示例:

void test01()
{
string str = "hello world"; for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl; for (int i = 0; i < str.size(); i++)
{
cout << str.at(i) << " ";
}
cout << endl; //字符修改
str[0] = 'x';
str.at(1) = 'x';
cout << str << endl; } int main() { test01(); system("pause"); return 0;
}

总结:string字符串中单个字符存取有两种方式,利用 [ ] 或 at

3.1.8 string插入和删除

功能描述:

  • 对string字符串进行插入和删除字符操作

函数原型:

  • string& insert(int pos, const char* s); //插入字符串
  • string& insert(int pos, const string& str); //插入字符串
  • string& insert(int pos, int n, char c); //在指定位置插入n个字符c
  • string& erase(int pos, int n = npos); //删除从Pos开始的n个字符

示例:

//字符串插入和删除
void test01()
{
string str = "hello";
str.insert(1, "111");
cout << str << endl; str.erase(1, 3); //从1号位置开始3个字符
cout << str << endl;
} int main() { test01(); system("pause"); return 0;
}

**总结:**插入和删除的起始下标都是从0开始

3.1.9 string子串

功能描述:

  • 从字符串中获取想要的子串

函数原型:

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

示例:

//子串
void test01()
{ string str = "abcdefg";
string subStr = str.substr(1, 3);
cout << "subStr = " << subStr << endl; string email = "hello@sina.com";
int pos = email.find("@");
string username = email.substr(0, pos);
cout << "username: " << username << endl; } int main() { test01(); system("pause"); return 0;
}

**总结:**灵活的运用求子串功能,可以在实际开发中获取有效的信息

更多信息请关注公众号:

[C++提高编程] 3.1 string容器的更多相关文章

  1. 数据结构《19》----String容器的三种实现

    一.序言 一个简单的string 容器到底是如何实现的? 本文给出了 String 的三种从易到难的实现,涉及了 reference counting, copy on write 的技术. 二.第一 ...

  2. 15个提高编程技巧的JavaScript工具

    原文地址:http://www.imooc.com/wenda/detail/243523 JavaScript脚本库是一个预先用JavaScript语言写好的库,它方便了我们开发基于JavaScri ...

  3. 17款提高编程效率的css工具

    参看下面链接:<17款提高编程效率的css工具>

  4. stl string 容器的使用

    string 是基本的字符串序列容器,对应数据结构中的串,和vector<char>也类似,但功能更多 string 容器的使用 1,string 的构造函数是. string() 2,s ...

  5. C++编程 - tuple、any容器

    C++编程 - tuple.any容器 flyfish 2014-10-29 一 tuple tuple是固定大小的容器,每一个元素类型能够不同 作用1 替换struct struct t1 { in ...

  6. 提高编程能力的7条建议 分类: T_TALENT 2014-04-12 10:41 294人阅读 评论(0) 收藏

    编程是非常酷的一件事情,但是在酷炫的背后它对很多人来说还是挺难的.很多人在学习编程之初就被困难击败了. 当你不熟悉编程的时候,你可能会觉得无从下手,并且不知道如何运用学到的知识.只要你通过了这一困难的 ...

  7. 浅谈C++ STL string容器

    浅谈C++ STL string容器 本篇随笔简单讲解一下\(C++STL\)中\(string\)容器的使用方法及技巧. string容器的概念 其实\(string\)并不是\(STL\)的一种容 ...

  8. Java编程思想之十七 容器深入研究

    17.1 完整的容器分类方法 17.2 填充容器 import java.util.*; class StringAddress { private String s; public StringAd ...

  9. 【Java并发编程二】同步容器和并发容器

    一.同步容器 在Java中,同步容器包括两个部分,一个是vector和HashTable,查看vector.HashTable的实现代码,可以看到这些容器实现线程安全的方式就是将它们的状态封装起来,并 ...

  10. 深入理解java:2.3.3. 并发编程concurrent包 之容器ConcurrentHashMap

    线程不安全的HashMap 因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap. 效率低下的HashTable容器 H ...

随机推荐

  1. webpack 3/4踩坑,我太难了,从安装、卸载、到使用,各相应的版本号,sass-loader报错-版本的原因,webpack -v 不识别,没卸载干净

     -先说卸载: wabpack@4对应的每个插件的版本号都在最后 1 全局安装的话,npm uninstall webpack -g 有时候并不能卸载干净, 2 webpack -v 可判断是否安装成 ...

  2. Linux 复制时排除某文件/目录

    如果要排除/home/data目录下面的a.b.c.三个目录,同时拷贝其它所有目录,执行rsync命令yum install rsync -y #安装rsync 排除单个文件/目录rsync -avP ...

  3. c#和JS数据加密(转)

    前台提交按纽 后以赋值后台取值    Base64编解码   C# /* 编码规则 Base64编码的思想是是采用64个基本的ASCII码字符对数据进行重新编码. 它将需要编码的数据拆分成字节数组. ...

  4. idea远程仓库无法更新导致的各种错误,jar包无法下载

    直接把下面的配置替换成settings.xml中的内容 <?xml version="1.0" encoding="UTF-8"?> <set ...

  5. warning: the `gets' function is dangerous and should not be used.

    LINUX下编译C程序时,出现了:warning: the `gets' function is dangerous and should not be used. 原因:Linux 下gcc编译器不 ...

  6. conda Installing on Linux - 搬运

    转自:Installing on Linux - conda 23.1.0 documentation Installing on Linux Download the installer: Min ...

  7. Stm32设置串口300波特率 2400 4800 9600

    Stm32设置串口300波特率   本文以串口4为例子: 在APB1为72MHz的时钟频率下,是设置不了300波特率的,原因在于 Tx/Rx baud = fck / (16 * reg_value) ...

  8. NDVI最大值合成

    这里有NDVI250m分辨率的数据,目标:合成年最大值数据 [Spatial Analyst工具]|[局部分析]|[像元统计数据]

  9. 集成电路仿真器(SPICE)的实现原理

    本文系统地介绍类SPICE集成电路仿真器的实现原理,包括改进节点分析(MNA).非线性器件建模.DC/AC分析.时域/(复)频域仿真以及涉及的数值方法. 基于介绍的原理,实现了SPICE-like仿真 ...

  10. Jmeter——性能测试的认知以及思考bug(一)

    前言 性能测试是一个全栈工程师/架构师必会的技能之一,只有学会性能测试,才能根据得到的测试报告进行分析,找到系统性能的瓶颈所在,而这也是优化架构设计中重要的依据. 测试流程: 需求分析→环境搭建→测试 ...