1.Blue Bridge Cup ---- Math ----- Dynamic Programming Notes
2.C++ Blue Bridge Cup basics commonly used to organize
Continuously updated -------------------------------------------
catalogs
Commonly used start templates
Interchange between int and string
Bitwise conversion of numeric strings
Usage of typedef
Simple use of hashes
Inputs and outputs of common containers
Number of fixed characters in the string
Conversion between systems
Common Starttemplates
-
#include<bits/stdc++.h>// Universal header file
-
-
using namespace std;// Namespace
-
-
typedef long long LL;// Use the typedef keyword to define your own customary datatype names
-
-
int main(){
-
-
return 0;
-
}
int andstring interoperability
string to int.
This is the most common General int num =stoi(s) to int type
and long long num = stol(s); long long num = stoll(s).
Similarly, if you want to convert to a float, you have double num = stod(s) float num = stof(s).
Note that only the sto remains the same, and the letters after that change according to the type of conversion, which is good to remember.
int to string:
This is just a direct string num = to_string(num) Simple and violent hahaha
Bitwise conversion of numeric strings
When it comes to strings, such as scientific notation, etc., the default number of significant digits for floating-point types is 7. If it is too large, it will be converted to a scientific notation type, such as 1.2e+10, so what if we need to display all the digits?
You need to use setprecision(8) to write the number of digits you want to display, such as 8 for eight digits.
It could look like this cout<<setprecision(8)<<b1; b1 is one of my floating point constants
Also, if you want to keep the exact number of decimal places, you need to do this.
(ios::fixed);//set the precision of retaining the decimal point
cout<<setprecision(2)<<b1;
Put a statement in front of it that preserves the precision of the decimal point, and you're done.
You can try it out, little buddy.
Usage of typedef
I've been working on some questions and found that the most common usage of typedef is
typedef long long ll;
What's the point? It's to make it easier to define constants (i.e., to steal chickens hahahahaha) and to write less code.
For example, you can
ll num;//This is a long long variable.
Simple use of hashes
-
//Character order issues (hashing)
-
// Attach order to the 26 letters of the alphabet
-
// Solve the problem of who comes first and who comes last with characters
-
for (int i = 1; i <= 26; i++) {
-
char ch;
-
cin >> ch;
-
a[ch - 'a'] = i;
-
-
}
Inputs and outputs of common containers
The inputs are pretty much the same.vectorwith push_back();
set with insert().
pari with make_pair()
vector outputs the same as an array
pair output:
-
for (auto it = nums.begin();it != nums.end();it++)
-
{
-
cout << it->first << it->second ;
-
}
set output:
-
//Orthogonal output from smallest to largest map is autosorted
-
for (set<int>::iterator it = res.begin(); it != res.end(); it++) {
-
cout << *it << endl;
-
}
-
//Reverse order output
-
for(auto iter = mapStr.rbegin(); iter != mapStr.rend(); ++iter) {
-
undefined
-
cout<<iter->second.c_str()<<endl;
-
}
Number of fixed characters in the string
-
#include <iostream>
-
#include <algotirhm>
-
#include <string>
-
using namespace std;
-
int main()
-
{
-
string temp = "aaabcdaaa!!!";
-
int num = count(temp.begin(),temp.end(),'a');
-
cout <<"In the string" << temp << "Medium." <<"The number of times the letter a occurs is" << num << endl;
-
return 0 ;
-
}
Conversion between systems
-
// Decimal to hexadecimal
-
int num = 10;
-
char str[100];
-
itoa(num, str, 16);
-
printf("%s\n", str);
-
return 0
-
// Hexadecimal to decimal
-
char str[30] = "2030300 This is test";
-
char *ptr;
-
long ret;
-
ret = strtol(str, &ptr, 10);
-
//Decimal to Octal Hexadecimal
-
string s1,s2;
-
int a=30;
-
stringstream ss;
-
ss<<oct<<a; //Octal to octal read into the stream and output as a string
-
ss>>s1; // Here also string s(());
-
cout<<s1<<endl; //Output: 36
-
ss.clear(); //Not emptied may be an error
-
ss<<hex<<a; //Octal to hexadecimal read into the stream and output as a string
-
ss>>s2;
-
cout<<s2<<endl; //Output: 1e