C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ String

在C++中,字符串是 std::string 类的对象,代表字符序列。我们可以对字符串执行许多操作,例如连接,比较,转换等。

C++字符串示例

让我们看一下C++字符串的简单示例。
#include <iostream>
using namespace std;
int main( ) {
    string s1 = "Hello";  
        char ch[] = { 'C', '+', '+'};  
        string s2 = string(ch);  
        cout<<s1<<endl;  
        cout<<s2<<endl;  
}
输出:
Hello
C++

C++字符串比较示例

让我们看看使用strcmp()函数进行字符串比较的简单示例。
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
  char key[] = "mango";
  char buffer[50];
  do {
     cout<<"What is my favourite fruit? ";
     cin>>buffer;
  } while (strcmp (key,buffer) != 0);
 cout<<"Answer is correct!!"<<endl;
  return 0;
}
输出:
What is my favourite fruit? apple
What is my favourite fruit? banana
What is my favourite fruit? mango
Answer is correct!!

C++字符串连接示例

让我们看看使用strcat()函数进行字符串连接的简单示例。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char key[25], buffer[25];
    cout << "Enter the key string: ";
    cin.getline(key, 25);
    cout << "Enter the buffer string: ";
     cin.getline(buffer, 25);
    strcat(key, buffer); 
    cout << "Key = " << key << endl;
    cout << "Buffer = " << buffer<<endl;
    return 0;
}
输出:
Enter the key string: Welcome to
Enter the buffer string:  C++ Programming.
Key = Welcome to C++ Programming.
Buffer =  C++ Programming.

C++字符串复制示例

让我们看一下使用strcpy()函数复制字符串的简单示例。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char key[25], buffer[25];
    cout << "Enter the key string: ";
    cin.getline(key, 25);
    strcpy(buffer, key);
    cout << "Key = "<< key << endl;
    cout << "Buffer = "<< buffer<<endl;
    return 0;
}
输出:
Enter the key string: C++ Tutorial
Key = C++ Tutorial
Buffer = C++ Tutorial

C++字符串长度示例

让我们看一下使用strlen()函数查找字符串长度的简单示例。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char ary[] = "Welcome to C++ Programming";
    cout << "Length of String = " << strlen(ary)<<endl;
    return 0;
}
输出:
Length of String = 26

C++字符串函数

功能 说明
int compare(const string&str) 它用于比较两个字符串对象。
int length() 它用于查找字符串的长度。
void swap(string&str) 用于交换两个字符串对象的值。
string substr(int pos,int n) 它将创建一个新的n个字符的字符串对象。
int size() 它以字节为单位返回字符串的长度。
void resize(int n) 它用于将字符串的长度调整为最多n个字符。
string&replace(int pos,int len,string&str) 它将替换从字符位置pos开始并跨越len个字符的字符串部分。
string&append(const string&str) 它在另一个字符串对象的末尾添加新字符。
char&at(int pos) 用于访问指定位置pos上的单个字符。
int find(string&str,int pos,int n) 用于查找参数中指定的字符串。
int find_first_of(string&str,int pos,int n) 它用于查找指定序列的第一次出现。
int find_first_not_of(string&str,int pos,int n) 用于在字符串中搜索与字符串中指定的任何字符都不匹配的第一个字符。
int find_last_of(string&str,int pos,int n) 用于在字符串中搜索指定序列的last字符。
int find_last_not_of(string&str,int pos) 它将搜索与指定序列不匹配的last字符。
string&insert() 它将在位置pos所指示的字符之前插入一个新字符。
int max_size() 它找到字符串的最大长度。
void push_back(char ch) 它将在字符串的末尾添加一个新字符ch。
void pop_back() 它将删除字符串的last字符。
string&Assign() 它将新值分配给字符串。
int copy(string&str) 它将字符串的内容复制到另一个。
char&back() 它返回last字符的引用。
Iterator begin() 它返回第一个字符的引用。
int Capacity() 它返回为字符串分配的空间。
const_iterator cbegin() 它指向字符串的第一个元素。
const_iterator cend() 它指向字符串的last元素。
void clear() 它将删除字符串中的所有元素。
const_reverse_iterator crbegin() 它指向字符串的last字符。
const_char * data() 它将字符串的字符复制到数组中。
bool empty() 它检查字符串是否为空。
string&erase() 它将删除指定的字符。
char&front() 它返回第一个字符的引用。
string&operator+=() 它将在字符串的末尾附加一个新字符。
string&operator=() 它为字符串分配一个新值。
char operator[](pos) 它检索指定位置pos处的字符。
int rfind() 它将搜索字符串的最后一次出现。
iterator end() 它引用字符串的last字符。
reverse_iterator rend() 它指向字符串的第一个字符。
void shrink_to_fit() 它减少了容量并使它等于字符串的大小。
char * c_str() 它返回一个指向包含空终止字符序列的数组的指针。
const_reverse_iterator crend() 它引用字符串的第一个字符。
reverse_iterator rbegin() 它引用字符串的last字符。
void reserve(inr len) 它要求更改容量。
allocator_type get_allocator(); 它返回与字符串关联的分配对象。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4