c++:string类

对于初学者,首先需要注意的一点是: 使用string类必须包含#include<string>头文件以及using namespace std

1.string类

1.auto和范围for

auto关键字

直接给出对auto的总结:

  • c++11之后,auto作为一个新的类型指示符来指示编译器,auto声明的变量必须由编译器在编译时期推导而得
  • 用auto声明指针类型时,用auto和auto*没有任何区别,但用auto声明引用类型时则必须加&
  • 当在同一行声明多个变量时,这些变量必须是相同的类型,否则编译器将会报错,因为编译器实际 只对第一个类型进行推导,然后用推导出来的类型定义其他变量
  • auto不能作为函数的参数,可以做返回值,但是建议谨慎使用
  • auto不能直接用来声明数组

#include<iostream>

using namespace std;

int func1()

{

return 10;

}

// 编译报错,auto不能做参数

void func2(auto a)

{

;

}

// 可以做返回值,但是建议谨慎使用

auto func3()

{

return 3;

}

int main()

{

int a = 10;

auto b = a;

auto c = 'a';

auto d = func1();

// 编译报错:rror C3531: "e": 类型包含"auto"的符号必须具有初始值设定项

auto e;

cout << typeid(b).name() << endl;

cout << typeid(c).name() << endl;

cout << typeid(d).name() << endl;

int x = 10;

auto y = &x;

auto* z = &x;

auto& m = x;

cout << typeid(x).name() << endl;

cout << typeid(y).name() << endl;

cout << typeid(z).name() << endl;

auto aa = 1, bb = 2;

// 编译报错:error C3538: 在声明符列表中,"auto"必须始终推导为同一类型

auto cc = 3, dd = 4.0;

// 编译报错:error C3318: "auto []": 数组不能具有其中包含"auto"的元素类型

auto array[] = { 4, 5, 6 };

return 0;

}

以上是关于auto使用的方法和注意事项,但是并没有展现出auto的优势

使用auto具有优势的场景之一如下:

#include<iostream>

#include <string>

#include <map>

using namespace std;

int main()

{

std::map<std::string, std::string> dict = { { "apple", "苹果" },{ "orange",

"橙子" }, {"pear","梨"} };

// auto的用武之地

//std::map<std::string, std::string>::iterator it = dict.begin(); //迭代器

auto it = dict.begin(); //这一行与上一行的意思相同

while (it != dict.end())

{

cout << it->first << ":" << it->second << endl;

++it;

}

return 0;

}

对于程序员来说写一个一个类型很长的变量,不仅需要记住类型名,还费时间,而使用auto时,系统会自动推导它的类型,实现同样的作用,这种场景使用auto具有很大的优势

范围for

对范围for的总结:

  • 对于一个有范围的集合而言,由程序员来说明循环的范围是多余的,有时候还会容易犯错误。因此 C++11中引入了基于范围的for循环。for循环后的括号由冒号" :"分为两部分:第一部分是范围 内用于迭代的变量,第二部分则表示被迭代的范围,自动迭代,自动取数据,自动判断结束
  • 范围for可以作用到数组和容器对象上进行遍历
  • 范围for的底层很简单,容器遍历实际就是替换为迭代器

#include<iostream>

#include <string>

#include <map>

using namespace std;

int main()

{

int array[] = { 1, 2, 3, 4, 5 };

// C++98的遍历

for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)

{

array[i] *= 2;

}

for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)

{

cout << array[i] << endl;

}

// C++11的遍历

for (auto& e : array) //auto使用的语法

e *= 2;

for (auto e : array)

cout << e << " " << endl;

string str("hello world");

for (auto ch : str)

{

cout << ch << " ";

}

cout << endl;

return 0;

}

这种场景,使用范围for可以方便地遍历这个数组

2.string的常用接口

string的接口有很多并且很多接口的作用很相似,记住一些常用的接口就行了

string类对象的常见构造

|----------------------------------|---------------------------|
| (constructor)函数名称 | 功能说明 |
| string() (重点) | 构造空的string类对象,即空字符串 |
| string(const char* s) (重点) | 用C-string来构造string类对象 |
| string(size_t n, char c) | string类对象中包含n个字符c |
| string(const string&s) (重点) | 拷贝构造函数 |

void Teststring()

