C++学习 --string

这篇具有很好参考价值的文章主要介绍了C++学习 --string。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

1, 什么是string

2, 创建string

2-1,普通创建 

2-2, 拷贝构造创建

2-2-1, 整体拷贝

2-2-2, 部分拷贝

2-3, 指定长度创建

3, 赋值(assign)

3-1, 拷贝构造赋值

3-1-1, 整体赋值

3-1-2, 部分赋值

3-2, 指定长度赋值

4, 拼接

4-1, 拼接(+)

4-2, 拼接(append)

4-2-1, 整体拼接

4-2-2, 部分拼接

5, 替换(replace)

6, 插入(insert)

7, 查找(find)

8, 子串(substr)

9, 比较(compare)

10, 空检查(empty)

11, 清空

11-1, 清空(clear)

11-2, 清空(erase)

12, 获取长度

12-1, 长度(length)

12-2, 长度(size)

13, 交换(swap)

14, 单字符

14-1, 单字符([])

14-2, 单字符(at)

14-3, 单字符(front)

14-4, 单字符(back)

14-5, 单字符(pop_back)

14-6, 单字符(push_back)

15, 格式化输出


1, 什么是string

C++中的字符串, 本质是一个类

2, 创建string

2-1,普通创建 

通过string 对象名, 创建string

//创建并赋值
string str1 = "bbb";

2-2, 拷贝构造创建

2-2-1, 整体拷贝

通过string 对象名(字符串对象), 创建string

string str1 = "bbb";
//创建str2并赋值str1
string str2(str1);
//创建str3并赋值bbb
string str3("bbb");

2-2-2, 部分拷贝

通过string 对象名(字符串对象, 起始索引, 字符数), 创建string, 此时拷贝部分字符赋值给对象名

string str1 = "abcdefg";
//将拷贝cde赋值给str2
string str2(str1, 2, 3);
//将拷贝cde赋值给str3
string str3("abcdefg", 2, 3);

注意:若没有给字符数, 参数为字符串变量和常量字符串时, 结果不同

string str1 = "abcdefg";
//将拷贝cdefg赋值给str2
string str2(str1, 2);
//将拷贝ab赋值给str3
string str3("abcdefg", 2);

2-3, 指定长度创建

通过string 对象名(长度, 单个字符), 创建string

//定义5个a组成的字符串
string str3(5, 'a');

3, 赋值(assign)

3-1, 拷贝构造赋值

3-1-1, 整体赋值

 通过对象名.assign(字符串), 进行string对象赋值

string str1 = "abcdefg";
string str2;
string str3;
//将str1赋值给str2
str2.assign(str1);
//将abcdefg赋值给str3
str3.assign("abcdefg");

3-1-2, 部分赋值

通过对象名.assign(字符串, 起始索引, 字符数), 进行string对象赋值

string str1 = "abcdefg";
string str2;
string str3;
//将cde赋值给str2
str2.assign(str1, 2, 3);
//将cde赋值给str3
str3.assign("abcdefg", 2, 3);

注意:若没有给字符数, 参数为字符串变量和常量字符串时, 结果不同

string str1 = "abcdefg";
string str2;
string str3;
//将cdefg赋值给str2
str2.assign(str1, 2);
//将ab赋值给str3
str3.assign("abcdefg", 2);

3-2, 指定长度赋值

string str1;
//str1由5个a组成
str1.assign(5, 'a');

4, 拼接

可通过符号"+"append方法, 可实现字符串拼接

4-1, 拼接(+)

通过对象名1+对象名2, 拼接两个字符串

string str1 = "abc";
string str2 = "def";

//输出abcdef
cout << str1 + str2 << endl;
//输出abcdef
cout << str1 + "def" << endl;

注意:不支持两个字符常量向加

//编译器会报错
cout << "abc" + "def" << endl;

4-2, 拼接(append)

4-2-1, 整体拼接

通过对象名1.append(对象名2)拼接, 注意,这里的对象名1不能为常量字符串

string str1 = "abc";
string str2 = "def";
string str3 = "abc";

//输出abcdef
str1.append(str2);
//输出abcdef
str3.append("def");

4-2-2, 部分拼接

通过对象名1.append(对象名2, 起始索引, 字符数), 指定字符数拼接, 注意,这里的对象名1不能为常量字符串

string str1 = "abc";
string str2 = "def";
string str3 = "abc";

//输出abcef
str1.append(str2, 1, 2);
//输出abcef
str3.append("def", 1, 2);

注意:若没有给字符数, 参数为字符串变量和常量字符串时, 结果不同

string str1 = "abc";
string str2 = "def";
string str3 = "abc";

//输出abcef
str1.append(str2, 1);
//输出abcd
str3.append("def", 1);

