Function class summary (constantly updated)

There are many functions in c + + that can play a good role in writing code. The following are some functions commonly used by the author in writing algorithm problems:

string class:

1.1 memset();

void *memset(void *s, int c, unsigned long n);

Explanation and usage:

1. s Is the array name;

2. c Is the integer number to assign;(Only the last eight bytes are taken);

4. n yes n Bytes;

memset Explanation: will s After the current position in the n For bytes ch Replace and return s. 
It should be noted that n Bytes

effect:

1. memset() function is often used for memory space initialization;

2. The profound connotation of memset(): it is used to set all a memory space to a certain character;

3. memset can easily empty variables or arrays of a structure type;

be careful:

memset Function initializes a block of memory in bytes, so it cannot be used to int Array initialized to ^^^ 0 ^^^and ^^^ -1 ^^^  Values other than
(Unless the high byte and low byte of the value are the same).

1.2 memcpy();

void *memcpy(void *destin, void *source, unsigned n)

Explanation and usage:


1. destin-- Refers to the target array used to store the copied content, and the type is cast to void* Pointer.
2. source-- Point to the data source to be copied, type cast to void* Pointer.
3. n-- The number of bytes to be copied. 


The function copies several bytes from the starting position of the source memory address to the target memory address, that is, from the source memory address source Medium copy n Bytes to
 target destin Yes.

effect:
1. Copy array;
2. In the algorithm, it is often used to save a certain state, and then restore it after operation.

1.3 strcpy();

char *strcpy(char* dest, const char *src);
Parameters: des Is the target string, source Is the original string.

1. dest Represents an empty array;

2. src Represents a space, encountered'\0'end;


3. Return value: the address of the target string,Pointer type.

Function: from src Address start with NULL The terminator string is copied to dest Start address space

There are three main differences between strcpy and memcpy.

1. The copied content is different. strcpy can only copy strings, while memcpy can copy any content, such as character arrays, integers, structs, classes, etc.
2. Replication methods are different. strcpy does not need to specify the length. It ends only when it encounters the string terminator "\ 0" of the copied character, so it is easy to overflow. memcpy determines the copy length according to its third parameter.
3. Different uses. strcpy is usually used when copying strings, while memcpy is usually used when copying other types of data.

1.4 strlen();

extern unsigned int strlen(char *s);

Parameters: s Header pointer representing a string;

Function: calculate the value of a given string( unsigned int Type) length, excluding'\0'included;
Description: Return s Length of, excluding Terminator NULL;

Function: calculate the length of string in character array;

Miscellaneous:

2.1 atoi();

int atoi(const char *nptr);

atoi (express ascii to integer)Is a function that converts a string into an integer, which is used in computer programs and office software.

int atoi(const char *nptr) The function scans the parameters nptr String, the preceding white space characters (such as spaces) will be skipped, tab Indent), etc.
If nptr Cannot convert to int perhaps nptr If it is an empty string, 0 will be returned [1]  . In particular, this function requires the string to be converted
 It is understood in decimal numbers.

atoi The number corresponding to the input string has a size limit (and int Type (related to size). If it is too large, an error may be reported-1. 

Function: read string, remove tab and space.

2.2 reverse()

1.reverse function reverses string

string N;
cin>>N;
reverse(N.begin(), N.end());//begin, and end;

2. The reverse function reverses the character array

char s[101];
cin.getline(s,sizeof(s));      //You can also not use cin.getline
int m=strlen(s);
reverse(s,s+m);
puts(s);

3. The reverse function reverses the integer array

int a[100];
reverse(a,a+10);         //The second parameter is the next address of the last element of the array;

2.2map and unordered_map

map:
The header file is #include < Map >
Inside the map is a red black tree (non strictly balanced binary tree). The red black tree has the function of automatic sorting, so all elements in the map are orderly, and each node of the red black tree represents an element of the map. Therefore, the operations of inserting, deleting and searching map are equivalent to the operations of red black tree. Finally, according to the middle order traversal of the tree, the key values can be traversed out in order of size.

unordered_map:
Header file #include < unordered_ map>
unordered_ Inside the map is a hash table (hash table, which maps key values to a position in the hash table, so the search time complexity is O(1)), and the arrangement order of elements is out of order.

1. When applied to problems requiring sequence, it will be more efficient to use map.
2. For frequently searched problems, use unordered_map is more efficient.
The details can be seen here

Tags: C C++

Posted on Sat, 18 Sep 2021 00:00:21 -0400 by mooshuligan