3.3 STL commonly used containers -- 1. string container

1. string container

1.1 Basic concept of string

Nature:

  • string is a C++-style string, and string is essentially a class.

The difference between string and char *:

  • char * is a pointer
  • string is a class, which encapsulates char* inside the class, and manages this string, which is a container of char* type.

Features:

  • The string class internally encapsulates many member methods
     such as: find find, copy copy, delete delete, replace replace, insert insert
  • String manages the memory allocated by char*, so there is no need to worry about copying out of bounds and value out of bounds, etc., and the internal responsibility of the class

1.2 string constructor

Constructor prototype:

  • string(); // Create an empty string, such as string str;
  • string(const char *s); // Initialize with string s
  • string(const string& str); // use a string object to initialize another string object
  • string(int n, char c); // Initialize with n string c

Code example:

#include<iostream>
using namespace std;
#include<string>

void test_01()
{
    
    
	string s1; // 创建空字符传,调用无参构造函数
	cout << "str1 = " << s1 << endl;

	const char* str = "hello world";
	string s2(str); // 把c_string 转换成了string
	cout << "str2 = " << s2 << endl; 

	string s3(s2); // 调用拷贝构造函数
	cout << "str3 = " << s3 << endl;

	string s4(10, 'a');
	cout << "str4 = " << s4 << endl;
}
int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// str1 =
// str2 = hello world
// str3 = hello world
// str4 = aaaaaaaaaa

1.3 String assignment operation (=, assign)

Function description:

  • assign value to string string

The function prototype of the assignment:

  • string& operator=(const char * s); // assign the char* type string to the current string
  • string& operator=(const string &s); // Assign the string s to the current string
  • string& operator=(char c); // assign the character to the current string
  • string& assign(const char s); // assign the char type string to the current string
  • string& assign(const char *s, int n); // Assign the first n characters of the string s to the current string
  • string& assign(const string &s); // Assign the string s to the current string
  • string& assign(int n, char c); // Assign n characters c to the current string

Code example:

#include<iostream>;
using namespace std;

void test_01()
{
    
    
	string str1;
	str1 = "hello world";
	cout << "str1= " << str1 << endl;

	string str2;
	str2 = str1;
	cout << "str2= " << str2 << endl;

	string str3;
	str3 = 'a';
	cout << "str3= " << str3 << endl;

	string str4;
	str4.assign("hello c++");
	cout << "str4= " << str4 << endl;

	string str5;
	str5.assign("hello c++", 5);
	cout << "str5= " << str5 << endl;

	string str6;
	str6 = str5;
	cout << "str6= " << str6 << endl;

	string str7;
	str7.assign(5, 'x');
	cout << "str7= " << str7 << endl;
}

int main() 
{
    
    
	test_01();
	system("pause");
	return 0;
}
// str1= hello world
// str2= hello world
// str3= a
// str4= hello c++
// str5= hello
// str6= hello
// str7= xxxxx

1.4 String concatenation (+=, append)

Function description:

  • Realize the function prototype of concatenating strings at the end of strings :
  • string& operator+=(const char* str); // overloaded += operator
  • string& op+=(const char c); // overloaded += operator
  • string& op+=(const string& str); // Overload += operator
  • 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 to the end of the current string
  • string& append(const string &s); //同operator+=(const char* str);
  • string& append(const string &s, int pos, int n); // Intercept n characters from pos in the string s and connect to the end of the string

Code example:

#include<iostream>
using namespace std;

void test_01()
{
    
    
	string str1 = "我";
	str1 += "爱玩游戏";
	cout << "str1: " << str1 << endl;

	str1 += ":";
	cout << "str1: " << str1 << endl;

	string str2 = "LOL DNF";
	str1 += str2;
	cout << "str1: " << str1 << endl;

	string str3 = "I";
	str3.append(" love ");
	str3.append("game abcde", 4);

	str3.append(str2, 4, 3);
	cout << "str3: " << str3 << endl;
}

int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// str1: 我爱玩游戏
// str1: 我爱玩游戏:
// str1: 我爱玩游戏:LOL DNF
// str3: I love gameDNF

1.5 string search and replace (find, rfind, replace)

Function description:

  • Find: Find whether the specified string exists
  • Replace: replace the string at the specified position

