# Tag Info

1 vote

### I wrote a simple time measurement/compare library in C++ [TimeIt]

In the constructor, result = result / count can be rewritten as result /= count. The code mixes ...
• 8,987
Accepted

### I wrote a simple time measurement library in C++

Don't do any time conversions yourself By storing the elapsed time in microseconds, and doing integer divisions when asking for the elapsed time in seconds, you lose precision. Also, what if you want ...
• 33.4k

### TFunction: std::function replacement for event system

Design Tell us what your code does, and also add such a comment to the code. As far as I can tell, it adds equality-comparability, by dropping type-erasure, thus dropping support for all but two ...
• 17.4k

### Fast way to compute a^d mod n

Write readable code Using single-letter variable names and making the source code compact doesn't have any effect on performance, but it does reduce readability and maintainability. So try to name ...
• 33.4k
1 vote

### Minimum number of swaps to sort a array, but swaps only allowed with elements 2 apart

General Observations Because the code only partially works the question is actually off-topic on the Code Review Community, please read How do I ask a good question? before you post another question. ...
• 18.5k
Accepted

### Evaluating math terms as nested lambdas instead of expression tree

I don't see any major problems with this approach, in fact it strikes me as quite elegant! However, I notice your Cube callable/functor and your ...
• 2,261

### A C++ Job Manager Class (Rev 1)

Pass functions by value You are passing job_func by rvalue reference, but that becomes a problem when someone has an lvalue ...
• 33.4k

### Implementation of std::any

It is good exercise to try to implement std features yourself. But if you want an elegant implementation of any, check boost::any...
• 101
1 vote

### Sieve of Eratosthenes - segmented to increase speed and range

Although late I publish a double segmentation solution that uses wheel factorization, useful for large numbers with low memory problems. I propose a traditional sieve that uses two vectors in order to ...
• 111
1 vote

### Two-layer RAII to support a critical section

If you can use the standard library, but do need to use CriticalSections instead of whatever std::mutex provides, you can still ...
• 33.4k
1 vote
Accepted

### Finding the cheapest path between two points using Dijkstra

Don't use compiler-specific vector extensions unnecessarily Using [[gnu::vector_size(16)]] makes your code unportable. I don't think there is any great benefit for ...
• 33.4k
1 vote

### C++ Hash table, Hash function, benchmark vs std::unordered_set and std::hash

