As we all know, struct in C++ comes from the historical legacy of the C language. Except that the default scope is public, it is no different from class. In theory, it can be mixed. Here we use the standard library and engineering practices such as Unreal to summarize the selection of struct and class keywords. If the type contains non-public member variables, class should be selected. If the type contains virtual functions, class should be selected. If the type can be aggregate-initialized, struct should be considered. If the type's construction, destruction and assignment functions are = default; implementation

Read more

This article was generated entirely by ChatGPT. std::function and std::any adopt the implementation method of constructing new objects and exchanging them, which also has some other advantages and benefits. For example, this implementation can avoid the assignment operator (operator=) throwing exceptions, thus ensuring the exception safety of assignment operations. For example, if std::function and std::any implement assignment by directly calling the assignment operation, then if an exception is thrown during the assignment operation, the state of the object may change, resulting in abnormal termination of the program. The implementation method of constructing new objects and exchanging them can avoid exceptions thrown during assignment operations.

Read more

First, here is an excerpt from the description of this part on cppreference. Variables declared at block scope with the static or thread_local specifier have static or thread storage duration, but are not initialized until control first passes through their declaration. In all subsequent calls, the declaration is skipped. If initialization throws an exception, the variable is not considered initialized, and initialization is attempted again the next time control passes through the declaration. The behavior is undefined if initialization recursively enters the block of the variable being initialized. The initialization of static local variables inside a function is performed when the function is first called, and will not be initialized in subsequent calls. In a multi-threaded environment, static layout can still be guaranteed.

Read more

Function signature reference documentation: new – delete Versioned replaceable allocation functions and replaceable deallocation functions are implicitly declared in each translation unit even if the standard library header file is not included. Versioned replaceable allocation functions and replaceable deallocation functions are replaceable: a user-supplied non-member function with the same signature defined anywhere in the program and in any source file replaces the default version. Its declaration does not need to be visible. This means that when we replace the global new/delete functions, we do not need to declare them in .h, we only need to define them in .cpp. For a function that can be replaced, if multiple replacements for it are provided in the program, or it has inl

Read more

When implementing TypeTraits, I discovered that std::is_scoped_enum of the standard library will not be officially installed until C++23, so I found a clever alternative by looking through the source code of Unreal Engine 4, similar to the following implementation: NAMESPACE_PRIVATE_BEGIN uint8(&Resolve(int64 ))[2]; uint8 Resolve(…); template struct TIsEnumConvertibleToInt : TBoolCons

Read more

Locking principles and rules 10.1 When multi-threaded processes access shared resources in parallel, they must be locked to protect shared resources including global variables, static variables, shared memory, files, etc. It is recommended to encapsulate an object like a smart pointer to manage the lock. For example, we encapsulate an auto_lock, apply for the lock during construction, and release the lock during destruction to ensure that you will not forget to unlock it. Rule 10.2 The lock has a single responsibility. Each lock only locks a unique shared resource. In this way, the lock application can be guaranteed to be single, and the locking scope can be better ensured to be as small as possible. For shared global resources, there should be one lock for each type or resource according to actual needs. In this way, this lock only locks the code that accesses this resource. Usually such code will be

Read more

Jump to the question with one click Question description There are $n$ tasks that need to be processed on the machine, and they form a sequence. The tasks are numbered $1$ to $n$, so the sequence is arranged as $1, 2, 3 \cdots n$. These $n$ tasks are divided into several batches, and each batch contains several adjacent tasks. Starting from time $0$, these tasks are processed in batches, and the time required to complete the ii-th task alone is $T_i$. Before each batch of tasks starts, the machine needs to start up time $s$, and the time required to complete this batch of tasks is the sum of the time required for each task. Note that the same batch of tasks will be completed at the same time. The cost of each task is its completion time multiplied by a fee

Read more

