C++中如何使用std::string类?字符串类用法

2021年4月1日15:38:01 发表评论 914 次浏览

C++在其定义中有一种表示方法字符序列作为类的对象。此类称为std::string字符串类将字符存储为字节序列, 并具有允许访问单字节字符.

std::string vs字符数组

  • 字符数组只是一个字符数组可以以空字符终止。字符串是定义对象的类表示为字符流。
  • 字符数组的大小必须静态分配, 如果需要, 则无法在运行时分配更多的内存。未使用分配内存浪费如果是字符数组。如果是字符串, 则内存为动态分配。可以在运行时按需分配更多的内存。由于没有预先分配内存, 没有内存浪费.
  • 有一个威胁阵列衰减如果是字符数组。由于字符串表示为对象, 没有阵列衰减发生。
  • 实施字符数组更快比std::string 。琴弦较慢与字符数组相比实现时。
  • 字符数组不提供许多内置函数操纵弦。字符串类定义多种函数允许对字符串进行流形运算。

字符串操作

输入函数

1. getline():-此函数用于存储字符流由用户在对象存储器中输入。

2. push_back():-此函数用于输入一个角色结束的字符串。

3. pop_back():-从C++ 11引入(用于字符串), 此函数用于删除最后一个字符从字符串。

// C++ code to demonstrate the working of
// getline(), push_back() and pop_back()
#include<iostream>
#include<string> // for string class
using namespace std;
int main()
{
     // Declaring string
     string str;
  
     // Taking string input using getline()
     // "geeksforgeek" in givin output
     getline(cin, str);
  
     // Displaying string
     cout << "The initial string is : " ;
     cout << str << endl;
  
     // Using push_back() to insert a character
     // at end
     // pushes 's' in this case
     str.push_back( 's' );
  
     // Displaying string
     cout << "The string after push_back operation is : " ;
     cout << str << endl;
  
     // Using pop_back() to delete a character
     // from end
     // pops 's' in this case
     str.pop_back();
  
     // Displaying string
     cout << "The string after pop_back operation is : " ;
     cout << str << endl;
  
     return 0;
  
}

输入如下:

geeksforgeek

输出如下:

The initial string is : geeksforgeek
The string after push_back operation is : lsbin
The string after pop_back operation is : geeksforgeek

容量函数

4.容量():-此函数返回容量分配给字符串, 可以是等于或大于尺寸的字符串。分配了额外的空间, 以便在将新字符添加到字符串时, 操作可以高效完成.

5. resize():-此函数改变字符串的大小, 大小可以增加或减少。

6.length():-此函数查找字符串的长度

7.shrink_to_fit():-此函数减少容量字符串的最大容量, 使其等于字符串的最小容量。该操作是有助于节省更多内存如果我们确定不必再添加任何字符。

// C++ code to demonstrate the working of
// capacity(), resize() and shrink_to_fit()
#include<iostream>
#include<string> // for string class
using namespace std;
int main()
{
     // Initializing string
     string str = "lsbin is for geeks" ;
  
     // Displaying string
     cout << "The initial string is : " ;
     cout << str << endl;
  
     // Resizing string using resize()
     str.resize(13);
  
     // Displaying string
     cout << "The string after resize operation is : " ;
     cout << str << endl;
  
     // Displaying capacity of string
     cout << "The capacity of string is : " ;
     cout << str.capacity() << endl;
  
     //Displaying length of the string
     cout<< "The length of the string is :" <<str.length()<<endl;
  
     // Decreasing the capacity of string
     // using shrink_to_fit()
     str.shrink_to_fit();
  
     // Displaying string
     cout << "The new capacity after shrinking is : " ;
     cout << str.capacity() << endl;
  
     return 0;
  
}

输出如下:

The initial string is : lsbin is for geeks
The string after resize operation is : lsbin
The capacity of string is : 26
The length of the string is : 13
The new capacity after shrinking is : 13

迭代器函数

8. begin():-此函数返回一个迭代器to开始的字符串。

9. end():-此函数返回一个迭代器to结束的字符串。

10. rbegin():-此函数返回一个反向迭代器指着结束的字符串。

11. rend():-此函数返回一个反向迭代器指着开始的字符串。

// C++ code to demonstrate the working of
// begin(), end(), rbegin(), rend()
#include<iostream>
#include<string> // for string class
using namespace std;
int main()
{
     // Initializing string`
     string str = "lsbin" ;
  
     // Declaring iterator
     std::string::iterator it;
  
     // Declaring reverse iterator
     std::string::reverse_iterator it1;
  
     // Displaying string
     cout << "The string using forward iterators is : " ;
     for (it=str.begin(); it!=str.end(); it++)
     cout << *it;
     cout << endl;
  
     // Displaying reverse string
     cout << "The reverse string using reverse iterators is : " ;
     for (it1=str.rbegin(); it1!=str.rend(); it1++)
     cout << *it1;
     cout << endl;
  
     return 0;
  
}

输出如下:

The string using forward iterators is : lsbin
The reverse string using reverse iterators is : skeegrofskeeg

操作函数

12.复制("字符数组", len, pos):-此函数复制目标字符数组中的子字符串在其论点中提到。它需要3个参数, 目标char数组, 要复制的长度以及开始复制的字符串起始位置。

13. swap():-此函数掉期一串串。

// C++ code to demonstrate the working of
// copy() and swap()
#include<iostream>
#include<string> // for string class
using namespace std;
int main()
{
     // Initializing 1st string
     string str1 = "lsbin is for geeks" ;
  
     // Declaring 2nd string
     string str2 = "lsbin rocks" ;
  
     // Declaring character array
     char ch[80];
  
     // using copy() to copy elements into char array
     // copies "lsbin"
     str1.copy(ch, 13, 0);
  
     // Diplaying char array
     cout << "The new copied character array is : " ;
     cout << ch << endl << endl;
  
     // Displaying strings before swapping
     cout << "The 1st string before swapping is : " ;
     cout << str1 << endl;
     cout << "The 2nd string before swapping is : " ;
     cout << str2 << endl;
  
     // using swap() to swap string content
     str1.swap(str2);
  
     // Displaying strings after swapping
     cout << "The 1st string after swapping is : " ;
     cout << str1 << endl;
     cout << "The 2nd string after swapping is : " ;
     cout << str2 << endl;
  
     return 0;
  
}

输出如下:

The new copied character array is : lsbin

The 1st string before swapping is : lsbin is for geeks
The 2nd string before swapping is : lsbin rocks
The 1st string after swapping is : lsbin rocks
The 2nd string after swapping is : lsbin is for geeks

有关更多函数:

C++字符串类及其应用

C++字符串类及其应用程序套装2

如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: