String of data structure and algorithm

String of data structure and algorithm

The common operations are string and number. String is still very important.

Basic knowledge
1. Input and output
List some commonly used things that cin itself needs to learn a lot...

  1. cin: use whitespace (spaces, tabs, and newlines) to determine where the character ends, which means that cin reads only one word when getting the character array
  2. cin.get() / / get() without any parameters keeps the carriage return after input. At the same time, stop input when receiving carriage return or space or tab.
  3. cin.getline(name,cnt) / / line oriented input, C-style string usage; Name is the name of the character to be read, and cnt is the number of characters to be read. Use spaces to determine the end, but do not leave spaces. The prototype is CIN Getline (char *, int), the first parameter is a char pointer, and the second parameter is the length of the array string.
  4. cin.get(name,cnt) / / do not discard blank space, but keep it in the input queue. cin.getline() actually has three parameters, CIN Getline (variable receiving string, number of characters received, end character)
  5. getline(cin,str) / / handle the function of string class. str is a variable of string type. When using getline(cin,str), you should note that after entering str, you need two carriage returns to output the result. If you receive a string, you can receive spaces and output

2. Characters

'a'//Character literal value, which actually represents an integer, is generally ASCII code value, 'a'=97,'A'=65; The difference between them is 32
"a"//A string literal that ends with a null character (\ 0, ASCII 0)
'b'-'a'//Relative value of ASCII

3. String

C style string

char c[3]={'a','b','\0'};
char c[]="ab";//The two are the same
char a[3]={'a','b','c'}//Char array, different from char string

string class

#include <string>
string a;//a is an empty string
string a="hello";//copy initialization 
string a("hello");//Direct initialization
string s4(n,'c');//Direct initialization, s4 is n c
string s4=string(n,'c');//Copy initialization, s4 is n c
//Read and write string objects
int main()
{
    string s;
    cin>>s;//Read the string object into s and stop when it encounters blank space
    cout<<s<<endl;
    string s1,s2;
    cin>>s1>>s2;
    cout<<s1<<s2<<endl;
    return 0;
}
//Read unknown number of string objects
int main()
{
    string word;
    while(cin>>word)//Repeated reading
        cout<<word<<endl;//Output one by one
    return 0;
}
//Use getline to read an entire line
int main()
{
    string line;
    while(getline(cin,line))//As soon as getline encounters a newline character, it ends reading and returns the result
        cout<<line<endl;
    return 0;
}
//empty and size operations of string
//empty returns a Boolean value
while(getline(cin,line))//Each time a line is read, it will be skipped if an empty line is encountered
    if(!line.empty())
        cout<<line<<endl;
//The size function returns the length of the string object (that is, the number of characters in the string object)
string line;
while(getline(cin,line))
    if(line.size()>80)
        cout<<line<<endl;
//string objects can be compared with ordinary relational operators
 Note: if two string Objects have different lengths and are short string Each character of the object must be and longer string If the characters in the corresponding position of the object are the same, it is said to be short string Less than long
    If two string Objects are inconsistent in some corresponding positions, then string The result of object comparison is string Comparison of the first pair of different characters in the object
    
//The value of one object is assigned to another object
string st1(10,'c'),st2;
st2=st1;
//Use a range based for loop to process each character
 Syntax: for(declaration:expression)//declaration defines a variable, which is used to access the basic element in the sequence, and expression is an object
    statement
string str("some string")
for(auto c:str)//One character in str is output on each line
    cout<<c<<endl;
//Use the range for to change the characters in the string
string s("Hello World");
for(auto &c:s)//Loop variables must be defined as reference types,
    c=toupper(c);//The assignment statement will change the value of the character in s, which is the value of the character bound by c
cout<<s<<endl;
//Accessing a single character in a string object
//Use subscript (index)
string s("some string");
if(!s.empty())
    s[0]=toupper(s[0]);

4. Conversion
Conversion of char and int
char character to int