{

string s1; // 构造空的string类对象s1

string s2("hello bit"); // 用C格式字符串构造string类对象s2

string s3(s2); // 拷贝构造s3

}

string类对象的容量操作

|-----------------|----------------------------------|
| 函数名称 | 功能说明 |
| size(重点) | 返回字符串有效字符长度 |
| length | 返回字符串有效字符长度 |
| capacity | 返回空间总大小 |
| empty(重点) | 检测字符串释放为空串,是返回true,否则返回false |
| clear(重点) | 清空有效字符 |
| reserve(重点) | 为字符串预留空间 |
| resize(重点) | 将有效字符的个数该成n个,多出的空间用字符c填充 |

注意

  • size()与length()方法底层实现原理完全相同,引入size()的原因是为了与其他容器的接 口保持一致,一般情况下基本都是用size()
  • clear()只是将string中有效字符清空,不改变底层空间大小
  • resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不 同的是当字符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的元素空间。注意:resize在改变元素个数时,如果是将元素个数 增多,可能会改变底层容量的大小,如果是将元素个数减少,底层空间总大小不变
  • reserve(size_t res_arg=0):为string预留空间,不改变有效元素个数,当reserve的参 数小于string的底层空间总大小时,reserver不会改变容量大小

int main()

{

string s1("abcdefg");

cout << s1.size() << endl;

cout << s1.length() << endl;

cout << s1.capacity() << endl; //总空间大小

if (s1.empty())

{

cout << "空串" << endl;

}

else

{

cout << "非空串" << endl;

}

s1.clear(); //清空字符串

if (s1.empty())

{

cout << "空串" << endl;

}

else

{

cout << "非空串" << endl;

}

s1.reserve(30); //预留30个空间

return 0;

}

string类对象的访问及遍历操作

|----------------------|---------------------------------------------|
| 函数名称 | 功能说明 |
| operator[](重点) | 返回pos位置的字符,const string类对象调用 |
| begin + end | begin获取一个字符的迭代器 + end获取最后一个字符下一个位 置的迭代器 |
| rbeing + rend | begin获取一个字符的迭代器 + end获取最后一个字符下一个位 置的迭代器 |
| 范围for | C++11支持更简洁的范围for的新遍历方式 |

string s1("abcdefghi");

s1[3] = 'a';

auto it = s1.begin(); //正向迭代器

while (it < s1.end())

{

cout << *it << " ";

++it;

}

cout << endl;

auto rit = s1.rbegin(); //反向迭代器

while (rit < s1.rend())

{

cout << *rit << " ";

++rit;

}

cout << endl;

for (auto ch : s1)

{

cout << ch << " ";

}

string类非成员函数

|-------------------------|----------------------|
| 函数 | 功能说明 |
| operator+ | 尽量少用,因为传值返回,导致深拷贝效率低 |
| operator>>(重点) | 输入运算符重载 |
| operator<<(重点) | 输出运算符重载 |
| getline(重点) | 获取一行字符串 |
| realation operators(重点) | 大小比较 |

这里只是简单的介绍常用的一些接口的大致功能,如果需要更详细信息,推荐访问cplusplus.com - The C++ Resources Network

相关推荐
JD技术委员会20 分钟前
Rust 语法噪音这么多,是否适合复杂项目?
开发语言·人工智能·rust
tekin22 分钟前
Go、Java、Python、C/C++、PHP、Rust 语言全方位对比分析
java·c++·golang·编程语言对比·python 语言·php 语言·编程适用场景
Hello.Reader23 分钟前
Rust 中的 `Drop` 特性:自动化资源清理的魔法
开发语言·rust·自动化
Vitalia27 分钟前
从零开始学 Rust:基本概念——变量、数据类型、函数、控制流
开发语言·后端·rust
小禾苗_1 小时前
C++ ——继承
开发语言·c++
李长渊哦1 小时前
Java 虚拟机(JVM)方法区详解
java·开发语言·jvm
进击ing小白1 小时前
Qt程序退出相关资源释放问题
开发语言·qt
烂蜻蜓2 小时前
前端已死?什么是前端
开发语言·前端·javascript·vue.js·uni-app
老猿讲编程2 小时前
安全C语言编码规范概述
c语言·开发语言·安全
OrangeJiuce2 小时前
【QT中的一些高级数据结构,持续更新中...】
数据结构·c++·qt