c++ string detailed explanation

string string

string can essentially be regarded as a vector< char >, that is, a vector whose elements are char. So the basic operations on vector can be performed on string.


String creation and initialization

String is similar to vector and has several initialization methods. (Learn with an example)

initialization of string

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

//string的初始化

int main()
{
    
    
	string s1;
	string s2 (3,'a');
	string s3 ("value");
	string s4 (s3);
	cout << "s1:" << s1 << endl;
	cout << "s2: " << s2 << endl;
	cout << "s3: " << s3 << endl;
	cout << "s4: " << s4 << endl;
	return 0 ;
}

Running results:
![Insert picture description here](https://img-blog.csdnimg.cn/826542e6cb6e405582aec63ab4b94c7d
The example shows several different string initialization methods. Since the string supports the output operator "<<", it can be printed directly.

The first and last initialization methods are similar to vector , which are empty strings and copy initialization strings, the second method is also similar to vector < char >, and the third method is to use string literals to initialize the string string. It should be noted that when using strings, we need to include the <string> header file of the standard library.


String reading and writing

Use cout to print the string, that is, output the string to the standard output port, which is the command line window. Similarly, C++ also provides a method to write data to string from standard input, that is, the keyboard.

String reading and writing

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

//string的读写

int main()
{
    
    
	string s1;
	string s2;
	cout << "请输入用两个空格隔开的字符串!" << endl;
	cin >> s1 >> s2;
	cout << "s1: " << s1 << endl;
	cout << "s2: " << s2 << endl;
	return 0 ;
}


Running results:
insert image description here
In actual operation projects, users often do not know how many strings the program expects to input. At this time, we can use the feature of the input operator ">>" to accept an uncertain number of inputs.

cyclic reading of string

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

//string的循环读取

int main()
{
    
    
	vector<string> strVec;
	string s;
	while (cin >> s)
	{
    
    
		strVec.push_back(s);
		for ( int i = 0; i < strVec.size() ;i++ )
	{
    
    
		cout << strVec[i] << " " ;
	}
		cout << endl;
	}
	return 0;
}

Running result:
insert image description here
As can be seen from the example, the return value of the expression of the input operation cin >> s can reflect whether there is currently any input. Since standard input is keyboard input, there is no end sign, so this cycle will continue forever. If it is a file input, it will jump out of the loop when there is no string at the end of the file.
In the example, 6 character strings are input sequentially at runtime, and the program will feedback and output all character strings in the current wife's wear vector each time.


Basic operation of string

The operations of vector are all applicable to string. Next, we briefly introduce these basic operations.

Basic operation of string

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

//string的基本操作

int main()
{
    
    
	string s;
	if (s.empty() ) cout << "字符串是空的" <<endl;
	cout << "添加两个字符" <<endl;
	s.push_back('a');
	s.push_back('b');
	if (s.empty() ) cout << "字符串是空的" <<endl;
	cout << "字符串有:" << s.length() << "个字符" << endl;
	cout << "打印字符串: " << s <<endl;
	cout << "移除一个字符!" << endl;
	s.pop_back();
	if (s.empty() ) cout <<"字符串是空的!" << endl;
	cout << "字符串有:" << s.length() << "个字符" << endl;
	cout << "打印字符串: " << s << endl;
	return 0;
}

Running results:
insert image description here
Note: The size() of string can also be replaced by length(). Generally, length() is used just to better indicate that this is a string.
In addition, string can also use square brackets ([]) to obtain characters at a certain position like vector.

Get the characters in string

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

//获取string中的字符

int main ()
{
    
    
	string s = "hello world!" ;
	for (int i = 0; i < s.length() ; i++)
	{
    
    
		if (i % 2)
		{
    
    
			cout <<s[i];
		}
	}
	cout << endl;
	return 0 ;
}

Running result:
insert image description here
The example shows the application of using the subscript operator “[]” to obtain strings.


comparison of strings

String supports relational operators. The equality and inequality judgment of string is somewhat similar to that of vector. It is necessary to judge whether all characters are equal. As long as there is one character that is not equal or a certain string has one more character, then the strings are not equal.
The greater than and less than judgments of string will compare each corresponding character separately, and return the result immediately if there is a mismatch. Among them, the character comparison uses the order of the dictionary, that is, the ASCLL code value of each character in the string is compared in turn, and if they are the same, continue to compare the next one.

comparison of strings

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

//string的比较

int main()
{
    
    
	string s1 = "";
	string s2 = "";
	for (int i = 0; i < 3; i++)
	{
    
    
		cout << "请输入两个用空格隔开的字符串:" << endl;
		cin >> s1 >> s2;
		if ( s1 < s2 )
		{
    
    
			cout << "字符串" << s1 << "小于" << s2 <<endl;
		}
		else if ( s1 > s2 )
		{
    
    
			cout << "字符串" << s1 << "大于" << s2 <<endl;
		}
		else
		{
    
    
			cout << "字符串" << s1 << "等于" << s2 <<endl;
		}
	}
	return 0 ;
}

Running result:
insert image description here
In the example, the method of assigning a string literal to string is used to initialize an empty string.

This small program supports input, and you can input two arbitrary strings to deepen your understanding of string comparison rules. In the example, 3 pairs of strings are input, which respectively show the 3 rules of string comparison:

<1> String comparison is case-sensitive, and uppercase letters are smaller than lowercase letters (according to the order of ASCLL codes).
<2> The strings are not equal in length, but when the short string is exactly equal to the previous part of the long string, the short string is smaller than the long string.
<3> When the first pair of unmatched characters is encountered, the result of the lexicographical comparison is returned immediately.

Here both c and csharp start with c, so the long csharp is bigger. The first character of both csharp and cplusplus is c, continue to look at the second character, because s is larger than p, so csharp is larger than cplusplus.


concatenation of strings

The concatenation of strings is achieved through the addition operator, and strings or string literals can be freely combined on both sides of the plus sign.

concatenation of strings

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

//string的连接

int main()
{
    
    
	string s1 = "";
	string s2 = "";
	cout << "请输入两个用空格间隔的字符串:" << endl;
	cin >> s1 >> s2 ;
	string s3 = s1 + s2 ;
	cout << "字符串连接的结果为:" << s3 << endl;
	for (int i = 0;i < 3;i++)
	{
    
    
		string s4 = "";
		cout << "请输入字符串:" << endl;
		cin >> s4;
		s3 +=s4;
		cout << "字符连接的结果是: " << s3 << endl;
	}
	return 0;
}

Running result:
insert image description here
The example shows the concatenation of strings. In addition to the addition operator, you can also use the addition assignment operator to concatenate strings.

If this article is helpful to you, please like it and support it~

Guess you like

Origin blog.csdn.net/m0_62870588/article/details/123716198