Luogu P4781 problem solving formula inline int FastPow(int x, int y) { if (y == 1) return x; if (!y) return 1; int tmp = FastPow(x, y >> 1) % mod; return tmp * tmp % mod * (y & 1 ? x : 1) % mod; } inline void Lagrange() { go(i, 1, n, 1) { up = down = 1; go(j, 1,

Read more

Use Tarjan algorithm lli fa[maxn]; lli dfn[maxn]; lli low[maxn]; bool cut[maxn]; void tarjan(lli u) { dfn[u] = ++cnt; low[u] = dfn[ u]; lli child = 0; for (lli i = 0; i < mp[u].size(); ++i) { lli v = mp[u][i]; if (!dfn[v]) { ++child; fa[v] = u; tarjan(v);

Read more

It is agreed that the edge storage method is a directed edge from the left to the right, and the left and right point numbers are the same. Hungarian algorithm lli mch[maxn]; lli vis[maxn]; bool dfs_hun(lli u, lli dfn) { if (vis[u ] == dfn) return false; vis[u] = dfn; for (lli i = head[u]; ~i; i = edge[i].nxt) { lli v = edge[i].v; if ( mch[v] == 0 || dfs_hun(mch[v], dfn)) { mc

Read more

lli n; lli arr[maxn]; lli lg[maxn]; lli st[maxn][32]; inline lli flg(lli x) { if (lg[x]) return lg[x]; lli tmp = x; lli res = 0; while (tmp) tmp >>= 1, ++res; return lg[x] = res – 1; } inline void init_st() { for (lli i = 1; i <= n; ++ i) st[

Read more

The property of the portal in the question: For all subtrees obtained after deleting the center of gravity, the number of nodes does not exceed 1/2 of the original tree. A tree has at most two centers of gravity; 2. The sum of the distances from all nodes in the tree to the center of gravity is the smallest. If there are two centers of gravity, then the sum of their distances is equal; two trees are merged through an edge, and the new center of gravity is on the path of the two centers of gravity of the original tree; when a leaf node is deleted or added to a tree, the center of gravity can only move by one edge at most; a tree can have at most Two centers of gravity, adjacent to each other. The idea is that if you find that there is only one center of gravity, then just delete the directly connected edge of one center of gravity and add it back. If two centers of gravity are found, then if you find a direct connection point on one of the centers of gravity and not the other center of gravity, just delete the other one. How to find the center of gravity of a tree? Choose one first

Read more

Original question link https://codeforces.com/contest/1406/problem/D In the example of the idea, a=2,-1,7,3; the difference is -3,8,-4; Suppose (b[1]= x)+(c[1]=y)=a[1]; ∵b[1]=c[2]>=…>=c[n] x+(y-3)=(x-1)+( y-2)=(x+1)+(y-4)=…=a[2] ∴b[2]=xc[2]=y-3 is the optimal solution. Other solutions will lead to b[n] Or c[1] becomes larger so that the final answer is not the smallest, that is: when the difference > 0, change the difference

Read more

Article 01: Treat C++ as a language federation. C++ is divided into: C part, object C++ part, template C++ part, and STL part. Article 02: Try to replace #define with const, enum, and inline const: means that the modified content cannot be changed. enum: The essence is int type. inline: When inside a class, if the function is allowed, it is automatically enabled. Item 03: Use const whenever possible. This helps the compiler better optimize the program and allows customers to reduce misoperations. When passing values ​​to functions, it is best to use const references for custom types. Use mutable to fix

Read more

dfs (the last few digits of the number, various restrictions, the current number) if the last digit return return value under various restrictions local variable ct = number of the current digit local variable sum = 0; for i = 0 to ct-1 sum+=There must be no unlimited number of legal states when the current bit is i. Sum+=The number of legal states that meet the current limit when the current bit is i. If the restriction condition is no longer satisfied according to ct update, then return sum return sum+dfs(current bit The next few digits, the updated constraints, the next digit) slv (current number) if (only one digit) return the corresponding contribution local variable ct; f

Read more

Prepare to download the source code download address: http://www.wxwidgets.org/downloads/ Select Source Code > Windows 7z to download and decompress the source code. Decompress wxWidgets. Compile the source code and use VS to open wxWidgets-XXX\build\msw\wx_vc15.sln. Select Debug and DLL respectively. Debug, DLL Release, Release and then click Generate > Generate Solution. Wait for the compilation to be completed. The sample is ready to create a new empty project, create Main.cpp, and copy the official

Read more

Annotations in the header of the source file list: copyright, author, date of writing, and description. Do not exceed 80 characters in width per line. Example: /*********************************************** ** Copyright:Call_Me_Why Author:why Date:2010-08-25 Description:Something about C++ ****************************** **********************/ Function header comments list: function purpose/function, output

Read more