Chapter 3 STL - string container of common containers

Posted by james_kirk on Sun, 20 Feb 2022 16:36:19 +0100

3.1 string container

3.1.1 basic concept of string

essence

  • 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. It is a container of char * type to manage this string

characteristic

  • string class encapsulates many member methods
  • For example: find, copy, delete, replace, insert

3.1.2 string constructor

Constructor prototype:

  • string(); / / create an empty string, for example: string str;
  • string(const char * s); / / initialize with string s
  • string(const string& str);// Initializing another string object with one string object
  • string(int n, char c); / / initialize with n characters C

code

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

void test01()
{
	string s1;//Default construction
	
	const char * str = "hello,world";
	string s2(str);

	cout << "s2= " << s2 << endl;

	string s3(s2);
	cout<< "s3= " << s3 << endl;

	string s4(10, 'a');
	cout << "s4= " << s4 << endl;
}

int main()
{
	test01();

	system("pause");

	return 0;
} 

3.1.3 string assignment

Function Description:

  • Assign a value to a string

Prototype of assigned function:

  • string& operator =(const cahr* s); / / char * type string assigned to the current string
  • string& operator =(const string &s); // Assign the string s to the current string
  • string& operator =(char c); / / assign characters to the current string
  • string& assign(const char *s); / / assign the string s to the current string
  • string& assign(const char *s,int n);// Assign the first n characters of 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

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

void test01() 
{
	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++",7);
	cout << "str5= " << str5 << endl;

	string str6;
	str6.assign(str5);
	cout << "str6= " << str6 << endl;

	string str7;
	str7.assign(8, 'd');
	cout << "str7= " << str7 << endl;

}

int main()
{

	test01();
	system("pause");

	return 0;
}

Summary:

There are many ways to assign values to string s, and operator = is more practical

3.1.4 string splicing

Function description

  • Implements splicing strings at the end of a string

Code

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

void test01()
{
	string str1 = "I";

	str1 += "Love playing games";

	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 ");
	cout << " str3= " << str3 << endl;

	str3.append("game abds", 4);//Intercept only the first four characters
	cout << " str3= " << str3 << endl;
	                                                                                   
	str3.append(str2,4,7);//Intercept DNF only
	cout << " str3= " << str3 << endl;
}


int main() 
{
	test01();

	system("pause");
	return 0;

}

3.1.5 string find and replace

Function description

  • Find: finds whether the specified string exists
  • Replace: replaces the string at the specified position

Code

#include<iostream>
using namespace std;
#include<string>
//1. Search
void test01() 
{
	string str1 = "abcdefde";
	int pos = str1.find("de");
	if (pos == -1) 
	{
		cout << "String not found" << endl;
	}
	else 
	{
		cout << "Find the string, pos=" << pos << endl;
	}
	//rfind
	//Difference between rfind and find - rfind from right to left and find from left to right
	pos = str1.rfind("de");
	
	cout << "pos= " << pos << endl;
}

//2. Replace
void test02() 
{
	string str1 = "abcdefg";
	str1.replace(1, 3, "2222");
	cout << "str1=" << str1 << endl;
}

int main()
{
	//test01();
	test02(); 

	system("pause");

	return 0;
}

summary

  • find is from left to right and rfind is from right to left
  • find returns the first character position of the string found, and - 1 if not found
  • When replacing, you need to specify where to start, how many characters to replace and what kind of characters to replace

3.1.6 string comparison

Code

#include<iostream>
using namespace std;

//string comparison
void test01() 
{
	string str1 = "hello";
	string str2 = "xello";

	if (str1.compare(str2) == 0) 
	{
		cout << "str1 be equal to str2" << endl;
	}
	else if (str1.compare(str2) > 0) 
	{
		cout << "str1 greater than str2" << endl;
	}
	else 
	{
		cout << "str1 less than str2" << endl;
	}
}

int main()
{
	test01();

	system("pause");

	return 0;
}

3.1.7 string character access

Code

#include<iostream>
using namespace std;
#include<string>
//string character access
void test01() 
{
	string str = "hello";

	//1. Single character access via []
	for (int i = 0; i < str.size(); i++) 
	{
		cout << str[i] << " ";
	}
	cout << endl;
	//2. Single character access via at mode
	for (int i = 0; i < str.size(); i++) 
	{
		cout << str.at(i) << " ";
	}
	cout << endl;

	//Modify single character
	str[0] = 'x';
	cout << "str= " << str << endl;

	str.at(1) = 'x';
	cout << "str= " << str << endl;
}

int main()
{
	test01();

	system("pause");

	return 0;
}

Summary:

  • There are two ways to access a single character in a string, using [] and at

3.1.8 string insertion and deletion

Code

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

void test01() 
{
	string str = "hello";
	//insert
	str.insert(1,"111");
	cout << "str= " << str << endl;
	//delete
	str.erase(1, 3);
	cout << "str= " << str << endl;
}

int main()
{
	test01();
	system("pause");

	return 0;
}

Summary: the starting subscript of insertion and deletion starts from 0

3.1.9 string substring

Code

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

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

}
//Practical operation - get the user name information in the email address
void test02() 
{
	string email = "zhoushamiao@qq.com";
	int pos = email.find("@");

	string usrname = email.substr(0, pos);

	cout << " usrname= " << usrname << endl;
}

int main()
{
	test01();
	test02();
	system("pause");

	return 0;
}

Topics: C++