New answers tagged

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 ...
user avatar
  • 8,987
4 votes
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 ...
user avatar
  • 33.4k
6 votes

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 ...
user avatar
  • 17.4k
2 votes

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 ...
user avatar
  • 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. ...
user avatar
  • 18.5k
5 votes
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 ...
user avatar
2 votes

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 ...
user avatar
  • 33.4k
0 votes

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...
user avatar
  • 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 ...
user avatar
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 ...
user avatar
  • 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 ...
user avatar
  • 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 ...
user avatar
  • 33.4k
2 votes
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 ...
user avatar
  • 33.4k
0 votes
Accepted

A C++ Job Manager Class

Use of std::shared_ptr In Job, you store the lock, condition variable and thread in ...
user avatar
  • 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 ...
user avatar
  • 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;...
user avatar
  • 91.7k
2 votes
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: <...
user avatar
  • 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 ...
user avatar
  • 33.4k
4 votes

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 ...
user avatar
  • 33.4k
2 votes

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 ...
user avatar
  • 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 ...
user avatar
2 votes

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 ...
user avatar
  • 53.4k
2 votes
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 ...
user avatar
  • 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). ...
user avatar
  • 4,563
3 votes
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 <...
user avatar
  • 33.4k
2 votes

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 ...
user avatar
  • 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: ...
user avatar
  • 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 ...
user avatar
  • 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. ...
user avatar
  • 4,563
2 votes

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 ...
user avatar
  • 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 ...
user avatar
  • 30.8k
2 votes

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. ...
user avatar
  • 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...
user avatar
  • 64.4k
-1 votes

Printing subarrays ⚡

All possible subarrays can be printed using one loop: ...
user avatar
0 votes

Printing subarrays ⚡

There is also an recursive solution: ...
user avatar
  • 353
2 votes

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 ...
user avatar
  • 33.4k
2 votes

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 ...
user avatar
  • 33.4k
6 votes

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 ...
user avatar
  • 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 ...
user avatar
8 votes

String formatting in C++

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

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: ...
user avatar
  • 64.4k
2 votes

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 ...
user avatar
12 votes
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 ...
user avatar
  • 17.4k
7 votes

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 ...
user avatar
  • 18.5k
3 votes
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 ...
user avatar
  • 33.4k
2 votes

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 ...
user avatar
  • 33.4k
3 votes

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 ...
user avatar
  • 64.4k
2 votes
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 ...
user avatar
  • 136
3 votes
Accepted

AEC-to-WebAssembly compiler in C++

How to create an alias for an enum I see you are declaring 5 constants, like i32, to avoid having to write out a whole ...
user avatar
  • 33.4k

Top 50 recent answers are included