📌  相关文章
📜  std :: C++中的字符串类

📅  最后修改于: 2021-05-30 08:15:16             🧑  作者: Mango

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

std ::字符串vs字符数组

  • 字符数组只是字符数组,可以以空字符结尾。字符串是一个类,用于定义表示为字符流的对象

  • 字符数组的大小必须静态分配,如果需要,则无法在运行时分配更多内存。如果使用字符数组,则会浪费未使用的分配内存。如果是字符串,则动态分配内存。可以在运行时按需分配更多内存。由于没有预先分配内存,因此不会浪费任何内存

  • 如果是字符数组,则存在数组衰减威胁。由于字符串表示为对象,因此不会发生数组衰减

  • 字符数组的实现比std :: 字符串更快。与实现相比,字符串比字符数组要慢

  • 字符数组没有提供太多内置函数来操作字符串。字符串类定义了许多功能,这些功能允许对字符串进行多种操作。

字符串操作

输入功能
1. getline() :-此函数用于将用户输入的字符流存储在对象存储器中。
2. push_back() :-此函数用于在字符串末尾输入字符。
3. pop_back() :-从C++ 11(用于字符串)引入,此函数用于删除字符串的最后一个字符

CPP
// C++ code to demonstrate the working of
// getline(), push_back() and pop_back()
#include
#include // for string class
using namespace std;
int main()
{
    // Declaring string
    string str;
 
    // Taking string input using getline()
    // "geeksforgeek" in giving 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;
 
}


CPP
// C++ code to demonstrate the working of
// capacity(), resize() and shrink_to_fit()
#include
#include // for string class
using namespace std;
int main()
{
    // Initializing string
    string str = "geeksforgeeks 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 :"<


CPP
// C++ code to demonstrate the working of
// begin(), end(), rbegin(), rend()
#include
#include // for string class
using namespace std;
int main()
{
    // Initializing string`
    string str = "geeksforgeeks";
 
    // 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;
 
}


CPP
// C++ code to demonstrate the working of
// copy() and swap()
#include
#include // for string class
using namespace std;
int main()
{
    // Initializing 1st string
    string str1 = "geeksforgeeks is for geeks";
 
    // Declaring 2nd string
    string str2 = "geeksforgeeks rocks";
 
    // Declaring character array
    char ch[80];
 
    // using copy() to copy elements into char array
    // copies "geeksforgeeks"
    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;
 
}


输入:

geeksforgeek

输出:

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

容量功能
4. Capacity() :-此函数返回分配给字符串的容量,该容量可以等于或大于字符串的大小。分配了额外的空间,以便在将新字符添加到字符串,可以高效地完成操作
5. resize() :-此函数更改字符串的大小,大小可以增加或减少。
6.length() :-此函数查找字符串的长度
7.shrink_to_fit(): -该函数减小了字符串的容量,并使其等于该字符串的最小容量。如果我们确定不需要进一步添加字符,则此操作对于节省额外的内存很有用。

CPP

// C++ code to demonstrate the working of
// capacity(), resize() and shrink_to_fit()
#include
#include // for string class
using namespace std;
int main()
{
    // Initializing string
    string str = "geeksforgeeks 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 :"<

输出:

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

迭代器功能
8. begin() :-此函数将迭代器返回到字符串的开头
9.端(): -该函数返回一个迭代到该字符串的结束
10. rbegin() :-此函数返回指向字符串末尾反向迭代器
11. rend() :-此函数返回指向字符串开头反向迭代器

CPP

// C++ code to demonstrate the working of
// begin(), end(), rbegin(), rend()
#include
#include // for string class
using namespace std;
int main()
{
    // Initializing string`
    string str = "geeksforgeeks";
 
    // 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 : geeksforgeeks
The reverse string using reverse iterators is : skeegrofskeeg

操作功能
12. copy(“ char array”,len,pos) :-此函数将子字符串复制到其参数中提到的目标字符数组中。它需要3个参数,目标char数组,要复制的长度以及字符串中的开始位置才能开始复制。
13. swap() :-此函数一个字符串与另一个字符串交换

CPP

// C++ code to demonstrate the working of
// copy() and swap()
#include
#include // for string class
using namespace std;
int main()
{
    // Initializing 1st string
    string str1 = "geeksforgeeks is for geeks";
 
    // Declaring 2nd string
    string str2 = "geeksforgeeks rocks";
 
    // Declaring character array
    char ch[80];
 
    // using copy() to copy elements into char array
    // copies "geeksforgeeks"
    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 : geeksforgeeks

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

有关更多功能:
C++字符串类及其应用
C++字符串类及其应用程序套装2

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”