Commonly used functions in C++ written test

array

#include <vector>

push_back: add an element to the end of the vector

pop_back: delete the last element in the vector

empty: Determine whether the vector is empty, if it is empty, there is no element in the vector

size : the number of data elements in the output queue

Vector initialization related:

vector<int> test(n, 0)

vector<int> test(5, 2);

The initialization method of vector two-dimensional array:

vector<vector<bool>> test(m, vector<bool>(n, false))

vector<vector<int>> test(m, vector<int>(n, 0))

vector iterator for traversal:

nums.begin()

nums.end()

numbers.rbegin()


for (vector<int>::iterator iter = test.begin(); iter != test.end(); iter++)
{
    cout << *iter << endl;
}


queue

#include <queue>

push: insert an element at the end of the queue

pop : remove the topmost data

size : the number of data elements in the output queue

empty : Determine whether the queue is empty

front : returns the first element in the queue, but does not delete it

back : returns the last element in the queue without deleting it


deque

#include <deque>

pop_back(): delete the element at the end of the queue, no return value;

pop_front(): delete the element at the head of the queue, no return value;

emplace_back(): Add an element at the end of the queue;

emplace_front(): Add an element at the head of the queue;

front : returns the first element in the queue, but does not delete it;

back : returns the last element in the queue without deleting it;

size(): get the queue size;

empty() : Determine whether the queue is empty.


the stack

#include <stack>

s.push(); //Push into the stack

s.pop(); //Pop out the stack, just delete the top element of the stack, and do not return the element

s.top(); //Access the top element of the stack

s.empty(); //Judge the stack is empty, when the stack is empty, return true

s.size(); //Access the number of elements in the stack


hash table

There are two different types of hash tables: hash sets and hash maps .

A hash set is one of the implementations of a set data structure, used to store unique values .
A hash map is one of the implementations of a map data structure for storing (key, value) key-value pairs .

Hash map: #include <unordered_map>

unordered_map is an associative container that stores <key, value> key-value pairs, and the arrangement order of its elements is unordered.

find(key): Find the element whose value is key, if found, return a forward iterator pointing to the element; otherwise, return an iterator pointing to the position after the last element in the container;

count(key): Find the number of elements whose value is key in the container;

erase(): delete the specified element;

Hash set: #include <unordered_set>

find(key): Find the element whose value is key, if found, return a forward iterator pointing to the element; otherwise, return an iterator pointing to the position after the last element in the container;

count(key): Find the number of elements whose value is key in the container, 0 or 1 ;

insert(): add a new element to the container;

erase(): delete the specified element;

empty(): Returns true if the container is empty; otherwise, false .


most value function

#include <algorithm>

max: Compare to get the largest element.

min: Compare to get the smallest element.

min_stack.push( ::min(x, min_stack.top()) ); // ::min represents the global min function, which compares the value to be pushed with the top element of the minimum stack to obtain the minimum element.

#include <cmath>

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    long int x = -101;
    double result;

    result = abs(x);
    cout << "abs(" << x << ") = |" << x << "| = " << result << endl;

    return 0;
}


binary search

#include<algorithm>

lower_ bound(first,last,val) is used to find the position of the first element whose value is greater than or equal to val within the [first,last) range of the array or container, if it is an array, return the pointer of the position; if it is a container , an iterator for that position is returned.

upper_bound(first,last,val) is used to find the position of the first element whose value is greater than val within the [first,last) range of the array or container . If it is an array, it returns a pointer to the position; if it is a container, then Returns the sender for this position.

#include<cstdio>
#include<algorithm> 
using namespace std;
int main(){
	int a[10] = {1,2,2,3,3,3,5,5,5,5};
	//寻找-1
	int* lowerPos = lower_bound(a,a+10,-1);
	int* upperPos = upper_bound(a,a+10,-1);
	printf("%d,%d\n",lowerPos-a,upperPos-a);

	return 0;
	
}
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v = {1, 3, 5, 7, 9};
    int val = 6; // 要查找的值

    std::vector<int>::iterator it = std::lower_bound(v.begin(), v.end(), val);

    if (it != v.end()) {
        std::cout << "第一个大于等于 " << val << " 的元素位置为:" << std::distance(v.begin(), it) << '\n';
    }
    else {
        std::cout << "在容器中未找到大于等于 " << val << " 的元素\n";
    }

    return 0;
}


string string

#include <string>

Various construction methods of string:

string str1;                 //生成空字符串
string str2("123456789");    //生成"1234456789"的复制品
string str3("12345", 0, 3);  //结果为"123"
string str4("012345", 5);    //结果为"01234"
string str5(5, '1');         //结果为"11111"
string str6(str2, 2);        //结果为"3456789"

int length(); int size(); // find the length

to_string(): Convert the numbers in brackets to strings;

push_back(): add characters, not emplace_back! emplace_back() is only applicable when the type stored in the container is an object, not a basic type;

pop_back(): delete characters at the end;

substr(): returns a substring;

resize(): Change the number of characters.

#include <algorithm>

reverse: Reverse the order in the range of [first, last);

sort(): realize the sorting of data, the third parameter comp is the sorting method, and the default sorting method is sorting from small to large.

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b);
main(){
  //sort函数第三个参数自己定义,实现从大到小
  int a[]={45,12,34,77,90,11,2,4,5,55};
  sort(a,a+10,cmp);
  for(int i=0;i<10;i++)
    cout<<a[i]<<" ";
}
//自定义函数
bool cmp(int a,int b){
  return a>b;
}


math related

General term and formula of arithmetic difference and geometric sequence

Arithmetic general terms:

Arithmetic sum formula:

The formula for the general term of the ratio:

The proportional sum formula:


INT_MIN、INT_MAX、LONG_MIIN、LONG_MIN

#include <limits.h>

Guess you like

Origin blog.csdn.net/qq_41709234/article/details/131745554