Details frequently encountered programming

1、输入输出
#include<iostream>
using namespace std;
int n;
cin>>n;
cout<< n<<endl;

String
Note: cin extraction always space (spaces, tabs, line breaks ...) be deemed terminated to extract value, extract string means always extract a single word, not the whole phrase or sentence.
cin >> s

String input using the entire line getline ()
String S;
the while (getline (CIN, S)) {

}

String flow
#include <sstream>
the stringstream

Formatting output
comprising header file: #include <The iomanip>
COUT << setiosflags (iOS :: Fixed) << setPrecision (2); together mean that the output of a floating point number right-aligned two decimal places.

1) Use setprecision (n) may control the output stream of the number of digits shown float C ++ default output value valid bit stream is 6. setprecision (n) is the output of the number n, there will be rounded.
2) setiosflags (ios :: fixed)
setPrecision (n-) and setiosflags (ios :: fixed) combination, can control the number of digits to the right of the decimal point.

eg:
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main() {
double s=20.7843000;
cout << s << endl;
cout << setiosflags( ios::fixed );
cout << "setprecision( 1 )"<< setprecision( 1 )<< s << endl;
cout << "setprecision( 2 )"<< setprecision( 2 )<< s << endl;
cout << "setprecision( 3 )"<< setprecision( 3 )<< s << endl;
cout << "setprecision( 4 )"<< setprecision( 4 )<< s << endl;
cout << "setprecision( 5 )"<< setprecision( 5 )<< s << endl;
cout << "setprecision( 6 )"<< setprecision( 6 )<< s << endl;
cout << "setprecision( 7 )"<< setprecision( 7 )<< s << endl;
cout << "setprecision( 8 )"<< setprecision( 8 )<< s << endl;
return 0;

}

Output:

20.7843
setprecision( 1 )20.8
setprecision( 2 )20.78
setprecision( 3 )20.784


2 to convert the integer to a string
int n-;
String S = the to_string (n-);

3, substr usage

Suppose: S String = "0123456789";
String SUB1 = s.substr (5); // only a subscript numeral 5 denotes from the beginning until the end of 5: SUB1 = "56789"
String s.substr SUB2 = (5, 3); // index of from 5 to intercept a length of 3: sub2 = "567"


4, two-dimensional array is defined dynamically DP [m] [n-]
. 1) using Vector
Vector <Vector <int>> DP (m, Vector <int> (n-)); // default initialized to 0
Vector <Vector <int> > dp (m, vector <int > (n, 1)); // initializes all 1

5, pointer

//申请空间
int ** p=new int *[rows];
for(int i=0; i<rows; i++) {
     p[i]=new int[columns];
}

// free up space
for (int I = 0; I <rows; I ++) {
    Delete [] P [I];
}
Delete [] P;

 

6, the pointer initialize
int * pia = new int [10 ]; // initializes each element are not
int * pia2 = int new [10 ] (); // for each element is initialized to zero

 

7,
when the sqrt use is often required to force the type of conversion, as sqrt supports only double and float type
c = (int) sqrt (( double) a * a + b * b); or c = (int) sqrt (( float) a * a + b * b) ;

 

8, met to consider issues related to the string hash table
character encountered in a string of ASCII code between 0-127


9, maximum and minimum values represent
#define INT_MAX 0x7fffffff
#define INT_MIN 0x80000000


10, create a pair of
Queue <pair <int, int >> Q;
q.push ({i, J});
q.push (the make_pair (I, J));
Create pair of two different spelling {i, j} and make_pair (i, j) can be

 

Guess you like

Origin www.cnblogs.com/sunshine1218/p/12507291.html