char c='6';
int a=c-'0';
char c1[]="123";
int a1=c1[0]-'0';

Convert char string to int

char str[]="123";
int a= atoi(str);

Conversion of string and int
string to int (numeric only)

string str="123";
int a=stoi(str);

//Method of removing leading 0
int ans=0;
for(int i=0;i<str.size();i++){
	ans=ans*10+str[i]-'0';
}

int to string

//to_string
int a=1;
string str=to_string(a);

toggle case
Single character conversion

//ASCII difference between upper and lower case characters 32
//Function, uppercase to lowercase
#include <stdio.h>
#include <ctype.h>
int main() {
    char s[] = "Clare";
    s[0] = toupper(s[0]);//Turn lowercase to uppercase
    // s[0] = tolower(s[0]);// Capital to lowercase
}

Conversion of string as a whole

#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>
using namespace std;
int main() {
    string s = "Clare";
    // toUpper
    transform(s.begin(), s.end(), s.begin(), ::toupper);
    // toLower
    //transform(s.begin(),s.end(),s.begin(), ::tolower);
    cout << s << endl;
}

5. Common methods
Find the substring of string object
substr member function can be used to find substring (n, m). The prototype is as follows:

string substr(int n = 0, int m = string::npos) const;
//When calling, if M is omitted or m exceeds the length of the string, the obtained substring is the part from the subscript n to the end of the string. For example:
    string s1 = "this is ok";
    string s2 = s1.substr(2, 4);  // s2 = "is i"
    s2 = s1.substr(2);  // s2 = "is is ok"

Insert string

//The insert member function can insert another string into the string object, and the return value is the reference of the object itself. For example:
    string s1("Limitless"), s2("00");
    s1.insert(2, "123");  //Insert the string "123" at subscript 2, S1 = "limitless"
    s1.insert(3, s2);  //Insert S2 at subscript 2, S1 = "li10023mitless"
    s1.insert(3, 5, 'X');  //Insert five 'X's' at subscript 3, S1 = "li1xxxxxxx 0023mitless"

Delete string

//The erase member function can delete the substring in the string object, and the return value is the reference of the object itself. For example:
    string s1("Real Steel");
    s1.erase(1, 3);  //Delete the substring (1, 3), and then s1 = "R Steel"
    s1.erase(5);  //Delete subscript 5 and all characters after it, and then s1 = "R Ste"

find function usage
Find another string in a string. s.find(str,position)//str is the element to be found. Position is the position in s. return the position of the found string. If it is not found, return npos Reverse lookup rfind.

int main() {
	int sum = 0;
	int n = 0;
	string a = "1234534567834";
	string b = "34";
	while (a.find(b,n) != -1) {
		sum++;
		n = a.find(b, n) + 1;//Search from the next position of the string
	}
	cout << sum << endl;
	return 0;
	
}

6. Others
Dictionary order: for two strings, the size relationship depends on the size relationship of the ASCII value of the first different character of the two strings from left to right.
Compare the first number in the number. If the first is equal, compare the second. Just use the dictionary order‘ 0’ < ‘1’ < ‘2’ < … < ‘9’ < ‘a’ < ‘b’ < … < ‘z’
The following procedure:

static bool cmp(string x,string y) {
	return x + y > y + x;//Is it big on the left or big on the right? Let's go back to the big one
}
int main() {
	string a = "123";
	string b = "234";
	cout << (a < b);
	string s1[3];//string array
	for (int i = 0; i < 3; i++) {
		cin >> s1[i];
	}
	sort(s1, s1 + 3,cmp);//
	for (int i = 0; i < 3; i++) {
		cout << s1[i];
	}
	return 0;
	
}

Classic questions
Most strings are simulated. Of course, there are some problems that are integrated with the algorithm.

  1. Snooker's password
  2. Count words
  3. One edit

Keywords: Algorithm data structure string

Added by pearllysun on Sun, 30 Jan 2022 08:39:12 +0200