Summary of usage of string objects in C++
C Compared with the C language, C has greatly enhanced support for strings. In addition to using C-style strings, you can also use the built-in data type string. The string class is particularly convenient for processing strings because of the function encapsulation. , let’s count the functions of the string class below
First of all, if you want to call the string class, include its header file first#include <string>
string s1;//变量s1只是定义但没有初始化,所以其默认值为""即空字符串string s2="Hello World!";//变量s2在定义时同时被初始化/*string类的变量可以相互之间直接赋值,不需要像C语言一样,使用strcpy()函数一个字符一个字符的去赋值*///例如string s3=s2;//此时s3的内容和s2一样也是Hello World!//如果需要定义一个由很多相同字符组成的字符串时,还有另外的简便写法string s4(int n,char c);//s4是被初始化为由n的字符c组成的字符串
About the request In C language, we can use the strlen() function to find the length of a string. In C, we can also use strlen(s3); this method can find the actual length of the s3 string, but because C has a different relationship with the string class Languages are fundamentally different, so we generally call the string.length() function to find the length of a string
int len=0;len=string.length(s3); cout<<"s3字符串的长度为"<<len<<endl;
As we mentioned above, if a string-like string is assigned to another string-like string, Just assign the value directly, but what if the string class is assigned to the char* class or the char* class is assigned to the string class? Of course, it cannot be assigned directly, just look at the code
//string类赋值给string类string s1="hello world";string s2; s2=s1;//string类赋值给char*类string s1="hello world";char str[20]={0}; strcpy_s(str,s1.c_str());//char*类赋值给string类char str[20]="hello world";string s2; s2=str;
At the same time, string type variables can also use character array operations to change one of the variables in it, for example
#include <iostream>#include <string>string s1="this is my house";int i;//如果我们现在想改变里面某一个字符,可以直接将s1当成数组,找到对应的下标来改变i=6; s[i]='t';//这样就可以将第6个字符改成t了
With string class, we can use the " " or " = " operator to directly splice strings, which is very convenient. We no longer need to use strcat(), strcpy(), malloc() and other functions in C language to splice strings. You no longer have to worry about insufficient space and overflow. When using "" to splice strings, both sides of the operator can be string strings, a string string and a C-style string, or a string string and a char character.
Assignment of string class
string &operator=(const string &s);//把字符串s赋给当前字符串 string &assign(const char *s);//用c类型字符串s赋值string &assign(const char *s,int n);//用c字符串s开始的n个字符赋值string &assign(const string &s);//把字符串s赋给当前字符串string &assign(int n,char c);//用n个字符c赋值给当前字符串string &assign(const string &s,int start,int n);//把字符串s中从start开始的n个字符赋给当前字符串string &assign(const_iterator first,const_itertor last);//把first和last迭代器之间的部分赋给字符串
Connection of string
string &operator+=(const string &s);//把字符串s连接到当前字符串的结尾 string &append(const char *s);//把c类型字符串s连接到当前字符串结尾string &append(const char *s,int n);//把c类型字符串s的前n个字符连接到当前字符串结尾string &append(const string &s); //同operator+=()string &append(const string &s,int pos,int n);//把字符串s中从pos开始的n个字符连接到当前字符串的结尾string &append(int n,char c); //在当前字符串结尾添加n个字符cstring &append(const_iterator first,const_iterator last);//把迭代器first和last之间的部分连接到当前字符串的结尾
Substring of stringstring substr(int pos = 0,int n = npos) const;/ /Return a string consisting of n characters starting from pos
void swap(string &s2); //交换当前字符串与s2的值
Search for string
rfind() is very similar to find(), it also searches for sub-characters in a string string, the difference is that the find() function searches backwards from the second parameter, while the rfind() function searches up to the second parameter. If the sub-character has not been found by the subscript specified by the second parameter String, it returns an infinite value 4294967295
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置int rfind(char c, int pos = npos) const;//从pos开始从后向前查找字符c在当前串中的位置int rfind(const char *s, int pos = npos) const;int rfind(const char *s, int pos, int n = npos) const;int rfind(const string &s,int pos = npos) const;//从pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置,成功返回所在位置,失败时返回string::npos的值
Insertion function of string class:
string &insert(int p0, const char *s);string &insert(int p0, const char *s, int n);string &insert(int p0,const string &s);string &insert(int p0,const string &s, int pos, int n);//前4个函数在p0位置插入字符串s中pos开始的前n个字符string &insert(int p0, int n, char c);//此函数在p0处插入n个字符citerator insert(iterator it, char c);//在it处插入字符c,返回插入后迭代器的位置void insert(iterator it, const_iterator first, const_iterator last);//在it处插入[first,last)之间的字符void insert(iterator it, int n, char c);//在it处插入n个字符c
Regarding the usage of string class objects in C, today I talked about some basics.
Related recommendations:
Summary of usage of string class in standard C
The above is the detailed content of Summary of usage of string objects in C++. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

The calculation of C35 is essentially combinatorial mathematics, representing the number of combinations selected from 3 of 5 elements. The calculation formula is C53 = 5! / (3! * 2!), which can be directly calculated by loops to improve efficiency and avoid overflow. In addition, understanding the nature of combinations and mastering efficient calculation methods is crucial to solving many problems in the fields of probability statistics, cryptography, algorithm design, etc.

Multithreading in the language can greatly improve program efficiency. There are four main ways to implement multithreading in C language: Create independent processes: Create multiple independently running processes, each process has its own memory space. Pseudo-multithreading: Create multiple execution streams in a process that share the same memory space and execute alternately. Multi-threaded library: Use multi-threaded libraries such as pthreads to create and manage threads, providing rich thread operation functions. Coroutine: A lightweight multi-threaded implementation that divides tasks into small subtasks and executes them in turn.

std::unique removes adjacent duplicate elements in the container and moves them to the end, returning an iterator pointing to the first duplicate element. std::distance calculates the distance between two iterators, that is, the number of elements they point to. These two functions are useful for optimizing code and improving efficiency, but there are also some pitfalls to be paid attention to, such as: std::unique only deals with adjacent duplicate elements. std::distance is less efficient when dealing with non-random access iterators. By mastering these features and best practices, you can fully utilize the power of these two functions.

In C language, snake nomenclature is a coding style convention, which uses underscores to connect multiple words to form variable names or function names to enhance readability. Although it won't affect compilation and operation, lengthy naming, IDE support issues, and historical baggage need to be considered.

The release_semaphore function in C is used to release the obtained semaphore so that other threads or processes can access shared resources. It increases the semaphore count by 1, allowing the blocking thread to continue execution.

Exploring Undefined Behaviors in C Programming: A Detailed Guide This article introduces an e-book on Undefined Behaviors in C Programming, a total of 12 chapters covering some of the most difficult and lesser-known aspects of C Programming. This book is not an introductory textbook for C language, but is aimed at readers familiar with C language programming, and explores in-depth various situations and potential consequences of undefined behaviors. Author DmitrySviridkin, editor Andrey Karpov. After six months of careful preparation, this e-book finally met with readers. Printed versions will also be launched in the future. This book was originally planned to include 11 chapters, but during the creation process, the content was continuously enriched and finally expanded to 12 chapters - this itself is a classic array out-of-bounds case, and it can be said to be every C programmer
