C++ container string class

C++ container string class

1. The constructor of the string class
1 default construction string s1;
2 copy construction string s2(s1);
3 parameter construction string s3("aaa");
4 two parameters construction string s4(3,'c') ;

// string类的构造函数
    string s1;       //默认构造
	string s2(s1);   //拷贝构造
	string s3("aaa");    //有参构造
	string s4(3, 'c');    //两个参数有参构造
	cout << "字符串s1为:"<<s1 << endl;
	cout <<"字符串s2为:"<< s2 << endl;
	cout <<"字符串s3为:"<< s3 << endl;
	cout <<"字符串s4为:"<< s4 << endl;
字符串s1为:
字符串s2为:
字符串s3为:aaa
字符串s4为:ccc

2. Assignment operation of string class
1. string& assign(const char *s,int n); assign the first n strings of string s to the current string
2. string& assign(const string &s,int start,int n); Assign n characters of s from start to the string

//string类的赋值操作
string s5;
	//string& assign(const char *s,int n);  把字符串s的前n个字符赋给当前的字符串
	s5.assign("asddfsdsgdsgd", 8);
	cout << "s5=" << s5 << endl;

	//string& assign(const string &s,int start,int n); 将s从start开始n个字符赋值给字符串(从0开始计算)
	string s6 = "asdfdgfaghd";
	string s7;
	s7.assign(s6, 4, 5);
	cout << "s7=" << s7 << endl;

Output:

s5=asddfsds
s7=dgfag

3. String storage operations
1. Get characters through subscripts []
2. Get characters through at
3. The difference between at and []: [] accesses out of bounds and hangs directly, while at accesses out of bounds will automatically throw an exception

    string s = "hello world!";
	for (int i = 0; i < s.size(); i++)
	{
    
    
		cout << s[i] << endl;//通过[]获取字符
		cout << s.at(i) << endl;//通过at获取字符
	}
	try
	{
    
    
		//s[100];
		s.at(100);
	}
	catch (out_of_range &e)
	{
    
    
		cout << e.what() << endl;
	}
h
h
e
e
l
l
l
l
o
o


w
w
o
o
r
r
l
l
d
d
!
!
invalid string position

Fourth, the string splicing operation
s.append(args), append args to s. Returns a reference to s

    string str1 = "我";
	string str2 = "爱北京";
	str1 += str2;
	cout << "str1="<<str1 << endl;
	string str3 = "天安门";
	str1.append("故宫");
	cout << "新的str1为:" << str1 << endl;

Output:

str1=我爱北京
新的str1为:我爱北京故宫

5. Find and replace string
1. find(), find the substring and return the first position, if the substring is not found, return -1 directly.
2. The difference between rfind() and rfind is that "r" stands for "right"
3. Replace stands for replacement. Replace n characters starting from pos as a string
s.replace(range,args) Delete the characters in the range of s, Replace with the character specified by args. Range is either a subscript and a length, or a pair of iterators to s. Returns a reference to s

string str1 = "ABCDEFGHIJKDE";
	int pos1 = str1.find("DE");
	int pos2 = str1.rfind("DE");
	cout << "pos1=" << pos1 << endl;
	cout << "pos2=" << pos2 << endl;
	str1.replace(3, 4, "126");
	cout << "str1"<<str1 << endl;

Output:

pos1=3
pos2=11
str1ABC126HIJKDE

Six, string string comparison The
compare function is similar to strcmp. According to whether s is equal to, greater than or less than the string specified by the parameter, s.compare returns 0, positive or negative.

string str1 = "ABCDE";
	string str2 = "ABCDE";
	if (str1.compare(str2) == 0)
	{
    
    
		cout << "str1==strr2" << endl;
	}
	else if (str1.compare(str2) > 0)
	{
    
    
		cout << "str1>str2" << endl;
	}
	else
	{
    
    
		cout << "str1<str2" << endl;
	}

Output:

str1==strr2

7. The string substring
s.substr(pos,n) returns a string containing a copy of n characters in s starting from pos. The default value of pos is 0. The default value of n is s.size()-pos, which means that all characters starting from pos are copied

string str = "ABCDE";
	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 << endl;

Output

subStr=BCD
782347428

8. Insert and delete string
1. s.insert(pos,args), insert the character specified by args before pos. pos can be a subscript or an iterator. The version that receives the subscript returns a reference to s; the version that receives the iterator returns an iterator to the first inserted character.
2. s.erase(pos,len), delete len characters starting from position pos. If len is omitted, all characters from the beginning of pos to the end of s are deleted. Return a reference to s.

    string str = "hello";
	str.insert(1, "111");//插入字符串
	cout << "str=" << str << endl;
	str.erase(1, 3);//删除从Pos开始的n个字符
	cout << "str=" << str << endl;

Output:

str=hlllello
str=hello

Guess you like

Origin blog.csdn.net/Little_XWB/article/details/108018758