Function prototype:

  • int find(const string& str, int pos = 0) const; // From the operation to the right, find the first occurrence of str, start from pos
  • int rfind(const string& str, int pos = 0) const; // From right to left, find the last occurrence of str, starting from pos
  • string& replace(int pos, int n, const string& str); // Replace n characters starting from pos with string str

Code example:

#include<iostream>
using namespace std;

// 查找和替换
void test_01()
{
    
    
	// 查找
	string str1 = "abcdefgde";
	int pos = str1.find("de");
	// find找到字符串后返回查找的第一个字符位置,找不到返回-1
	if (pos==-1)
	{
    
    
		cout << "未找到" << endl;
	}
	else
	{
    
    
		cout << "pos = " << pos << endl;
	}
	pos = str1.rfind('de');
	cout << "pos = " << pos << endl;
}

void test_02()
{
    
    
	// 替换
	string str1 = "abcdefgde";
	str1.replace(1, 3, "1111");
	cout << "str1 = " << str1 << endl;
}
int main()
{
    
    
	test_01();
	test_02();
	system("pause");
	return 0;
}
// pos = 3
// pos = 8
// str1 = a1111efgde

1.6 String comparison (Compare)

Function description:

  • comparison between strings

Compare by:

  • String comparison is based on the ASCII code of the characters
    = return 0 > return 1 < return -1
  • String comparison is mainly used to compare whether two strings are equal, and it is meaningless to judge the size

Function prototype:

  • int Compare(const string &s) const; //Compare with string s
  • int Compare(const char *s) const; // compare with char *s

Code example:

#include<iostream>
using namespace std;
#include<string>

// 字符串比较
void test_01()
{
    
    
	string s1 = "hello";
	string s2 = "aello";
	int ret = s1.compare(s2);

	if (ret ==0)
	{
    
    
		cout << "s1 等于 s2" << endl;
	}
	else if (ret > 0)
	{
    
    
		cout << "s1 大于 s2" << endl;
	}
	else
	{
    
    
		cout << "s1 小于 s2" << endl;
	}
}
int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// s1 大于 s2

1.7 String access ([], at)

There are two ways to access a single string in string:

  • char& operator[] (int n); // Get characters by []
  • char& at(int n); // get character by at method

Code example:

#include<iostream>
using namespace std;

void test_01()
{
    
    
	string str = "hello world";
	for (int i = 0; i < str.size(); i++)
	{
    
    
		cout << str[i] << " ";
	}
	cout << endl;
	
	for (int i = 0; i < str.size(); i++)
	{
    
    
		cout << str.at(i) << " ";
	}
	cout << endl;
	
	// 字符修改
	str[0] = 'x';
	str.at(1) = 'x';
	cout << str << endl;
}

int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// h e l l o   w o r l d
// h e l l o   w o r l d
// xxllo world

1.8 string insertion and deletion (insert, erase)

Function description:

  • Insert and delete characters on the string string

Function prototype:

  • string& insert(int pos, const char* s); // insert string
  • string& insert(int pos, const string& str); // insert string
  • string& insert(int pos, int n, char c); // Insert n characters c at the specified position
  • string& erase(int pos, int n ); // delete n characters starting from pos

Code example:

#include<iostream>
using namespace std;

// 字符串插入和删除
void test_01()
{
    
    
	string str = "hello";
	str.insert(1, "111");
	cout << str << endl;

	str.erase(1, 3);
	cout << str << endl;
}
int main()
{
    
    
	test_01();
	system("pause");
	return 0;
}
// h111ello
// hello

1.9 string substring (substr)

Function description:

  • Get the desired substring from a string

Function prototype:

  • string substr(int pos=0, int n=npos) const; // return a string consisting of n characters starting from pos

Code example:

#include<iostream>
using namespace std;
#include<string>

void test_01()
{
    
    
	string str = "abcdefg";
	string subStr = str.substr(1, 3);
	cout << "subStr = " << subStr << endl;

	string email = "[email protected]";
	int pos = email.find("@");
	string username = email.substr(0, pos);
	cout << "username: " << username << endl;
}

int main() 
{
    
    
	test_01();
	system("pause");
	return 0;
}
// subStr = bcd
// username: hello

Guess you like

Origin blog.csdn.net/yewumeng123/article/details/131137993