Questions tagged [c++]

C++ is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. This tag should be used for any question which requires knowledge or expertise with the C++ programming language. This is a general tag which is used for any of the C++ language standards (C++98, C++11, C++17, etc.). The question should identify the compiler being used, the operating system, and which of the C++ standards is being targeted.

Filter by
Sorted by
Tagged with
198
votes
5answers
32k views

Dijkstra path finding in C# is 15x slower than C++ version

I'm implementing Dijkstra's algorithm with a priority queue for a game I'm developing in Unity with C#. I was a bit disappointed with the performance, so I decided to port the code to C++ and see if ...
114
votes
5answers
26k views

Basic Pokedex in C++

I recently started learning C++ about a week ago, and I made a bit of progress. In honor of Pokemon Go releasing, I decided to make a Pokedex with the original 151 Pokemon. So far I have 120 or so of ...
108
votes
11answers
43k views

Fastest way to clamp an integer to the range 0-255

I'm working on some image processing code that can generate pixel values outside of the normal range of 0 to 255, and I'd like to clamp them back into the valid range. I know that there are saturating ...
94
votes
16answers
9k views

Outputting the names of cars, without repetitions, with the number of occurrences in order of decreasing repetitions

A short while ago, I have submitted a coding exercise to a potential employer. The response came back the next morning and you can guess what it was from the subject of this post. I am not ...
70
votes
8answers
12k views

Open, write and close a file

I have a small 10-liner function that writes some data to a file using an std::ofstream. I did not explicitly call .close() at ...
68
votes
6answers
3k views

infix_iterator code

I've previously posted this on Stack Overflow, and am considering submitting it to Boost for wider distribution, but thought perhaps it would be best to put it up here for peer review first, and see ...
53
votes
2answers
4k views

Named operators in C++

A post by Yakk alerted me to the idea of named operators in C++. This look splendid (albeit very unorthodox). For instance, the following code can be made to compile trivially: ...
52
votes
6answers
16k views

ASCII text-based RPG game in C++

I started to make a C++ RPG game just for fun. It does have quests but it isn't just one long adventure that lasts like 30 minutes. I would like to know what I did well on and things on which I can do ...
50
votes
6answers
37k views

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

I'm really tired of having to type for (int iSomething = rangeBegin; iSomething < rangeEnd; ++iSomething) { ... } whenever I want to iterate over an ...
48
votes
4answers
27k views

Seed std::mt19937 from std::random_device

Many people seed their Mersenne Twister engines like this: std::mt19937 rng(std::random_device{}()); However, this only provides a single ...
48
votes
1answer
7k views

Yet another C++ JSON parser

This is a framework for a JSON parser I put together last night. Any comments appreciated. JsonLexer.l: Breaks the input into lexemes JsonParser.y: Understands the language syntax JsonParser.h: ...
47
votes
7answers
18k views

Mandelbrot image generator and viewer

This is a C++ program which outputs a Mandelbrot fractal image in a graphics window and lets the user zoom and pan around the image, generating a new image each time the user does so. I'm using SFML ...
47
votes
3answers
18k views

shared_ptr and FILE for wrapping cstdio (update: also dlfcn.h)

Even in the presence of <fstream>, there may be reason for using the <cstdio> file interface. I was wondering if ...
42
votes
7answers
40k views

Conway's Game of Life in C++

This is my attempt at Conway's Game of Life. It works, and it's the most complicated program I've made to date. I'm sure it's pretty poorly done. Any ideas on how I can improve on it? ...
42
votes
12answers
9k views

Efficiently squaring each element in a sorted array, keeping it sorted

The other day I was asked the following question during an interview: Given a sorted array, how would you square each element of this array, while keeping it sorted? I froze up and wrote a ...
42
votes
5answers
6k views

Parallel sieve of Eratosthenes

For the first time, I tried to use threads by myself in order to implement a parallel sieve of Eratosthenes. The trick is that each time a prime is found, a thread is spawned to eliminate all the ...
41
votes
2answers
462k views

Snake game in C++

This is my version of the Snake game, written in C++. How could it be improved, and what general advice would be useful for future projects? ...
40
votes
9answers
104k views

Converting between std::wstring and std::string

While researching ways to convert back and forth between std::wstring and std::string, I found this conversation on the MSDN ...
40
votes
1answer
4k views

Recursive shared_mutex implementation

I found myself in need of a Readers-Writer mutex. With C++17 TR2 support not yet available in our compiler, I set out to implement std::shared_mutex so that we have ...
38
votes
5answers
5k views

Easier user input in C++

A more proper version of this utility can be found at the following link here: Giveth me thy easier user input in C++ - follow up. I've always been a little bothered by the fact, that in order to get ...
38
votes
6answers
3k views

Resolving a link

How can I clean this up? ...
38
votes
4answers
18k views

Boolean enums: improved clarity or just overkill? [closed]

Suppose we are writing a GUI toolkit in C++ (though this question may also apply to other languages). We have a button class with a member function hide, which ...
37
votes
6answers
19k views

AES Implementation in C++

I've implemented AES (128, 192 and 256) in C++ and I'm looking to improve the code to make it not "DIY-crypto-bad", if at all possible. I've also been trying to optimize my code, and so far I've ...
36
votes
8answers
18k views

Thread Pool C++ Implementation

I have been working on this thread pool for awhile to make it as simple to use as possible. I need some tips on improving performance, and some good ways to test its performance. I was wondering if ...
36
votes
6answers
9k views

Printing lyrics of 99 Bottles of Beer

