Skip to main content
Share Your Experience: Take the 2024 Developer Survey
39 votes
Accepted

Thread Pool C++ Implementation

Funny how the universe works - I just finished my own implementation of a thread pool (albeit in C++17), and it looks a lot like yours. I found this question on the front page when I went to post my ...
osuka_'s user avatar
  • 517
39 votes
Accepted

Find nth Fibonacci Number, using iteration and recursion

Why do most people (on the internet) recommend using recursion because it's simpler and easier to write the program? Logically I thought that we should write it in a way that is fast and simple. This ...
Eric Lippert's user avatar
  • 14.9k
36 votes
Accepted

Banking system in C++

Overview The code is OK. I found 1 bug. The encapsulation is slightly lacking. You have Account as a class, which is good, but the getters/setters are not required and you have better methods to ...
Martin York's user avatar
  • 95.2k
26 votes
Accepted

Tic Tac Toe in C++14

Here are some observations and suggestions that may help you improve your program. Check your spelling If you run a spell check on your comments, you'll find a number of things such as "peice" ...
Edward's user avatar
  • 66.4k
25 votes
Accepted

Simple NAPI wrapper around self written C++ msgpack, horrible performance

Profiling: We could use a library like Tracy to do the sort of profiling you mention in the question. But it's quicker to ask "what's taking the most time" and get an answer with a profiler ...
user673679's user avatar
  • 11.8k
17 votes

A dice game called "Greed"

Style You're not being charged by the character; there's no need to abbreviate "count", or "ret" (which I would call "score" instead). Also, main has inconsistent brackets with the rest of the ...
Errorsatz's user avatar
  • 1,354
17 votes
Accepted

TString - An implementation of std::string

Justify text. (.rjust, .ljust, center) That is becoming a "fat interface". These functions should be generic algorithms that can work with any string class, including yours, ...
JDługosz's user avatar
  • 11.4k
15 votes

Implement an algorithm to determine if a string has all unique characters

If I'd asked an interview question, and got this as an answer (at least as the first answer) I think my immediate reaction would be to consider it excessively clever. To make much sense, along with ...
Jerry Coffin's user avatar
  • 33.6k
15 votes
Accepted

"contains" function for STL containers

If you like new features, and even experimental features, you can make your code a lot cleaner. Concepts A lot of those arcane SFINAE techniques will be obsolete once ...
papagaga's user avatar
  • 5,757
15 votes
Accepted

Ring Buffer Implementation in C++14

Coding idiomatically Remember that #pragma once isn't standard (unlike include guards). If you only intend your class works on major compilers, you can use ...
Calak's user avatar
  • 2,331
14 votes

128-bit unsigned integer

std::uint64_t is consistently misspelt throughout the code, and may be a poor choice anyway (since an exact 64-bit type need not be provided). It's better to use ...
Toby Speight's user avatar
  • 76.2k
14 votes

Thread Pool C++ Implementation

@osuka_ and @anderas gave some very good advice. I just have a couple things I want to add: Macros #define MAX_THREADS std::thread::hardware_concurrency() - 1; ...
Kevin's user avatar
  • 241
14 votes
Accepted

Porting C-style socket to CPP class

Avoid using malloc() in C++ There are much better ways to allocate memory in C++ that are simpler and safer. First, in C++ you should use ...
G. Sliepen's user avatar
12 votes
Accepted

Implementation of the linked list data structure in C++

Namespace #ifndef LINKEDLIST_H #define LINKEDLIST_H The term "linked list" is a common term. So I would expect somebody out there is already using the macro <...
Martin York's user avatar
  • 95.2k
12 votes

Thread Pool C++ Implementation

You've received good style advice so far. But your pool doesn't work. So let's try to address that. 1. 'resize' is both wrong and not needed. As others mentioned, it's not thread safe. Making it so ...
Sam's user avatar
  • 221
11 votes
Accepted

Sorting algorithms - Bubble sort

I recommend putting your functions into a namespace. for (auto i = begin + 1; i != end; ++i) { By doing begin + 1, you ...
Justin's user avatar
  • 3,295
11 votes

Sorting algorithms - Bubble sort

std::swap(val1, val0); This hard codes std::swap as the function to use for swapping. C++ also has a powerful (and sometimes ...
Daniel Jour's user avatar
  • 1,581
11 votes

Sorting algorithms - Bubble sort

Speed Of course we all know bubble sort is one of the slowest sorting algorithms1. It turns out, however, that stopping when an iteration is completed without a swap usually adds more overhead than ...
Jerry Coffin's user avatar
  • 33.6k
11 votes
Accepted

Advent of Code 2017, Day 1: Sum of digits matching the next digit (circular list)

I think std::rotate can lead to a somewhat simpler approach: get the input create a copy rotate the copy by one element Accumulate the sum of elements that match ...
Jerry Coffin's user avatar
  • 33.6k
11 votes

Solution to Project Euler Problem #37 in C++: Truncatable Primes

Warnings It is usually a good idea to compile your code with all warnings enabled to see if anything is wrong. In your case, nothing really worrying but we may as well fix it. ...
SylvainD's user avatar
  • 29.2k
11 votes
Accepted

Benchmarking insertion sort

Your initial claim sounds about right to me, since for each iteration, checking at most cutoff elements for the insertion_point ...
Emma X's user avatar
  • 423
10 votes
Accepted

Simple log writer class

The code is pretty good, and quite readable. Here are some of the things I noticed: Header guards Your Logger.h needs header guards: ...
Justin's user avatar
  • 3,295
10 votes

Sorting algorithms - Bubble sort

I'll add this since nobody mentioned it: the code will cause undefined behavior on empty ranges. Th reason is that i != end will not properly work since ...
Incomputable's user avatar
  • 9,504
10 votes

"contains" function for STL containers

Rather than writing your detection from scratch, use the detection idiom, either in the compiler's header if you have it, or a copy of the published definitions otherwise. ...
JDługosz's user avatar
  • 11.4k
10 votes

"contains" function for STL containers

Wrong usage One shouldn't be afraid of manual SFINAE, but one should also use it appropriately. I'm in no way professional in this question, but guidelines are roughly the following for SFINAE and ...
Incomputable's user avatar
  • 9,504
10 votes

Efficiently counting rooms from a floorplan (version 2)

Unfortunately, it does not work correctly. The output is 0 (zero rooms) for the floor plan 4 6 ###### #.#..# #....# ###### After parsing the second row, the room ...
Martin R's user avatar
  • 23.3k
10 votes
Accepted

Efficiently counting rooms from a floorplan (version 2)

This is called connected component analysis or labeling in the image processing literature. There are many algorithms that try to make this more efficient. You’re only counting regions, not assigning ...
Cris Luengo's user avatar
  • 5,688
10 votes
Accepted

An ASCII histogram of a die rolled n times

The roll function generates a random sequence of integers. The loop body shows me that you know how to seed a pseudo-RNG with a true source of randomness but you're ...
Indiana Kernick's user avatar
10 votes
Accepted

Count the frequency of integers in an array

Names All-caps names are typically reserved for macros. They don't seem to me to make much sense for const variables. In fact, they only make minimal sense for ...
Jerry Coffin's user avatar
  • 33.6k

Only top scored, non community-wiki answers of a minimum length are eligible