Table of contents
Some basic operations of string
Conversion between char* type and string type
String truncation and deletion
Classification of containers
In the actual development process, the importance of the data structure itself is not less than that of the algorithm that operates on the data structure. When there are parts that require high time in the program, the choice of the data structure becomes even more important.
The number of classic data structures is limited, but we often repeat some codes written to implement structures such as vectors and linked lists. These codes are very similar, but there are differences in details to adapt to changes in different data. The STL container provides us with such convenience. It allows us to reuse the existing implementation to construct our own data structure of a specific type. By setting some templates, the STL container provides support for the most commonly used data structures. These templates Parameters allow us to specify the data type of the elements in the container, which can simplify many of our repetitive and tedious tasks.
The container part is mainly composed of header files <vector>, <list>, <deque>, <set>, <map>, <stack> and <queue>.
Sequence containers
Each element has a fixed position -- determined by when and where it is inserted, independent of the element's value.
vector、deque、list
Associated containers
Element position depends on a specific sorting criterion, independent of insertion order
set、multiset、map、multimap
data structure |
describe |
Implement the header file |
vector |
elements stored contiguously |
<vector> |
list |
A doubly linked list consisting of nodes, each node contains an element |
<list> |
Double queue (deque) |
An array of consecutively stored pointers to different elements |
<deque> |
set |
A red-black tree composed of nodes, each node contains an element, the nodes are arranged by some predicate acting on the element pair, no two different elements can have the same order |
<set> |
multiset |
A set that allows the existence of two elements of equal order |
<set> |
stack |
Last-in-first-out permutation of values |
<stack> |
queue |
first-in-first-out arrangement |
<queue> |
Priority queue (priority_queue) |
A queue in which the order of the elements is determined by some predicate acting on the stored value pairs |
<queue> |
map |
A collection of {key, value} pairs, arranged with some predicate acting on the key pairs |
<map> |
Multimap (multimap) |
map that allows key pairs to have equal order |
<map> |
string
The concept of string
- string is the string type of STL, which is usually used to represent strings. Before using string, the string is usually represented by char*. Both string and char* can be used to represent strings, so what is the difference between the two.
Comparison of string and char*
- string is a class and char* is a pointer to a character.
String encapsulates char*, manages this string, and is a container of char* type.
- String does not need to consider memory release and out-of-bounds.
string manages memory allocated by char*. Every time a string is copied, the value is maintained by the string class, so there is no need to worry about copying out of bounds and value out of bounds.
- string provides a series of string manipulation functions
Find find, copy copy, delete erase, replace replace, insert insert
initialization of string
There are four main ways to initialize string.
- Default constructor:
string(); //Construct an empty string string s1.
- Copy constructor:
string(const string &str); //Construct a string that is the same as str. Such as string s1(s2).
- constructor with parameters
string(const char *s); //initialize with string s
string(int n,char c); //Initialize with n characters c
void main21()
{
string s1 = "aaaa";
string s2("bbbb");
string s3 = s2;//通过拷贝构造函数来初始化对象
string s4(10, 'a');
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
cout << "s3:" << s3 << endl;
cout << "s4:" << s4 << endl;
}
String traversal
There are two main ways of string, one is to traverse through the array, and the other is to traverse through the iterator. When an error occurs in the array method, an exception will not be thrown, but the iterator will.
//string的遍历
void main22()
{
string s1 = "abcdefg";
//1 数组方式
for (int i = 0; i < s1.length(); i++)
{
cout << s1[i] << " ";//出现错误不向外面抛出异常引起程序的中断
}
cout << endl;
//2 迭代器
for (string::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
try
{
for (int i = 0; i < s1.length()+3; i++)
{
cout << s1.at(i) << " ";//抛出异常
}
}
catch (...)
{
cout << "发生异常\n";
}
cout << endl;
}
Some basic operations of string
Conversion between char* type and string type
const char *c_str() const; //Return the first address of a string ending with '\0'.
//字符指针和string的转换
void main23()
{
string s1 = "aaabbb";
//1 s1===>char *
printf("s1:%s\n", s1.c_str());
//2 char *===>string
//3 s1的内容copy buf中
char buf[128] = { 0 };
s1.copy(buf, 3, 0);//注意 只给你copy3个字符 不会加\0
cout << "buf1:" << buf << endl;
}
concatenation of strings
string &operator+=(const string &s); //Connect the string s to the end of the current string
string &operator+=(const char *s);//connect the string s to the end of the current string
string &append(const char *s); //Connect the string s to the end of the current string
string &append(const char *s,int n); //Connect the first n characters of the string s to the end of the current string
string &append(const string &s); //同operator+=()
string &append(const string &s,int pos, int n);//Connect the n characters starting from pos in the string s to the end of the current string
string &append(int n, char c); //Add n characters c at the end of the current string
//字符串的连接
void main24()
{
string s1 = "aaa";
string s2 = "bbb";
s1 = s1 + s2;
cout << s1 << endl;
string s3 = "333";
string s4 = "444";
s3.append(s4);
cout << s3 << endl;
}
String Find and Replace
look up
int find(char c,int pos=0) const; //Start from pos to find the position of character c in the current string
int find(const char *s, int pos=0) const; //Start from pos to find the position of string s in the current string
int find(const string &s, int pos=0) const; //Start from pos to find the position of string s in the current string
If the find function cannot find it, it returns -1
int rfind(char c, int pos=npos) const; //Find the position of character c in the current string starting from pos from back to front
int rfind(const char *s, int pos=npos) const;
int rfind(const string &s, int pos=npos) const;
//rfind means reverse lookup, if not found, return -1
replace
string &replace(int pos, int n, const char *s);//Delete n characters starting from pos, then insert string s at pos
string &replace(int pos, int n, const string &s); //Delete n characters starting from pos, then insert string s at pos
void swap(string &s2); //Exchange the value of the current string and s2
//字符串的查找和替换
void main25()
{
string s1 = "wbm hello wbm 111 wbm 222 wbm 333";
//查找第一次wbm index
int index = s1.find("wbm", 0);//位置下标从0开始
cout << "index:" << index << endl;
//求wbm出现的次数 每一次出现的数组下标
int offindex = s1.find("wbm", 0);
while (offindex!=string::npos)
{
cout << "offindex:" << offindex << endl;
offindex = offindex + 1;
offindex = s1.find("wbm", offindex);
}
//替换 把小写换成大写
string s3 = "aaa bbb ccc";
s3.replace(0, 3, "AAA");
cout << "s3:" << s3 << endl;
offindex = s1.find("wbm", 0);
while (offindex != string::npos)
{
cout << "offindex:" << offindex << endl;
s1.replace(offindex, 3, "WBM");
offindex = offindex + 1;
offindex = s1.find("wbm", offindex);
}
cout << "s1:" << s1 << endl;
}
String truncation and deletion
string &insert(int pos, const char *s);
string &insert(int pos, const string &s);
//The first two functions insert the string s at position pos
string &insert(int pos, int n, char c); //Insert n characters c at position pos
string &erase(int pos=0, int n=npos); //Delete n characters starting from pos, and return the modified string
//截断和删除
void main26()
{
string s1 = "hello1 hello2 hello1";
string::iterator it = find(s1.begin(), s1.end(), 'l');
while (it != s1.end())
{
s1.erase(it);
it = find(it, s1.end(), 'l');
}
cout << "s1:" << s1 << endl;
s1.erase(s1.begin(), s1.end());
cout << "s1:" << s1 << endl;
cout << "s1.length():" << s1.length() << endl;
string s2 = "BBB";
s2.insert(0, "AAA"); // 头插法
cout << "s2:" << s2 << endl;
s2.insert(s2.length(), "CCC");
cout << "s2:" << s2 << endl;
}
String algorithm related
Function to convert string case to case
void main27()
{
string s1 = "AAAbbb";
//函数的入口地址 函数对象 预定义的函数
transform(s1.begin(), s1.end(), s1.begin(), toupper);
cout << s1 << endl;
string s2 = "AAAbbb";
transform(s2.begin(), s2.end(), s2.begin(), tolower);
cout << s2 << endl;
}