C++字符串用法

本文概述

  • C ++字符串示例
  • C ++字符串比较示例
  • C ++字符串Concat示例
  • C ++字符串复制示例
  • C ++字符串长度示例
  • C ++字符串函数
【C++字符串用法】在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 ++字符串Concat示例让我们看一下使用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() 它用于查找字符串的长度。
无效交换(字符串和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个字符的字符串部分。
字符串和附加(const string&str) 它将在另一个字符串对象的末尾添加新字符。
char&at(int pos) 它用于访问指定位置pos处的单个字符。
int find(字符串&str, int pos, int n) 用于查找参数中指定的字符串。
int find_first_of(字符串&str, int pos, int n) 它用于查找指定序列的首次出现。
int find_first_not_of(string&str, int pos, int n) 它用于在字符串中搜索与该字符串中指定的任何字符都不匹配的第一个字符。
int find_last_of(字符串&str, int pos, int n) 用于在字符串中搜索指定序列的最后一个字符。
int find_last_not_of(string&str, int pos) 它搜索与指定序列不匹配的最后一个字符。
字符串和insert() 它将在位置pos指示的字符之前插入一个新字符。
int max_size() 它找到字符串的最大长度。
void push_back(char ch) 它在字符串的末尾添加了一个新字符ch。
void pop_back() 它删除字符串的最后一个字符。
字符串和assign() 它将新值分配给字符串。
int复制(string&str) 它将字符串的内容复制到另一个。
char&back() 它返回最后一个字符的引用。
Iterator begin() 它返回第一个字符的参考。
int capacity() 它返回为字符串分配的空间。
const_iterator cbegin() 它指向字符串的第一个元素。
const_iterator cend() 它指向字符串的最后一个元素。
void clear() 它从字符串中删除所有元素。
const_reverse_iterator crbegin() 它指向字符串的最后一个字符。
const_char *数据() 它将字符串的字符复制到数组中。
bool empty() 它检查字符串是否为空。
字符串和擦除() 它删除指定的字符。
char& front() 它返回第一个字符的引用。
字符串&运算符+ =() 它在字符串的末尾附加一个新字符。
string& operator=() 它为字符串分配一个新值。
char operator[](pos) 它在指定位置pos检索一个字符。
int rfind() 它搜索字符串的最后一次出现。
iterator end() 它引用字符串的最后一个字符。
reverse_iterator rend() 它指向字符串的第一个字符。
void shrink_to_fit() 它减少了容量并使它等于字符串的大小。
char* c_str() 它返回指向包含空终止字符序列的数组的指针。
const_reverse_iterator crend() 它引用字符串的第一个字符。
reverse_iterator rbegin() 它引用字符串的最后一个字符。
void reserve(inr len) 它要求更改容量。
allocator_type get_allocator(); 它返回与字符串关联的已分配对象。

    推荐阅读