I wrote a C++ program for 99 Bottles of Beer, but it's kind of messy. Is there any way I can make it short and cleaner and more easier to read? ...
35
votes
9answers
7k views

Solving the Lazy Hobo Riddle

I would like help in optimizing this calculation, which is from the Lazy Hobo Riddle: There once were 4 hoboes travelling across the country. During their journey, they ran short on funds, so ...
35
votes
6answers
6k views

Parser written in PHP is 5.6x faster than the same C++ program in a similar test (g++ 4.8.5)

I'm absolutely dumbfounded by this. I was trying to demonstrate to myself how much faster C++ is than even modern PHP. I ran a simple CSV parsing program in both that have the same output. The CSV ...
33
votes
3answers
89k views

LinkedList with Node implementation

Below is my implementation of templated Node and LinkedList classes. I would really appreciate it if someone would give me ...
32
votes
10answers
16k views

Sorting large 1GB file with 100 millions numbers using merge sort

My task is to sort 1GB file with 100 million numbers using merge sort without recursion. My idea is to split file into 4 pieces, then to 2 and at the end to one file. ...
32
votes
2answers
5k views

Tic Tac Toe C++

I made this Tic Tac Toe game and I would like to know your opinion and what can be improved. I also want advice about making it check if someone wins. I'm 10th grader, so I still need to learn a lot. ...
32
votes
4answers
2k views

Standardly deviated Fibonacci

I'm new to C++ (about 4 weeks with no prior programming experience in other languages) and am writing a program that's supposed to do the following: Define an array of 100 sequential numbers of type <...
32
votes
2answers
64k views

A multi-threaded Producer Consumer with C++11

I am trying to learn concurrent programming in C++11. I tried to write code for a classic producer consumer concurrency problem. Would you please review and make any comments about it? ...
32
votes
6answers
3k views

Aliens at the train

I solved this problem on SPOJ: The aliens have arrived to Earth and everything is in harmony, the two races can live together. However, one specific Female Alien does not want to see humans on ...
32
votes
2answers
7k views

Lexer+Parser code for my “Reedoo” programming language

I've been working on my own programming language, as a hobby for the past couple of months now, called Reedoo. It is implemented in pure C++ 11 and uses only the libraries that come with C++; no ...
32
votes
1answer
6k views

JSON Serializer

Carrying on from: Yet another C++ Json Parser Yet another C++ Json Parser (Recursive) All the code is available from git hub: ThorsSerializer but only reviewing a small section here. The idea is ...
31
votes
9answers
5k views

Simple n-body class in C++

As part of my training, I implemented a n-body class in C++ to simulate gravitational interaction of bodies and to get more familiar with features that C++ offers such as object oriented programming. ...
31
votes
4answers
3k views

Extending Sieve of Eratosthenes beyond a billion

For MAX value of 1000000000 (\$10^9\$), it takes about 45 seconds to reach the line where it prints "done". How can I speed this ...
31
votes
2answers
1k views

Dynamically call lambda based on stream input

Some context: I have code that looks like this (minor issue noted here): ...
31
votes
1answer
3k views

Yet another 'any' class implementation, named 'some'

This is a follow-up of 'any' class implementation. After posting my answer, I kept working on the code towards parameterizing with respect to memory management as I had promised. I ended up in some ...
30
votes
4answers
4k views

Validity of doubly linked list

I am currently in a data-structures class. The professor will be providing a singly linked list. I wanted to make a doubly linked list to "learn" how to do it. I tested it and I think it works. After ...
30
votes
3answers
7k views

Keeping track of the tennis score

I'm trying to get hired by some company and they gave me a simple remote test to take at home so they can judge my coding skills. It's a really simple problem based on the website HackerRank. It's a ...
30
votes
5answers
14k views

A Fraction class in C++

I am trying to learn clear and concise C++. Currently I'm learning classes in school, and to practice I have made a Fraction class. I'd appreciate any help regarding style, cleanliness or anything ...
30
votes
2answers
55k views

Custom iterator for a linked list class

I have made a LinkedList class. This is a singly-linked-list and I want to make a forward_iterator for this class without using ...
29
votes
3answers
24k views

“Zip-like” functionality with C++11's range-based for-loop

My goal was to get the following code to work: ...
29
votes
3answers
27k views

Quadtree implementation

I've implemented a quadtree in C++. Ignore any apparent similarity to the pseudocode on Wikipedia, it's all in your head, I swear. How can I improve it? Algorithm speed/space improvements, any extra ...
29
votes
3answers
56k views

Impossibly fast delegate in C++11

This legendary C++ delegate article can be easily converted into C++11, without the need for fancy preprocessor magic in the original. I'd like to know if I got all the necessary C++11 nuances right. ...
29
votes
1answer
14k views

SIMD matrix multiplication

I recently started toying with SIMD and came up with the following code for matrix multiplication. First I attempted to implement it using SIMD the same way I did in SISD, just using SIMD for things ...
28
votes
5answers
7k views

C++ logging library

I am creating a logging library in C++, and I wanted your opinion on code quality and performance within some methods. You may ask why yet another logger - the answer is simple. This logging library ...
28
votes
4answers
7k views

Is it Friday yet?

This is the first program that I have written in my C++ saga that I actually think is useful. The description for this assignment is kinda long and mundane though: Write a program that inputs a ...
28
votes
4answers
6k views

Project Euler 001 in C++

I come from a C background and have now just begun C++. This also means that I am not focussing on the algorithm, but more on C++ idioms. Description: If we list all the natural numbers below 10 ...

1
2 3 4 5
167