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
205 votes
5 answers
36k 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 ...
Overv's user avatar
  • 1,972
119 votes
5 answers
30k 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 ...
Dylan Black's user avatar
  • 1,451
118 votes
11 answers
52k 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 ...
Mark Ransom's user avatar
  • 1,866
100 votes
16 answers
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 ...
Jozin S Bazin's user avatar
75 votes
8 answers
15k 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 ...
dreamlax's user avatar
  • 1,128
72 votes
6 answers
4k 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 ...
Jerry Coffin's user avatar
  • 33.5k
57 votes
4 answers
35k 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 ...
Baum mit Augen's user avatar
55 votes
6 answers
20k 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 ...
Handge's user avatar
  • 653
55 votes
6 answers
42k 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 ...
isarandi's user avatar
  • 797
55 votes
2 answers
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: ...
Konrad Rudolph's user avatar
51 votes
3 answers
25k 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 ...
Kerrek SB's user avatar
  • 1,193
49 votes
1 answer
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: ...
Martin York's user avatar
  • 95.1k
48 votes
7 answers
20k 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 ...
laurencevs's user avatar
43 votes
7 answers
46k 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? ...
Twooey's user avatar
  • 974
43 votes
5 answers
7k 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 ...
Morwenn's user avatar
  • 20k
43 votes
2 answers
471k 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? ...
Funky's user avatar
  • 950
42 votes
12 answers
10k 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 ...
carbon_ghost's user avatar
41 votes
4 answers
23k 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 ...
daknøk's user avatar
  • 664
41 votes
9 answers
127k 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 ...
Jere.Jones's user avatar
41 votes
1 answer
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 ...
Emily L.'s user avatar
  • 16.6k
38 votes
5 answers
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 ...
Ethan Bierlein's user avatar
38 votes
6 answers
3k views

Resolving a link

How can I clean this up? ...
Billy ONeal's user avatar
  • 1,470
38 votes
6 answers
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 ...
Luke's user avatar
  • 481
37 votes
8 answers
26k 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 ...
Paul's user avatar
  • 492
37 votes
6 answers
28k 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 ...
shmoo6000's user avatar
  • 583
36 votes
9 answers
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 ...
user avatar
36 votes
6 answers
10k 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? ...
Amession's user avatar
  • 547
34 votes
2 answers
68k 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? ...
Robomatt's user avatar
  • 391
34 votes
3 answers
90k 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 ...
Yechiel Labunskiy's user avatar
33 votes
9 answers
8k 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. ...
Gilfoyle's user avatar
  • 1,125
33 votes
1 answer
7k 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 ...
Martin York's user avatar
  • 95.1k
32 votes
10 answers
17k 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. ...
Adrian's user avatar
  • 459
32 votes
2 answers
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. ...
Dovyd's user avatar
  • 321
32 votes
4 answers
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 ...
piepi's user avatar
  • 823
32 votes
4 answers
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 <...
Elizabeth C.'s user avatar
32 votes
6 answers
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 ...
Nitin Labhishetty's user avatar
32 votes
2 answers
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 ...
Francis's user avatar
  • 547
31 votes
4 answers
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 ...
horvste's user avatar
  • 531
31 votes
2 answers
1k views

Dynamically call lambda based on stream input

Some context: I have code that looks like this (minor issue noted here): ...
Martin York's user avatar
  • 95.1k
31 votes
3 answers
28k 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 ...
Yann's user avatar
  • 2,226
31 votes
1 answer
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 ...
iavr's user avatar
  • 2,284
30 votes
3 answers
10k 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 ...
Philippe Balleydier's user avatar
30 votes
5 answers
18k 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 ...
loading...'s user avatar
30 votes
2 answers
58k 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 ...
Rohith R's user avatar
  • 403
30 votes
3 answers
28k views

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

My goal was to get the following code to work: ...
Andrew Spott's user avatar
30 votes
1 answer
24k 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 ...
Peter's user avatar
  • 451
29 votes
4 answers
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 ...
syb0rg's user avatar
  • 21.8k
29 votes
4 answers
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 ...
FromTheStackAndBack's user avatar
29 votes
3 answers
59k 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. ...
28 votes
5 answers
8k 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 ...
Christian Panov's user avatar

1
2 3 4 5
190