0
点赞
收藏
分享

微信扫一扫

【C++grammar】string类和array类

东言肆语 2022-06-24 阅读 56


目录

  • ​​1、C++11的string类​​
  • ​​1、创建 string 对象​​
  • ​​2、追加字符串append函数​​
  • ​​3、为字符串赋值assign函数​​
  • ​​4、at, clear, erase, and empty函数​​
  • ​​5、比较字符串compare()​​
  • ​​6、获取子串at() 、substr()函数​​
  • ​​7、搜索字符串find()​​
  • ​​8、插入和替换字符串insert() 、replace()​​
  • ​​9、字符串运算符​​
  • ​​10、string_view与string的几点差别​​
  • ​​2、C++11的数组类​​
  • ​​1. C-Style Array v.s. C++ Style Array (C风格数组和C++风格数组)​​
  • ​​1、C Style Array (C++ raw array,也叫做C++原生数组)​​
  • ​​2、C Style Array (C++ raw array,也叫做C++原生数组)​​
  • ​​2. Create C++ Style Array (创建C++风格数组)​​
  • ​​3.std::array的成员函数​​

C++ 使用 string 类处理字符串

string类中的函数

(1) 构造

(2) 追加

(3) 赋值

(4) 位置与清除

(5) 长度与容量

(6) 比较

(7) 子 串

(8) 搜索

(9) 运算符

1、C++11的string类

1、创建 string 对象

由一个字符串常量或字符串数组创建string对象

string message{ "Aloha World!" };
char charArray[] = {'H', 'e', 'l', 'l', 'o', '\0'};
string message1{ charArray };

2、追加字符串append函数

一系列的重载函数可以将新内容附加到一个字符串中

string s1{ "Welcome" };
s1.append( " to C++" ); // appends " to C++" to s1
cout << s1 << endl; // s1 now becomes Welcome to C++

string s2{ "Welcome" };
s2.append( " to C and C++", 3, 2 ); // appends " C" to s2
cout << s2 << endl; // s2 now becomes Welcome C

string s3{ "Welcome" };
s3.append( " to C and C++", 5); // appends " to C" to s3
cout << s3 << endl; // s3 now becomes Welcome to C

string s4{ "Welcome" };
s4.append( 4, 'G' ); // appends "GGGG" to s4
cout << s4 << endl; // s4 now becomes WelcomeGGGG

3、为字符串赋值assign函数

一系列的重载函数可以将一个字符串赋以新内容

string s1{ "Welcome" };
s1.assign( "Dallas" ); // assigns "Dallas" to s1
cout << s1 << endl; // s1 now becomes Dallas

string s2{ "Welcome" };
s2.assign( "Dallas, Texas", 1, 3 ); // assigns "all" to s2
cout << s2 << endl; // s2 now becomes all

string s3{ "Welcome" };
s3.assign( "Dallas, Texas", 6 ); // assigns "Dallas" to s3
cout << s3 << endl; // s3 now becomes Dallas

string s4{ "Welcome" };
s4.assign( 4, 'G' ); // assigns "GGGG" to s4
cout << s4 << endl; // s4 now becomes GGGG

4、at, clear, erase, and empty函数

(1) at(index): 返回当前字符串中index位置的字符
(2) clear(): 清空字符串
(3) erase(index, n): 删除字符串从index开始的n个字符
(4) empty(): 检测字符串是否为空

string s1{ "Welcome" };

cout << s1.at(3) << endl; // s1.at(3) returns c

cout << s1.erase(2, 3) << endl; // s1 is now Weme

s1.clear(); // s1 is now empty

cout << s1.empty() << endl; // s1.empty returns 1 (means true)

5、比较字符串compare()

compare() 函数用于比较两个字符串。它与C语言中的 strcmp() 函数很像。

string s1{ "Welcome" };
string s2{ "Welcomg" };
cout << s1.compare(s2) << endl; // returns -2
cout << s2.compare(s1) << endl; // returns 2
cout << s1.compare("Welcome") << endl; // returns 0

6、获取子串at() 、substr()函数

string s1{ "Welcome" };
cout << s1.substr(0, 1) << endl; // returns W; 从0号位置开始的1个字符
cout << s1.substr(3) << endl; // returns come; 从3号位置直到末尾的子串
cout << s1.substr(3, 3) << endl; // returns com;从3号位置开始的3个字符

7、搜索字符串find()

find() 函数可以在一个字符串中搜索一个子串或者一个字符

