Detailed explanation of common usage of string

Posted by stageguys on Wed, 02 Mar 2022 13:53:52 +0100

catalogue

1. Definition of string

2. Access of contents in string

3. Analysis of common function examples of string

In C language, the character array char str [] is generally used to store strings, but sometimes it is troublesome to use string array. String type is added to C + +.

Note that if you want to use string, you need to add a string header file, that is # include < string > (note that string. h and string are different header files). In addition, you need to add a sentence under the header file: "using namespace std".

Namely:

#include<string>

using namespace std;

1. Definition of string

Definition and initialization are the same as normal data types:

string str;//Define variables
string str="abcd";//initialization

2. Access of contents in string

(1) Access by subscript

Generally speaking, you can access string s directly like character arrays.

Program code:

#include<bits/stdc++. h> / / Universal header file, which contains header file < string > 
#include<string>
using namespace std;
int main(){
	string str = "hello";
	for(int i=0;i<str.length();i++){
		printf("%c ",str[i]);
	}
	return 0;
}

Operation results:

If you want to read and output the whole string, you can only use cin and cout

Program code:

//#include<bits/stdc++.h>
//Universal header file, which contains header files < string > and < iostream >
#Include < iostream > / / CIN and cout are in the iostream header file, not stdio h 
#include<string>
using namespace std;
int main(){
	string str;
	cin>>str;
	cout<<str<<endl;
	return 0;
}

Operation results:

 

(2) Access via iterator

Generally, only (1) can meet the access requirements, but some functions, such as insert() and erase(), require iterators as parameters, so let's learn iterator Usage of.

Define string iterators:

string::iterator it;

In this way, the iterator it can be obtained, and every bit in the string can be accessed through * it:

Program code:

#include<cstdio>
#include<string>
using namespace std;
int main(){
	string str = "abcd"; 
	for(string::iterator it = str.begin();it !=str.end();it++) {
		printf("%c",*it); 
	}
	return 0;
}

Operation results:

3. Analysis of common function examples of string

(1)operator+=

This is the addition of strings. You can splice two strings directly.

Examples are as follows:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str1= "abcd",str2="xyz",str3;
	str3=str1+str2;//Splice str1 and str2 and assign a value to str3
	str1+=str2;//Splice str2 directly to str1
	cout<<str1<<endl;
	cout<<str3<<endl;
	return 0;
}

Output result:

 

(2) length()/size()

length() returns the length of a string, that is, the number of characters stored. The time complexity is O(1). size() is basically the same as length ().

Examples are as follows:

string str="abcxyz";

printf("%d %d\n"),str.length,str.size());

Output result:

6 6

(3) insert()

insert(pos,string). Insert string at pos.

Examples are as follows:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "nihaoshijie"; 
	str.insert(2, "**");
	cout<<str<<endl;
	return 0;
}

Output result:

(4) erase()

  • Delete a single element

str.erase(it) is used to delete a single element. It is the iterator that needs to delete the element.

Examples are as follows:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(str.begin()+4);//Delete position 4 (i.e. E.)
	cout<<str<<endl; 
	return 0;
}

Output result:

 

  • Delete all elements in an interval

str.erase(pos,length), where pos is the starting position to be deleted and length is the number of characters to be deleted.

Examples are as follows:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "abcdefg";
	str.erase(3,2);//Delete 2 characters from position 3, i.e. de 
	cout<<str<<endl; 
	return 0;
}

Output result:

 

(5) clear()

clear() is used to clear the data in the string. The time complexity is generally O(1).

Code example:

#include<iostream>
#include<string>
using namespace std;
int main(){
	string str = "youdianlei"; 
	str.clear();	//Empty string 
	cout<<str.length()<<endl;
	return 0;
}

Output result:

 

(6) string::npos

string:npos is a constant with its own value of - 1, but it is unsigned_int type, so it can actually be considered unsigned_ Maximum value of type int. string:npos is used as the return value when the find function is mismatched.

(7) find()

str.find(str2). When str2 is a substring of STR, return the position where it first appears in str; If str2 is not a substring of STR, string::npos is returned.

str.find(str2,pos) matches str2 from the pos number of str, and the return value is the same as above. The time complexity is O(nm), where n and m are the lengths of str and str2, respectively.

Examples are as follows:

Output result:

Statement: the content of this article is extracted from the book "algorithm notes" edited by Hu fan and Zeng Lei.

Topics: C++ C# Algorithm