5, 替换(replace)

通过对象名.replace(起始索引, 结束索引, 替换值), 可替换字符串内容, 其中的替换值长度没有限制

string str1 = "abcdef";
string str2 = "abcdef";
string str3 = "abcdef";

//str1变为a111ef
cout << str1.replace(1, 3, "111") << endl;
//str1变为a1ef
cout << str2.replace(1, 3, "1") << endl;
//str1变为a1111ef
cout << str3.replace(1, 3, "1111") << endl;

6, 插入(insert)

通过对象名.insert(索引,  值), 向字符串中插入数据

string str1 = "abc";
string str2 = "abc";

//str1变为aabcbc
str1.insert(1, "abc");
//str2变为aabcbc
str2.insert(1, str2);

7, 查找(find)

通过对象名.find(), 可查找字符串中的子串, 从开始查找, 返回首次匹配索引, 未找到返回一个很大的整数

string str1 = "abccba";
//返回索引2
cout << str1.find("c") << endl;
//没有找到匹配的,返回18446744073709551615
cout << str1.find("d") << endl;
	

通过对象名.find(), 可查找字符串中的子串, 从开始查找, 返回首次匹配索引, 未找到返回一个很大的整数

string str1 = "abccba";
//返回索引3
cout << str1.rfind("c") << endl;
//没有找到匹配的,返回18446744073709551615
cout << str1.rfind("d") << endl;

8, 子串(substr)

通过对象名.substr(起始索引, 结束索引), 返回起始索引~结束索引的字符

string str1 = "abccba";

//返回bcc
cout << str1.substr(1, 3) << endl;

9, 比较(compare)

通过对象名1.compare(对象名2), 进行比较字符串大小逐个比较两个字符串的Ascii码, 相等返回0, 对象名1大于对象名2返回1, 对象名1小于对象名2返回-1 

string str1 = "hello c++";
string str2 = "1hello c++";
//返回1
cout << str1.compare(str2) << endl;

10, 空检查(empty)

通过对象名.empty(), 检查一个字符串是否为空, 为空返回1, 不为空返回0

string str1 = "";
string str2;
//返回1
cout << str1.empty() << endl;
//返回1
cout << str2.empty() << endl;

11, 清空

11-1, 清空(clear)

通过对象名.clear(), 可清空一个字符串

string str1 = "abc";
//清空str1
str1.clear();
//返回1
cout << str1.empty() << endl;
//返回0
cout << str1.length() << endl;

11-2, 清空(erase)

通过对象名.erase(), 可清空一个字符串

string str1 = "abc";
//清空str1
str1.erase();
//返回1
cout << str1.empty() << endl;
//返回0
cout << str1.length() << endl;

12, 获取长度

12-1, 长度(length)

通过对象名.length(), 返回字符串长度

string str1 = "abc";
//返回3
cout << str1.length() << endl;

12-2, 长度(size)

通过对象名.size(), 返回字符串长度

string str1 = "abc";
//返回3
cout << str1.size() << endl;

13, 交换(swap)

通过对象名1.swap(对象名2), 交换两个字符串的值

string str1 = "abc";
string str2 = "def";
str1.swap(str2);
//str1为def
cout << str1 << endl;
//str2为abc
cout << str2 << endl;

14, 单字符

14-1, 单字符([])

通过对象名[索引], 获取单个字符

string str1 = "abc";
//返回b
cout << str1[1] << endl;
//修改
str1[1] = '1';
//a1c
cout << str1 << endl; 

14-2, 单字符(at)

通过对象名.at(索引), 获取单个字符

string str1 = "abc";
string str2 = "def";
//返回b
cout << str1.at(1) << endl;
//修改
str1.at(1) = '1';
//a1c
cout << str1 << endl; 

14-3, 单字符(front)

通过对象名.front(), 获取首字符

string str1 = "abc";
//返回a
cout << str1.front() << endl;

14-4, 单字符(back)

通过对象名.back(), 获取尾字符

string str1 = "abc";
//返回c
cout << str1.back() << endl;

14-5, 单字符(pop_back)

通过对象名.pop_back(), 删除尾元素

string str1 = "abc";
//删除尾元素
str1.pop_back();
//返回ab
cout << str1 << endl;

14-6, 单字符(push_back)

通过对象名.push_back(字符), 在尾部插入单个字符

string str1 = "abc";
//在尾部插入元素d
str1.push_back('d');
//返回abcd
cout << str1 << endl;

15, 格式化输出

通过头文件#include <iomanip>, 可制定格式化输出文章来源地址https://www.toymoban.com/news/detail-758293.html