My hash function seems to work surprisingly well! [...] My hash function and my hash table benchmark result for std::string: (Generated 50000 random std::string ...
• 33.4k
Accepted

### C++ Connect 4 in SFML

First we have to get this out of the way: I decided to try learning a low level language for once, so I did C++. C++ is definitely a high-level language. If you want a low-level language, you should ...
• 33.4k
Accepted

### A C++ Job Manager Class

Use of std::shared_ptr In Job, you store the lock, condition variable and thread in ...
• 33.4k
1 vote

### A C++ Job Manager Class

I don't have time for a full review at the moment, but just glancing at it, one thing jumped out at me about JobManager: you don't seem to have really made up your ...
• 33.2k
1 vote

### Finding the cheapest path between two points using Dijkstra

Overview This code is very dense and hard to read. It could definitely do with some additional comments to make it more readable. Not convinced this is an accurate implementation of Dijkstra algorithm;...
• 91.7k
Accepted

### Coordinate Descent Non-negative Least Squares optimization

If I understand your code correctly, you subtract the columns of the matrix a from vector b, each column multiplied by a different scalar diff. You also add diff to an element of vector x. Basically: <...
• 2,277
1 vote

### Coordinate Descent Non-negative Least Squares optimization

Avoid branches Branches in code, like those caused by if-statements, can sometimes be expensive on modern processors. Consider the case of ...
• 33.4k

### A uniform interface for storing data by fd or container

Simplify the design Unless you plan to add many more storage options in the future, I would apply the KISS and YAGNI principles, and just create one single class that can write to both a string and a ...
• 33.4k

### Virtual method with variations for different types

Captain Hatteras has a nice answer, I just want to present another way of doing it that does not use CRTP at all, but does address your remark that: [...] conventional doReactToOwner() overrides for ...
• 33.4k
1 vote

### Virtual method with variations for different types

Small things: When dynamic_cast is used on a pointer and fails, it returns nullptr. In this case, it can't fail because you ...

### non recursive quick sort in c++

First of all, the code looks correct. Kudos. Few notes though. No naked loops. Every loop implements an important algorithm, and therefore deserves a name. In your case, the loop ...
• 53.4k
Accepted

### Boolean evaluation

Time complexity cannot be $n^3$. There are just too many ways to parenthesize the string, and the code inspects them all. I run an experiment counting a number of calls to ...
• 53.4k
1 vote

### Bogo Sort Algorithm

any ways to make this code easier to read for someone else? One important question to ask, especially where someone else includes a later self (maintenance). ...
• 4,563
Accepted

### Convert string to date C++

Unnecessary use of wide strings In your example, there is no need to use wide strings for date_time_format and ss. Just use <...
• 33.4k

### Ball movement within an area

Edward has given a very good answer, and I agree with everything he said. However, he didn't address this issue: “You need to improve the usage of cv.wait() and ...
• 33.4k
1 vote
Accepted

### Bogo Sort Algorithm

Advice 1 int seq[entryNum]; That did not compile on Visual Studio 2022 to begin with. Use: ...
• 25.7k
1 vote

### Printing contiguous sub-arrays

Your code has one bug that causes it to crash. The line std::cout << subarray[array.size()-i-1] << " "; should instead be ...
• 1,063
1 vote

### Reducing the time for rolling hash

Two more things about something like sum=sum+(int)pattern[j]-96;: - 96 is a magic number: The reader is left to figure out what it means here. ...
• 4,563

### Ball movement within an area

I see a number of things that may help you improve your program. Don't abuse using namespace std Putting using namespace std at ...
• 64.4k
1 vote

### Why my implementation of Anagram function is redundant and might have cache issues?

You are focused far too much on the one special case: s1="anag", s2="1234pgana". Consider what happens with ...
• 30.8k

### Templatizing a Timer class

I will not comment on the code itself, there is already an answer discussing the issues with the chosen clock. Instead, I want to discuss the API. The API for your class is not easy to learn to use. ...
• 4,062
1 vote
Accepted

### Why my implementation of Anagram function is redundant and might have cache issues?

I see some things that may help you improve your program. Provide a complete program Part of the interface of a function is the associated list of required #include...
• 64.4k

### Printing subarrays ⚡

All possible subarrays can be printed using one loop: ...

### Printing subarrays ⚡

There is also an recursive solution: ...
• 353

### Vectorset, a faster std::set

Enable compiler warnings and fix them My compiler warns about the wrong order of the initializer list for the constructor of vectorset::Iterator. I would simply ...
• 33.4k

### Printing subarrays ⚡

Your code is obfuscated The code you have written looks very obfuscated. For example, the outer do-loop does two things: it copies elements from ...
• 33.4k

### String formatting in C++

You've already gotten some good reviews, so I'll just cover the things not already mentioned. Eliminate unused variables Unused variables are a sign of poor quality code, and you don't want to write ...
• 64.4k
1 vote

### Hackerrank Inherited Code

maybe now is very late, but I think it is better if the username var is passed by reference. And the username should change to ...

### String formatting in C++

This style of substitution is brittle. Suppose we have a string with two placeholders for substitution: ...
• 57.9k

### Random dice class based off D&D

Here are some things that may help you improve your code. Use include guards There should be an include guard in each .h file. That is, start the file with: ...
• 64.4k

### Random dice class based off D&D

I'd say this is the wrong place to use an "OOP" approach (is there a right one? :p). I think that what the Die class represents is a bit mixed up, and not ...
• 11k
Accepted

### String formatting in C++

Keep your includes ordered. Thus, you won't lose track of them, even if there are more of them. Common modern order goes from local to universal, to ensure headers are self-contained. Exceptions are ...
• 17.4k

### String formatting in C++

General Observations You have mixed C and C++ in this program, since it is C++ there is no reason to mix the two. A thing to remember with C++ strings is that you don't have to manipulate the ...
• 18.5k
Accepted

### Storing (and retrieving) functions by indexing them by address

Don't use std::function<> if you are only going to allow plain functions I would avoid using std::function<> if your ...
• 33.4k

### Terminal calculator that saves and displays calculation history

For a first project in C++, this is looking really decent! There are still some things you can improve though, which I'll list below. Answers to your questions Is there a better/cleaner way to [Q]uit ...
• 33.4k

### Terminal calculator that saves and displays calculation history

Here are some things that may help you improve your code. Avoid singletons You probably already know about avoiding global data because it makes the program harder to understand and reason about. The ...
• 64.4k
Accepted

### Any bettter way to achieve this lookup table?

It depends what you really need. As @Aganju pointed out that looking up a hash is much faster than looping through strings and comparing. But for your case， you seems to find out whether a specific ...
• 136