Detailed explanation of string string type in C++ and analysis of common methods

Because there is no variable type directly related to strings in C except for character arrays, it is not very convenient when dealing with some problems. So C++ provides a new data type - string type (string type). In terms of usage, it can be used to define variables in the same way as char and int types. This is a string variable - a character sequence represented by a name 。Actually

, string is not a basic type of the C++ language itself. It is a string class declared in the C++ standard library. Objects can be defined with this class. Each string variable is an object of the string class ( note Header file format #include<string>) .

string can construct a string variable, so what are its operations? It is included in the string library (not string.h and no .h), it can define a string variable just like defining a character, and the powerful C++ also has various built-in functions, basically realizing that there is no need to write the function by hand. And can perform lexicographical comparison and string operations.

1. Common methods of string string variables

The method summary and some results are shown as follows:

#include<bits/stdc++.h>
using namespace std;
int main(){
	string s="61116";
	string s2="71117";
	cout<<s+s2<<endl;//连接两个字符串
	cout<<(s>s2) <<endl;//比较两个字符串的大小 
	//上面比较两个字符串的大小结果是 true 或者 false  在这里是 1  或者 0 
	cout<<s.substr(3,3)<<endl;//substr(n,l)  从第n个位置截取长度为l的字串,若超过最大长度,则截取到最后。 
	cout<<s.substr(3,5)<<endl;
	 s.append(2,'b');//s.append(n,c);在s串后面加入n个c字符(append还有其他用法,详情可百度)
     s.erase(s.begin(),s.begin()+2);//s.erase(l,r);删除某个区间,l,r都为迭代器
     s.erase(it);//删除一个字符,it为迭代器
     s.size();//返回字符串s的长度大小
     s.begin();//返回首位置的迭代器
     s.end();//返回末位置的迭代器
     s.insert(it,ch);//在it位置插入ch字符,it为迭代器
     s.resize(len,c);//把字符串当前大小置为len,并用c填充不足的部分
     s.empty();//判断是否为空串
     s.length();//返回字符串的长度
     s.max_size();//返回当前系统string对象可存放的最大长度
     s.capacity();//返回当前容量
     s.at(k);//返回第k+1个字符(该用法会坚持是否越界)
     s.c_str();//返回C字符串的指针,内容为s串
     s.find(s1);//查找s中是否包含s1,并返回头位置,找不到则返回string::npos
     s.replace(k,x,ch);//从k位置开始,把后面的x个元素替换为ch(还有很多其他用法)
     s.swap(s2);//交换两个string字符串
	 
	return 0; 
}

2. Summary of common points of string

  1. The string type can be treated as a character array! Contains several (currently 5) elements in a string array, and each element is equivalent to a string variable.
  2. It is not required that each string element has the same length. Even for the same element, its length can be changed. When a value is reassigned to an element, its length may change.
  3. A string is stored in each element of the string array instead of a character. This is the difference between a string array and a character array. If a character array is used to store strings, one element can only store one character, and a one-dimensional A character array stores a string.
  4. Each string element only contains the characters of the string itself and does not include '\0'. (Please correct me if there is any mistake!)

Guess you like

Origin blog.csdn.net/weixin_49418695/article/details/123501244