1. cout << setiosflags(ios::fixed) << setprecision(n);
Header file: #include <iomanip>
cout << setiosflags(ios::fixed) << setprecision(n);
It can make the subsequent output retain n decimal places
example
cout << setiosflags(ios::fixed) << setprecision(2);
cout << 12.212;
输出:
12.21
2. standing
Header files: #include <string>
Function prototype: int stoi( const std::string& str, std::size_t* pos = nullptr, int base = 10 );
Its three parameters can be understood as stoi (string, starting position, n-ary), among which only string has no default value.
Return value:
If the conversion is successful, the stoi function will return the converted number as an int.
If there is no number in the string, an "invalid_argument" exception will be thrown;
if the number in the string is converted beyond the range of int, an "out_of_range" exception will be thrown;
example
string a;
cin >> a;
cout << stoi(a)+1;
输入:
12
输出:
13
3.sort (from big to small)
The function prototype of the sort function is: Sort(start, end, cmp)
start indicates the starting address of the array to be sorted, end indicates the next digit of the end address of the array, and cmp is used to specify the sorting method, which can be left blank and the default is ascending.
In order to make the sort function sort from large to small, we need to assign a value to the parameter cmp
Method 1: Create your own function
bool cmp(int x,int y)
{
return x > y;
}
Method 2: Use the greater<T>() function.
Header file: #include <functional.h>
sort(strat,end,greater<T>())
4. isdigit()
Header file: #include <ctype.h>
Function prototype: int isdigit(int c);
Parameter c indicates the character or ASCII code to be detected.
Return value: A return value other than 0 (true) indicates that c is a number , and a return value of 0 (false) indicates that c is not a number .
example
char a='1';
char b='x';
cout<<isdigit(a)<<endl;
cout<<isdigit(b)<<endl;
输出:
4
0
5.getline
To get a string, the first thing that comes to mind is the cin operator to input a string, but it may cause some problems:
When cin reads data, it passes through and ignores any leading white-space characters (spaces, tabs, or newlines). It starts reading as soon as it hits the first non-whitespace character, and stops when it hits the next whitespace character. In order to solve this problem, the getline function appeared. This function reads the entire line, including leading and embedded whitespace, and stores it in a string object.
Header files: #include<string>
Function prototype: getline(cin, inputLine);
where cin is the input stream being read and inputLine is the name of the string variable that receives the input string
example
//使用cin
string a;
cin >> a;
cout << a ;
输入:
beijing shanhai
输出:
beijing
//使用getline
string b;
getline(cin, b);
cout << b;
输入:
beijing shanhai
输出:
beijing shanhai
6. Precautions for using erase in vector
Erase will change the size of the container. When using the container size later, you cannot use the initial n, but use vector.size to represent the size of the container, because functions such as erase may be used to change the container during the use of the container the size of
7. setfill function
Can be used together with the setw function, so that
Example:
int a;
cin >> a;
cout << setfill('0') << setw(5) << a;//也可以把setfill('0')和setw(5)的顺序调换一下
输入:
12
输出:
00012