string s1{ "Welcome to C++" };
cout << s1.find("co") << endl; // returns 3; 返回子串出现的第一个位置
cout << s1.find("co", 6) << endl; // returns -1 从6号位置开始查找子串出现的第一个位置
cout << s1.find('o') << endl; // returns 4 返回字符出现的第一个位置
cout << s1.find('o', 6) << endl; // returns 9 从6号位置开始查找字符出现的第一个位置

8、插入和替换字符串insert() 、replace()

string s1("Welcome to C++");
s1.insert(11, "Java and ");
cout << s1 << endl; // s1 becomes Welcome to Java and C++

string s2{ "AA" };
s2.insert(1, 4, 'B'); //在1号位置处连续插入4个相同字符
cout << s2 << endl; // s2 becomes to ABBBBA

string s3{ "Welcome to Java" };
s3.replace(11, 4, "C++"); //从11号位置开始向后的4个字符替换掉。注意'\0'
cout << s3 << endl; // returns Welcome to C++

9、字符串运算符

【C++grammar】string类和array类_字符串

string s1 = "ABC"; // The = operator

string s2 = s1; // The = operator

for (int i = s2.size() - 1; i >= 0; i--)

cout << s2[i]; // The [] operator



string s3 = s1 + "DEFG"; // The + operator

cout << s3 << endl; // s3 becomes ABCDEFG



s1 += "ABC";

cout << s1 << endl; // s1 becomes ABCABC



s1 = "ABC";

s2 = "ABE";

cout << (s1 == s2) << endl; // Displays 0

cout << (s1 != s2) << endl; // Displays 1

cout << (s1 > s2) << endl; // Displays 0

cout << (s1 >= s2) << endl; // Displays 0

cout << (s1 < s2) << endl; // Displays 1

cout << (s1 <= s2) << endl; // Displays 1

10、string_view与string的几点差别

C++中的string类与Java中的String类不同。为了有一个和Java中的String类特性类似的东西,C++17中提供了string_view类。请你查查资料,说说string_view与string的几点差别。
C++的string对象,如果大于默认的字符串长度阀值。对于长度为N的字符串,时间成本为O(n),空间成本是2xS(n);

于是C++17就有了string_view这个标准库的扩展,这个扩展极大地解决了string拷贝的空间成本和时间成本问题。

string_view基本没有涉及内存的额外分配。

string_view 是C++17所提供的用于处理只读字符串的轻量对象。这里后缀 view 的意思是只读的视图。

通过调用 string_view 构造器可将字符串转换为 string_view 对象。string 可隐式转换为 string_view。

string_view 是只读的轻量对象,它对所指向的字符串没有所有权。

string_view通常用于函数参数类型,可用来取代 const char* 和 const string&。string_view 代替 const string&,可以避免不必要的内存分配。

string_view的成员函数即对外接口与 string 相类似,但只包含读取字符串内容的部分。string_view::substr()的返回值类型是string_view,不产生新的字符串,不会进行内存分配。string::substr()的返回值类型是string,产生新的字符串,会进行内存分配。

string_view字面量的后缀是 sv。(string字面量的后缀是 s)

string_view的适用场合
(由于string_view对象无法被使用它的函数修改,因此要更新string_view所引用的字符串副本,还是需要修改它所引用的string类型的内部字符串副本。)



字符串查找
遍历字符串
显示字符串


2、C++11的数组类

1. C-Style Array v.s. C++ Style Array (C风格数组和C++风格数组)

1、C Style Array (C++ raw array,也叫做C++原生数组)

特点:

int arr[ ] = { 1, 2, 3 };
arr 可能会退化为指针:void f(int a[]) { std::cout << sizeof(a)/sizeof(a[0]); }
arr 不知道自己的大小: sizeof(arr)/sizeof(arr[0])
两个数组之间无法直接赋值: array1 = array2;
不能自动推导类型:auto a1[] = {1,2,3};

2、C Style Array (C++ raw array,也叫做C++原生数组)

特点;

是一个容器类,所以有迭代器(可以认为是一种用于访问成员的高级指针)
可直接赋值
知道自己大小:size()
能和另一个数组交换内容:swap()
能以指定值填充自己: fill()
取某个位置的元素( 做越界检查) :at()

2. Create C++ Style Array (创建C++风格数组)

#include
std::array< 数组 类型, 数组大小> 数组名字;
std::array< 数组 类型, 数组大小> 数组 名字 { 值1, 值2, …};

限制与C风格数组相同 std::array<int , 10> x;
std::array<char , 5> c{ ‘H’,‘e’,‘l’,‘l’,‘o’ };

3.std::array的成员函数

【C++grammar】string类和array类_c++_02
【C++grammar】string类和array类_数组_03


举报

相关推荐

0 条评论