C/C ++ string processing
Strncpy (A, B, 5 );
A [5] = '\ 0 ';
Char A [10];
Memset (A, '#', sizeof ());
A [10] = '\ 0 ';
C:
Char st [100];
1.String Length
Strlen (ST );
2.String comparison
Strcmp (ST1, st2 );
Strncmp (ST1, st2, N );
SetST1, st2BeforeN.
3.Additional
Strcat (ST1, st2 );
Strncat (ST1, st2, n); nIndicates connectedSt2BeforeNPacketsST1, Do not add'\ 0'.
4.Replace
Strcpy (ST1, st2 );
Strncpy (ST1, st2, n); nIndicates copyingSt2BeforeNPacketsST1, Add'\ 0'.
5.Search
Where = strchr (St, CH) CHIs the character to be searched.
Where = strspns (ST1, st2 );
Search for strings.
Where = strstr (ST1, st2 );
C ++:
# Include <string>
String STR;
1.String Length
Len = Str. Length ();
Len = Str. Size ();
2.String comparison
Can be directly compared
Yes.:
Str1.compare (str2 );
Str1.compare (pos1, len1, str2, pos2, len2 );
The value is negative,0, Positive.
NOPsLength to the end.
3.Additional
Str1 + = str2;
Or
Str1.append (str2 );
Str1.append (str2.pos2, len2 );
4.String Extraction
Str2 = str1.substr ();
Str2 = str1.substr (pos1 );
Str2 = str1.substr (pos1, len1 );
String A = S. substr (0, 4 );//Returns a string.SFrom0The length starting from the bit is4String
5.String search
Where = str1.find (str2 );
Where = str1.find (str2, pos1); pos1YesfromStr1The first digit.
Where = str1.rfind (str2 );
Search from the back.
6.Insert string
It is not a value assignment statement.
Str1.insert (pos1, str2 );
Str1.insert (pos1, str2, pos2, len2 );
Str1.insert (pos1, numchar, char); numcharIs the number of inserts,CharIs the character to be inserted.
7.Replace string
Str1.replace (pos1, str2 );
Str1.replace (pos1, str2, pos2, len2 );
8.Delete string
Str. Erase (Pos, Len)
Str. Clear ();
9.Exchange string
Swap (str1, str2 );
10. c --> C ++
Char * CSTR = "hello ";
String str1;
CSTR = CSTR;
String str2 (CSTR );
Sprintf application: print the number to a string
Char s [];
Int;
Double B;
Sprintf (S, "% d", );
Sprintf (s, "%. 2lf", B );//SetBAccurate to the second decimal point, and printedSMedium
Character string left cyclic shift
String A, C;
Lena = A. Length ();
For (I = 0; I <Lena; I ++)
{
C = A. substr (I );//Slave locationIStart to copy the string to the end and cycle to the left
C. append (A, 0, I );//Returns the stringA, From location0Start copyingITo StringC
Cout <C <Endl;
}
Number of decimal places
C:
Double ans;
Print ("%. 2lf \ n", ANS );
% Lf double % F float % d int % LD long % C char
C ++:
# Include <iomanip>
Double ans
Cout <fixed <showpoint <setprecision (2) <ans <Endl;
Format alignment. c ++ is right alignment by default.
Cout. Width (20 );
Cout <S <Endl; // The result is right aligned.
Cout. Width (20 );
Cout. SETF (ios_base: Left );
Cout <S <Endl; // The result is left aligned.
C Input and Output
Int A, B;
Scanf ("% d", & A, & B );
Printf ("% d", & A, & B );
Math function library
# Include <math. h>
Evaluate the square root double C = SQRT ();
The function POW (X, Y) is used for y times of x. x is double, and Y is int.
Open X to the power of y pow (x, 1/y)
About map container
# Pragma warning (Disable: 4786)
# Include <map>
Map <string, int> m;
String A = "hello ";
If M [a] is not assigned a value, M [a] = 0;
Map <string, string> m;
If M [a] is not assigned a value, M [a] = "";
Determine Prime Number
1. From 2 to SQRT (x) sort method
Bool Jude (int x) {If (x = 1 | x = 0) return false; double M = SQRT (double) X ); for (INT I = 2; I <= m; I ++) if (X % I = 0) return false; return true ;}
2. Screening Method for Determining prime numbers
Bool isprime [1000000 + 5] memset (isprime, true, sizeof (isprime); isprime [0] = isprime [1] = false; for (I = 2; I <1000000 + 5; I ++) {If (isprime [I] = true) {temp = 2 * I; while (temp <1000000 + 5) {isprime [temp] = false; temp + = I ;}}}
Sort () function usage
# Include <algorithm> bool CMP (int A, int B) {return A> B;} int A [10]; sort (A, A + 10, CMP ); stable_sort (A, A + 10, CMP); // The stable sorting method applies to any A [I], a [J] (I <j ), where a [I] = A [J], after sorting, a [I] must appear before a [J], the sorting is considered stable.
Stack and queue
1. About Stack
# Include <stack> stack <int> S; S. Push (a); While (! S. Empty) {cout <S. Top (); S. Pop () ;}int size = S. Size ();
2. About queue
# Include <queue> queue <string> q; q. Push (s); While (! Q. empty () {cout <q. front (); q. pop ();} cout <"the first element is" <q. front () <"and the last element is" <q. back () <Endl; int size = Q. size ();
Priority_queue
# Include <queue>
Empty () true if the priority queue has no elements
Pop () removes the top element of a priority queue
Push () adds an element to the end of the priority queue
Size () returns the number of items in the priority queue
Top () returns the top element of the priority queue
Priority_queue <node, vector <node>, greater <node> q;
// Greater is arranged in ascending order
About the overrading of greater, you must write, greater uses operator>
Bool operator> (node)
{
Return A. x> B. X;
}
// Less is arranged in ascending order.
Priority_queue <node> q;
Priority_queue <node, vector <node> q;
Both methods can be used.
By default, less functions are relatively large to small. Operator is used when less functions are overloaded. <
Never forget Initialization Especially for loop input data For stack queue Empty () must be taken into account. If Stack or queue is empty, S. Top () or Q. Top () cannot be used again. Stack or queue is left empty for each new loop
Query the set to find the root
1. Do not change the value of the Set Element
Int findr (int x) {int r = x; while (U [R]! = R) r = U [R]; return r ;}
2. Change and check the set element values so that each element points directly to the root, which will facilitate future search.
Int findr (int r) {If (U [R] = r) return r; U [R] = findr (U [R]); Return U [R];}
Deep priority (DFS) Search
// Search for the matrix G [] [] From X and Mark void DFS (int x) {visit [x] = 1 with visit []; for (Int J = 1; j <= N; j ++) if (visit [J] = 0 & G [x] [J] = 1) DFS (j );}
Dijkstra finds the single-source shortest path
If Dijkstra has multiple starting points or multiple ending points, you can add two more points to connect all the starting points and all the ending points of the other link, and set the value between the two points to zero.
# Define Max int_maxint G [200 + 2] [200 + 2]; int dis [200 + 2], pre [200 + 2]; bool set [200 + 2]; // template function int Dijkstra (int x, int y, int citynum) {int I, j; for (I = 1; I <= citynum; I ++) {dis [I] = G [x] [I]; set [I] = false; If (DIS [I] = int_max) Pre [I] = 0; else pre [I] = s;} dis [x] = 0; set [x] = true; for (I = 1; I <citynum; I ++) {int temp = max; int u = x; For (j = 1; j <= citynum; j ++) if (! Set [J] & dis [J] <temp) {u = J; temp = dis [J];} set [u] = true; For (j = 1; j <= citynum; j ++) if (! Set [J] & G [u] [J] <max) {int newdis = dis [u] + G [u] [J]; if (newdis <dis [J]) {dis [J] = newdis; Pre [J] = u ;}} return dis [y];}