66
votes
Accepted
Easier user input in C++
namespace std
Others have said this already but it's important enough to be repeated: Don't put your own definitions into ...
- 4,250
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 ...
- 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 ...
- 14.7k
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 ...
- 92.7k
27
votes
Accepted
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" ...
- 64.9k
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 ...
- 11.2k
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 ...
- 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, ...
- 11.1k
16
votes
Accepted
Compile-time sieve of Eratosthenes
I believe I can help with these things:
Obliterate macros
Obliterate raw arrays
Macros can be replaced with std::integer_sequence (C++14), and raw arrays can be ...
- 541
16
votes
Accepted
C++ FlagSet: typesafe usage of enumeration as bitset/bitmask
This looks pretty good. I'd propose the following.
First, you use set_value in exactly one place. It's a pretty unnecessary function, and just this would suffice:
...
- 18.4k
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 ...
- 5,697
14
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 ...
- 33.3k
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 ...
- 65.4k
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;
...
- 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 ...
- 40.8k
13
votes
Accepted
Gray codes addition
Loop invariants code motion
This is the name of an optimization performed by most compilers: when they detect code that actually does not depend on the state of the loop, they move it out of the loop....
- 19.9k
13
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 ...
- 2,303
12
votes
Accepted
Find longest lines from a file
This is a bug:
else if( record.size() > hash_table.rbegin()->second.size() )
As implemented, insertion stops once your program encounters the longest line ...
- 1,053
12
votes
Safe strcpy implementation with C++
Break out your type trait
You have this large amount of is_same checks in a couple places. Break it out in its own trait, and call it something like ...
- 18.4k
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 <...
- 92.7k
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 ...
- 221
11
votes
Easier user input in C++
I've actually implemented the same functionality in the past, so here are my comments.
Wrong behaviour?
It depends on what you can consider wrong. Consider running your own example and inputting <...
- 565
11
votes
Easier user input in C++
namespace std
is reserved. Don't add stuff to it. Especially since there's no reason to in this case. Just put it in your own namespace.
unnecessary string usage
...
- 18.4k
11
votes
Accepted
Filtering variadic template arguments
One reason you think you require a lot of helper metafunctions is that you haven't clearly separated the code for solving the problem (filtering a list) from the code for testing the solution (e.g. <...
- 19.2k
11
votes
Accepted
Ford-Johnson merge-insertion sort
First of all, there is an error
Not a fatal error though since the algorithm still sorts the collection properly, but it actually means that the algorithm doesn't perform as few comparisons as it ...
- 19.9k
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 ...
- 3,265
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 ...
- 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 ...
- 33.3k
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
c++14 × 475c++ × 472
c++11 × 61
algorithm × 53
template-meta-programming × 45
multithreading × 27
template × 27
performance × 26
programming-challenge × 26
reinventing-the-wheel × 21
beginner × 19
sorting × 16
memory-management × 15
strings × 14
iterator × 14
pointers × 14
object-oriented × 12
c++17 × 12
time-limit-exceeded × 11
tree × 10
random × 10
thread-safety × 10
collections × 10
stream × 10
array × 9