//setw(5):设置输出位宽为5位
//setfill('0'):多余为填充0
//right:右对齐
//注意:规则是从setw开始,所以前面的a_原样输出
for (int i = 0; i < 15; i++)
{
	cout << "a_" << setw(5) << setfill('0') << right << to_string(i) << endl;
}
--------------------------------------------------------
输出为
a_00000
a_00001
a_00002
a_00003
a_00004
a_00005
a_00006
a_00007
a_00008
a_00009
a_00010
a_00011
a_00012
a_00013
a_00014

到了这里,关于C++学习 --string的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【C++初阶】学习string类的模拟实现

    前面已经学习了string类的用法,这篇文章将更深入的学习string类,了解string类的底层是怎么实现的。当然,这里只是模拟一些常用的,不常用的可以看文档学习。 我们一共创建两个文件,一个是test.cpp文件,用于测试;另一个是string.h文件,用于声明和定义要模拟的string类。

    2024年02月03日
    浏览(40)
  • c++学习:STL之string类初识

    目录 1.关于STL 1.什么是STL 2.STL的六的组件 2.关于string类的学习 1.为何学习string类 2.何为string类 3.string类对象的构造 4.容量操作  5.元素访问的操作 6.迭代器(Iterators) 7.修改类的操作  8.字符串操作 STL(standard template library-标准模板库):是c++标准库的重要组成部分,不仅是一

    2024年02月03日
    浏览(29)
  • 【C++初阶】STL之学习string的用法

    STL是C++的标准模板库 ,里面包含了许多 算法和数据结构 ,例如我们熟悉的顺序表、链表、栈和队列以及一些常见的算法等等,编程者想使用这些就可以直接从库中调用,不必再自己造轮子了。 下面为STL内容的一张图: 接下来,我们要学习STL中的string。 string 是C++的一个类模

    2024年02月04日
    浏览(32)
  • 【C++】day4学习成果:仿写string类等等

    1.仿照string类,完成myString 类 代码: 运行结果: 2.思维导图

    2024年02月09日
    浏览(31)
  • 第一百一十六天学习记录:C++提高:STL-string(黑马教学视频)

    string是C++风格的字符串,而string本质上是一个类 string和char 区别 1、char 是一个指针 2、string是一个类,类内部封装了char*,管理这个字符串,是一个char 型的容器。 特点: string类内部封装了很多成员方法 例如:查找find,拷贝copy,删除delete替换replace,插入insert string管理char

    2024年02月15日
    浏览(37)
  • 【C++】手撕string(string的模拟实现)

    手撕string目录: 一、 Member functions 1.1 constructor 1.2  Copy constructor(代码重构:传统写法和现代写法) 1.3 operator=(代码重构:现代写法超级牛逼) 1.4 destructor 二、Other member functions 2.1 Iterators(在string类中,迭代器基本上就是指针) 2.1.1 begin() end() 2.1.2  范围for的底层

    2024年02月08日
    浏览(35)
  • c++命名空间和include C++ #include<string> 和 using std::string

    1、C++中的命名空间namespace_51CTO博客_c++中的命名空间   2、 C++ #includestring 和 using std::string_yang20141109的博客-CSDN博客 //不光要加头文件,和C语言不同 #include utils/Errors.h //还要加using using android::status_t; using android::INVALID_OPERATION; using android::NO_ERROR; using android::BAD_VALUE; 3、c和c++的差

    2023年04月20日
    浏览(38)
  • c++命名空间和include C++ #include<string> 和 using std::string

    1、C++中的命名空间namespace_51CTO博客_c++中的命名空间   2、 C++ #includestring 和 using std::string_yang20141109的博客-CSDN博客 //不光要加头文件,和C语言不同 #include utils/Errors.h //还要加using using android::status_t; using android::INVALID_OPERATION; using android::NO_ERROR; using android::BAD_VALUE; 3、c和c++的差

    2023年04月20日
    浏览(39)
  • C++(13)——string

    上篇文章中介绍了中部分函数的用法,本篇文章将继续对其他的函数进行介绍:   函数的两个参数如上述代码所示,此函数的主要作用是根据一个已有的的对象的起始坐标开始,以长度为范围内的内容生成一个新的类型的对象。主要用于对一个已有的类型的对象进行分隔。例

    2024年01月20日
    浏览(20)
  • 【C++】模拟实现string

      目录 🌞专栏导读 🌛定义string类  🌛构造函数 🌛拷贝构造函数 🌛赋值函数 🌛析构函数  🌛[]操作符重载  🌛c_str、size、capacity函数  🌛比较运算符重载   🌛resize与reserve函数 🌛push_back、append函数  🌛insert函数  🌛erase函数 🌛find函数  🌛swap函数 🌛clean函数  🌛

    2024年02月14日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包