Hot answers tagged

39 votes
Accepted

Seed std::mt19937 from std::random_device

Well, first off, why do you use a std::vector for a comparatively small sequence of known length? A raw array or std::array ...
user avatar
  • 17.7k
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 ...
user avatar
  • 14.6k
34 votes
Accepted

Modeling an IPv4 Address

I think it's very strange that you provide iterators and an operator[] for an IP address. Generally speaking, IP addresses are not considered to be "iterable"; an ...
user avatar
  • 19.1k
31 votes
Accepted

If she floats then she is not a witch like we thought

There are several small things that you can improve: double weight = 0; works fine, but double weight = 0.0; would be more ...
user avatar
  • 19.9k
30 votes

A multi-threaded Producer Consumer with C++11

Prefer Reference over Pointer Since the producer and consumer must have a buffer you should pass it by reference (rather than pointer). This also makes sure there is no confusion over ownership of ...
user avatar
  • 91.7k
29 votes

C++ Vector Clone

Issues Your vector assumes that the type being stored has a default constructor. arr = new element[size]; Also this is really inefficient for expensive to ...
user avatar
  • 91.7k
29 votes

Deck of Cards written in C++

Design review Card Let’s start by looking at the Card class. As designed, Card has 4 data ...
user avatar
  • 8,521
26 votes
Accepted

std::tuple foreach implementation

Loki's solution does not enforce the order in which the function calls are performed, because the order in which function arguments are evaluated is unspecified. Here's a C++14 solution that ensures ...
user avatar
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 ...
user avatar
23 votes
Accepted

Implementing a Stack with Templates and Smart pointers

Readability Please put a space after the include and before the < ...
user avatar
  • 91.7k
23 votes
Accepted

A vector implementation

Design The main thing about a vector is not constructing its members until they are put into the container. The vector you have constructs all the members immediately which can be very expensive if ...
user avatar
  • 91.7k
23 votes
Accepted

C++11 smart pointer 'library'

I'll hit the red flags first, and then review the details. ...
user avatar
  • 19.1k
22 votes

Modern Vector implementation

Memory management new T[m_capacity] default-constructs m_capacity objects of type T in ...
user avatar
  • 6,428
21 votes

Simple integer range for C++11 range-based for loops

You can make the class template with trivial changes (add template<typename T> and change int by ...
user avatar
  • 19.9k
21 votes
Accepted

Quadtree implementation

Ok, so here we go for a few tips: Several methods such as AABB::contains and AABB::intersects do not modify the ...
user avatar
  • 19.9k
20 votes
Accepted

My implementation for std::unique_ptr

Overall Ohhhhh. Is it well-formed? It compiles, so yes. i.e. does it follow common C++ standard and patterns (for example, should private members be declared before public ones? Personally I think ...
user avatar
  • 91.7k
20 votes
Accepted

Most efficient way to find an entry in a C++ vector

Header files It's strange that this code uses the C header <string.h> but the C++ versions of <cmath>, ...
user avatar
  • 58.1k
20 votes
Accepted

BigInt class in C++

We use std::string without including <string> - a possible portability bug. Representation as a string of ...
user avatar
  • 58.1k
19 votes
Accepted

Lexer+Parser code for my "Reedoo" programming language

C style Well, the first thing I have to say is that your code is very C-like. The first action here would be to move all those loose functions and globals into two C++ classes, ...
user avatar
  • 17k
19 votes

String case reverse function in C++

This seems like a perfect opportunity to leverage the standard library. I would make a function to toggle a single character and then use std::transform on top of ...
user avatar
  • 10.5k
18 votes
Accepted

C++ Linux TCP client/server socket wrapper

Design This is a very thin C like wrapper around sockets. In my opinion there are a lot of style changes that are need to make this good C++ or usable by modern C++ library. Main issues: Two phase ...
user avatar
  • 91.7k
18 votes

Multithread to print odd and even numbers

Multiple threads are usually used to compute things in parallel. In this example nothing is computed in parallel: while one thread is running, the other is waiting. With no practical value, it's not a ...
user avatar
  • 107k
18 votes
Accepted

Array Dynamic resize in heap

If you had tagged this code as C, it would have been acceptable. Since you tagged it as C++, it's horrible. Instead of writing your own swap function, there's ...
user avatar
18 votes
Accepted

Simple dataset class in C++

Here are some things that may help you improve your program. Separate interface from implementation The interface goes into a header file and the implementation (that is, everything that actually ...
user avatar
  • 64.5k
17 votes

If she floats then she is not a witch like we thought

Bugs You calculate the volume of the sphere incorrectly! Style You misspelled the variable bouyantForce. The constant ...
user avatar
17 votes
Accepted

Implementing a C++11 raw string class

#include <string> strcpy and its friends reside within the C library header ...
user avatar
  • 1,109
17 votes
Accepted

C++ Split string into a vector

I haven't attempted to measure the timing of this, in part because I don't know how you're planning to use it, but here are some ideas for improving your code. Consider returning something useful ...
user avatar
  • 64.5k
17 votes
Accepted

Vector-transposing function

Change your column loop to use a reference. for (const auto &c : column_vec) Without the reference, a copy will be made of each vector. This will involve a ...
user avatar
16 votes

STL queue implementation

A few points that nearly jump out at me: You've implemented it as a linked list of individually allocated nodes. This nearly guarantees poor locality of reference and for small types wastes quite a ...
user avatar
  • 33.2k
16 votes
Accepted

STL queue implementation

Don't do this in a header file: using std::cout; You are now messing with everybody else's code that uses your header file. That will not please them and can ...
user avatar
  • 91.7k

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