New answers tagged

1

While comments have suggested more suitable types than std::vector, I'm going to assume that the use of this underlying type is a given, and suggest straightforward improvements without changing that. The main performance concern is here: void add_pro(int pro_ID, int pro_pri, std::vector<std::pair<int, int>>& pair_list) { pair_list....


27

Design review Card Let’s start by looking at the Card class. As designed, Card has 4 data members: value suit name rank value is an unsigned int, while the others are all std::strings. This is enormously wasteful. A card really only has two properties: rank and suit. If you know the rank, you know the value and the name, so duplicating that data in the ...


3

Overview Your code produces the exact same set of samples each time you random_shuffle. So the output is always 0 1 2 3 4 5 6 7 8 9 4 3 7 8 0 5 2 1 6 9 0 5 7 8 4 3 9 2 1 6 5 7 6 3 8 4 2 0 1 9 4 6 0 2 8 1 3 9 5 7 4 0 5 1 7 9 6 2 8 3 3 8 5 6 1 7 2 4 0 9 0 4 6 2 9 5 8 3 1 7 1 3 6 8 2 7 5 9 0 4 5 1 7 9 8 0 6 4 2 3 You should initialize the seed. You ...


6

This was fun code to play with and to review. I haven't played with a modular synth for about forty years. Here are some things that may help you improve your code. Consider hiding data Right now everything's public in every Module but it's not clear that's a good thing. For example, any 70's vintage synth should have a Phaser so I implemented this one: ...


2

Readability Version 1: This is unreadable. If you presented that me in a professional context I would literally fire you (for real). Version 2: This is arguably readable. But you are making it hard for yourself and other people. You are probably going to be breaking your companies coding guidelines and I can see your colleagues complain. But its all ...


2

#include only the headers you need where you need them. MyImageSize.h needs no includes. MonoPixelOperation.h needs only <memory>. template<class T = unsigned int> class MyImageSize { private: T xsize; T ysize; public: MyImageSize(T new_xsize, T new_ysize) { this->xsize = new_xsize; this->ysize = new_ysize;...


6

Perspective The answers that were already provided are great for solving the particular problem you have. I will provide feedback based on my current workflow. I implement a tool for those who would like to make quick C++ prototypes without much regard to engineering, which I then fix and make more of a complete solution. Underlying problem What you want to ...


17

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 emits bytes including all functions and data) should be in a separate .cpp file. The reason is that you might have multiple source files including the .h file but ...


5

Design: Data is not a descriptive name, and also misrepresents what the class does. It's really a generator function that's triggered by calling get(). As such, it would be better to replace it with a function, called something like generate_spirals(). We should use the C++ standard library random header for random number generation. This gives us various ...


8

This is a nice, readable piece of code. I would just highlight a few things you might want to consider. Unnecessary Class Let's take a close look at Random class. It holds no state, just a member function rand. In addition to having just one member function, it is only used in one place, in member function generate_data. Classes are meant to be reusable and ...


4

Trivial: the declaration is not complete - you're missing the final ; after the class definition. Missing includes: #include <algorithm> #include <cstddef> using std::size_t; // don't do this in a header! As noted in the comment, don't put standard library identifiers into the global namespace - just write the type in full throughout the ...


4

There are some bugs There are several bugs in your code. In the constructor that takes a C string, you compute the length of cstr and set pSize accordingly, but you allocate pCapacity bytes. You should allocate at least pSize bytes. I recommend you get rid of pCapacity and try to make the class work with just pSize first. You make the same mistake in ...


5

I’m not aware of any sources that give a really good intro to the standard <random> library, unfortunately. I wrote about it years ago, but that was when it was brand new, and I was mostly just introducing it; it’s not really a comprehensive tutorial. However, I can give some advice about the code you’ve got. std::default_random_engine random_engine(...


8

First of all, note that std::default_random_engine and std::uniform_int_distribution<int> are both non-portable in the same sense as rand() is non-portable: they won't produce the same sequence of numbers on different platforms, because their behavior is implementation-defined. For uniform_int_distribution there effectively is no portable alternative, ...


2

You seem to be missing some headers (<vector>, <iostream>, <cmath> and <set>), and also to be assuming the standard names are in the global namespace, rather than std. After attempting to read from std::cin, it's important to ensure the read was successful before attempting to use the result. (E.g. if (!(std::cin >> year)) { ...


Top 50 recent answers are included