web123456

Blue Bridge Cup c++ group commonly used tips

 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 

  1. #include<bits/stdc++.h>// Universal header file
  2. using namespace std;// Namespace
  3. typedef long long LL;// Use the typedef keyword to define your own customary datatype names
  4. int main(){
  5. return 0;
  6. }

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

  1. //Character order issues (hashing)
  2. // Attach order to the 26 letters of the alphabet
  3. // Solve the problem of who comes first and who comes last with characters
  4. for (int i = 1; i <= 26; i++) {
  5. char ch;
  6. cin >> ch;
  7. a[ch - 'a'] = i;
  8. }

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:

  1. for (auto it = nums.begin();it != nums.end();it++)
  2.     {
  3.     cout << it->first << it->second ;
  4.     }

set output:

  1. //Orthogonal output from smallest to largest map is autosorted
  2. for (set<int>::iterator it = res.begin(); it != res.end(); it++) {
  3.         cout << *it << endl;
  4. }
  1. //Reverse order output
  2. for(auto iter = mapStr.rbegin(); iter != mapStr.rend(); ++iter) {
  3. undefined
  4. cout<<iter->second.c_str()<<endl;
  5. }

Number of fixed characters in the string

  1. #include <iostream>
  2. #include <algotirhm>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7. string temp = "aaabcdaaa!!!";
  8. int num = count(temp.begin(),temp.end(),'a');
  9. cout <<"In the string" << temp << "Medium." <<"The number of times the letter a occurs is" << num << endl;
  10. return 0
  11. }

Conversion between systems

  1. // Decimal to hexadecimal
  2. int num = 10;
  3. char str[100];
  4. itoa(num, str, 16);
  5. printf("%s\n", str);
  6. return 0
  1. // Hexadecimal to decimal
  2. char str[30] = "2030300 This is test";
  3. char *ptr;
  4. long ret;
  5. ret = strtol(str, &ptr, 10);
  1. //Decimal to Octal Hexadecimal
  2. string s1,s2;
  3. int a=30;
  4. stringstream ss;
  5. ss<<oct<<a; //Octal to octal read into the stream and output as a string
  6. ss>>s1; // Here also string s(());
  7. cout<<s1<<endl; //Output: 36
  8. ss.clear(); //Not emptied may be an error
  9. ss<<hex<<a; //Octal to hexadecimal read into the stream and output as a string
  10. ss>>s2;
  11. cout<<s2<<endl; //Output: 1e