Location>code7788 >text

string, String Usage Guide

Popularity:151 ℃/2024-11-18 19:42:31

string

establish

Create a string or an array of strings as follows
With cin, you can read a whole string of characters until a space or a newline is added.

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	string strs[N];
	cin >> s;
	for (int i = 0; i < N; i ++ )
	{
		cin >> strs[i];
	}
	cout << s << endl;
	for (int i = 0; i < N; i ++ )
	{
		cout << strs[i] << endl;
	}
	return 0;
}

access size

.size() and .length() access the length of the string.

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	cin >> s;
	cout << () << endl;
	return 0;
}

beginning and end

.begin() is the address of the beginning

.end() is the address of the end

stick

.push_back() can be tailed

.insert() allows for precise insertion, where the position of the insertion and the content of the inserted character are placed.

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	cin >> s;
	cout << s << endl;
	s.push_back('5');
	cout << s << endl;
	((), '0');
	cout << s << endl;
	return 0;
}

removing

.erase(), which holds the address

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	cin >> s;
	cout << s << endl;
	(());
	cout << s << endl;
	return 0;
}

find

find(), put strings or characters inside, return subscript numbers

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	cin >> s;
	cout << s << endl;
	cout << ('1') << endl;
	return 0;
}

Here's what happens when you put a character

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	cin >> s;
	cout << s << endl;
	cout << ("23") << endl;
	return 0;
}

interception

substr(), puts two numbers, one for the subscript and one for the length of the intercept.

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s;
	cin >> s;
	cout << s << endl;
	s = (2, 5);
	cout << s << endl;
	return 0;
}

comparisons

string will be compared in dictionary order by default, from high to low.

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s1 = "123456", s2 = "12345";
	cout << (s1 > s2) << endl;
	s1 = "12345", s2 = "12346";
	cout << (s1 > s2) << endl;
	return 0;
}

put together

#include <iostream>
using namespace std;
 
const int N = 9;
 
int main()
{
	string s1 = "123456", s2 = "789";
	string s3 = s1 + s2;
	cout << s3 << endl;
	return 0;
}

invert (upside-down, inside-out, back-to-front, white to black etc)

reverse(start address, end address) can reverse the content.