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. Servlet与JSP学习笔记

    一.Servlet 注册 web.xml里边注册Servlet ,定义格式如下: <servlet> <servlet-name>helloworld</servlet- ...

  2. 如何让charles无论怎么请求都返回一个结果

    1. map Local         将匹配的url映射到本地文件.这个需要首先将url右键,save Response,将原有报文保存到本地,然后映射到该文件,修改该文件即可,直接自己写费事2. ...

  3. .net core中使用HttpClient碰到的问题:This instance has already started one or more requests. Properties can only be modified before sending the first request

    项目里使用httpclient一般加staic或者单例来防止每次请求都会新建立一个连接,从而占用太多的服务器资源, 问题产生 但是今天新加的一个方法中每次需要请求不同的url,这时候就出现了错误: 就 ...

  4. jacoco插件添加

    1.添加依赖 <dependency> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-p ...

  5. Git 仓库7K stars!学Java开源项目austin要多久?

    我是3y,一年CRUD经验用十年的markdown程序员‍常年被誉为职业八股文选手 开源项目消息推送平台austin仓库地址: 消息推送平台推送下发[邮件][短信][微信服务号][微信小程序][企业微 ...

  6. Android笔记--FileProvider

    FileProvider介绍 继承于ContentProvider,本质上依旧是用于跨境通信,对第三方应用暴露文件,并授予文件读写地权限 具体内容 1.在Strings.xml里面配置一个常量 2.在 ...

  7. Python学习笔记--第二阶段啦

    初识对象 示例: 类的成员方法 上图中的self必须填写!!! 示例: 类和对象 有c和c++语言基础的话,就会发现其实是一样的道理,只是实现代码有差异 构造方法(init) 示例: 注意: 其他内置 ...

  8. QOE 驱动下的分布式实时网络构建:Agora SD-RTN 的演进

    编者按:近日,全球软件案例研究峰会在北京召开.全球软件案例研究峰会(简称"TOP100Summit")是科技界一年一度的案例研究榜单,每年甄选年度最值得借鉴的100个好案例,旨在揭 ...

  9. LeetcodePractice-数组

    目录 88. 合并两个有序数组 思路解析 字节&1. 两数之和 思路解析 腾讯&15. 三数之和 思路解析 18. 四数之和 思路解析 解释下,tmpSum为什么用int不行,需要用i ...

  10. IntelliJ IDEA 下载安装及配置使用教程(图文步骤详解)

    前言 壹哥在前面的文章中,带大家下载.安装.配置了Eclipse这个更好用的IDE开发工具,并教会了大家如何在Eclipse中进行项目的创建和代码编写.运行.但是实际上,在各种IDE开发工具中,Ecl ...