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
3 votes
1 answer
115 views

OOP Tic Tac Toe Console C++ Program

Board.h ...
2 votes
2 answers
120 views

the multi-threading of adding all the values from a random generated vector

I currently am creating a vector and fill it with random values( for this case the random values will be 1) once the vector is created i want to calculate the total sum which should be 100000000, I am ...
  • 21
3 votes
1 answer
165 views

C++ std::string implementation

Following is my simple implementation of the std string class. Please review my code and offer some advice. I want to figure out is there anything wrong with my code or is there anything I can improve ...
  • 41
1 vote
0 answers
170 views

Benchmarking an improved wheel sieve

I followed your answers for my last questions (1st 2nd) and I came up with these new versions of my codes but I face some difficulties to measure the efficiency of my code, I use chrono library to ...
0 votes
0 answers
96 views

Make C++ Task class similiar to C#'s Task

I am enjoying making various programs with C++. This is my first code review request. And I hope I can get some insights or might have good advice to make my code better. I make a task class using C++...
  • 19
2 votes
2 answers
195 views

Generic observable type

I am writing an observer design pattern that is not too complex but I still feel there's a lot for me to learn about C++ be it little details (especially coming from C). A couple of observations: I ...
  • 410
0 votes
0 answers
125 views

Function block diagram code in CPP to program Arduino

I have created a processing program (Java) with which a programmer can use a GUI to make a program for an Arduino using logic function blocks. It looks like this: Ofcourse it still needs some work. I ...
  • 136
5 votes
1 answer
73 views

Traverse/transform (xml) tree

I am trying to write a utility for transforming xml trees using single pass and specifying node handlers. I used pugixml prior to this, and the library provides a ...
5 votes
4 answers
290 views

StringPool in C++

I wrote a simple string pool for my compiler in C++ and want to hear your thoughts on its design. String pool has 2 functions: intern and ...
2 votes
1 answer
215 views

Prime sieve for large numbers

I state that I am not an expert and that certainly the code can be improved. I made this prime number sieve to be able to handle large numbers. The basic idea is to write a number p=r+bW*k where r is ...
4 votes
3 answers
523 views

Split string into same consecutive characters

Description Splits a string into a vector of strings that has consecutive same characters Examples in test cases below ("5500025" --> ...
9 votes
1 answer
165 views

Improving the speed of an MD5 implementation from scratch in C++

I wrote an implementation of the MD5 hashing algorithm from scratch in C++, following the original RFC: https://www.ietf.org/rfc/rfc1321.txt . It works as expected, and the outputs match all the test ...
4 votes
0 answers
156 views

Multithreaded MVC game engine

This is an attempt at a multithreaded model-view-controller based engine for 2d console games (board games, roguelikes that sort of thing.) The code below will provide a fully working example but is ...
  • 397
5 votes
2 answers
968 views

Find the best couple of an array in 1.5 seconds

I faced this exercise. Given a sequence \$S\$ (\$2 <= |S| <= 10^6\$) of numbers (\$1 <= S_x <= 10^7\$), determine \$max(abs(S_i - S_j) + abs(i -j))\$. Here is the solution I found: ...
10 votes
3 answers
2k views

Battleships console game - my first code in c++ using classes

Below my first code in c++. Its running fine. I feel I could improve a few things in my code. Please feel free to comment ...
0 votes
1 answer
150 views

Find factors of a Mersenne number

On RosettaCode I found this C++ version of modPow (compute 2^p mod n) to find the factors of a Mersenne number. ...
3 votes
1 answer
81 views

Calculator for multiplying price and quantity

I'm coding with c++. Basically the question is about to make calculation of any product and calculate with price and quantity with our name. Anyone can simplify this coding? ...
0 votes
1 answer
63 views

Flippable atomic boolean

I was trying to implement a boolean that can be atomically flipped. The suggestion on Stack Overflow is to use an integer. This would be my implementation. ...
  • 133
3 votes
2 answers
123 views

Goats up hills II: revenge of the goat

This is a continuation of Goat racing up a hill (C++ hiring take-home) . This is an interview take-home challenge that has been translated to avoid compromising the company's question. Problem ...
  • 55.3k
3 votes
2 answers
165 views

Find pairs of similar (by hamming distance) bit strings

I have a list of binary strings where each binary string max length is 15. I need to find list of integers which is count of similar (should differ by max 1 bit position) binary strings present in the ...
  • 319
6 votes
4 answers
664 views

Counting the number of patterns that exist in a particular string

I am solving this problem where the function returns the number of times a pattern is found in a said string word. It could be easily solved via ...
  • 410
2 votes
1 answer
107 views

Linked List implemented in C++

I have a linked list code in C++: ...
  • 65
5 votes
2 answers
415 views

Unit testing for a multi-dimensional array class

I have designed a class bunji::Tensor which is a multi-dimensional array. I have designed it to have a similar interface to a multi-dimensional ...
2 votes
3 answers
779 views

Check if parentheses are balanced using a stack implemented with a linked list

Problem: Stacks can be used to check whether the given expression has balanced symbols. This algorithm is very useful in compilers. Each time the parser reads one character at a time. If the character ...
  • 29
0 votes
1 answer
35 views

Improving time complexity for codechef Que [closed]

Question Link. Binary Search and Two Pointers related question. For the above-mentioned question, In other people's solutions, people get AC with a binary search solution. However, I get TLE with ...
0 votes
2 answers
149 views

Temperature Scale Converter in C++

Problem statement: Write a program that converts all given temperatures from a given input temperature scale to a given output temperature scale. The temperature scales to be supported are Kelvin, ...
5 votes
1 answer
65 views

Character source with lookahead for lexer

I've been out of the C++ game for a few years and I'm trying to knock the rust off by implementing a programming language. This Source interface is used to read ...
  • 3,261
12 votes
3 answers
4k views

Goat racing up a hill (C++ hiring take-home)

This is an interview take-home challenge that has been translated to avoid compromising the company's question. Problem statement You are a goat that needs to race up a hill. Designated waypoint rocks ...
  • 55.3k
4 votes
1 answer
82 views

Hashtable solution to 2 sum

...
3 votes
3 answers
345 views

Finding first unique character in string

Below is a solution I came up with (in C++) for an algorithm that is supposed to find the first character in a string that only appears once in the input string (the input string is guaranteed to be ...
4 votes
3 answers
273 views

Optimize memory and disk usage for sequences of n-bit values

Motivation: I am working with sequences of n-bit values coming from an ADC. I was initially using a std::vector<unsigned short> to store these values (12 bit ...
  • 61
1 vote
1 answer
42 views

Helper functions for use with std::variant without an actual object

Sometimes, you want to an type of a particular index in an std::variant, and do something with that type without having an actual object. An example use-case would be de-serialization. What do you ...
2 votes
2 answers
52 views

Template Complex class

I have created a class Complex which represents a complex number of any arbitrary arithmetic type. Overloads are given for the 4 common functions, addition, ...
2 votes
1 answer
62 views

Text Based UNO with Colors and Bots

This is the first big thing I have tried in c++. I would like to know any improvements that I can make on it. It uses 4 custom header files, 3 of which are general tools. Also I am on linux and I have ...
  • 23
5 votes
1 answer
194 views

Executing a Bash script from C++

I am trying to execute a Bash command, get the pid, read the (error) output and optionally pass input. I am still quite new at C++ and I would like to know if this is a correct & safe ...
2 votes
1 answer
200 views

Unix epoch seconds corresponding to midnight in a local time zone

I came up with the following function to obtain epoch seconds that correspond to previous midnight in a local time zone: ...
  • 23
3 votes
1 answer
136 views

A Flappy bird game using C++ and SFML

I have written a Flappy Bird using SFML and C++, kindly review my code. I'm hoping for objective criticism. main.cpp ...
  • 33
2 votes
1 answer
44 views

Ring buffer for non-trivial types

I tried to explain in detail everything there should be of concern regarding this particular implementation, and non-code body turned out quite long, scroll down if you just want to read the ...
  • 21
1 vote
2 answers
149 views

Evaluate a boolean expression as a string

Here's my code to evaluate a boolean expression: ...
  • 307
3 votes
2 answers
127 views

Huffman Coding implementation in C++

Here is an implementation of compression with Huffman coding. It works well by my testing. It compresses a 1 GB string in about 30 seconds, and decompresses in about 15 seconds with VisualStudio 2022 &...
3 votes
1 answer
90 views

Queue using circular linked list

I'm reading through Algorithms Fourth Edition by Sedgewick and doing some of the practice exercises in C++11 instead of Java. Exercise 1.3.29 is to write a Queue that uses a circular linked list, ...
2 votes
1 answer
45 views

Implementation of a multi-dimensional array as a single memory buffer [Part 2]

A couple days ago I posted my first implementation of my data structure here, which implements a cache-optimized tensor representation. I have worked the ideas from the responses into my ...
3 votes
2 answers
494 views

Bookshop Management System

I am doing a beginner c++ project, where you can add books, display books. I'm planning on adding other functionalities like searching the database and checking if a book is available for purchase. I ...
8 votes
3 answers
742 views

A generic binary search function in C++

I have written the following generic function template: ...
0 votes
2 answers
638 views

Next level Improved Sieve of Eratosthenes

This is the next development for the Sieve of Eratosthenes algorithm, where all the multiples of 2,3 and 5 are eliminated, which will not take any memory or time. As my last question Improved Sieve of ...
2 votes
1 answer
120 views

Finding anagram groups using hashing and mapping the alphabet to random numbers

I was solving Group Anagram problem in leetcode. Link for the problem https://leetcode.com/problems/group-anagrams/ Below is the code which I wrote ...
  • 287
4 votes
3 answers
106 views

Implementation of a multi-dimensional array as a single memory buffer

There is an updated version of this code with some of the recommended changes made here. I have created an implementation of a multi-dimensional array which utilizes a single continuous memory buffer ...
8 votes
7 answers
4k views

Improved Sieve of Eratosthenes

How can I get to a perfect coding for this algorithm? I made the mathematical theorem, which is a development of the Sieve of Eratosthenes algorithm. I might use some help in coding. You can find the ...
6 votes
2 answers
98 views

LeetCode 126: Word Ladder II -- Memory limit exceeded

I am trying to solve LC126, and got a memory limit exceeded for the following solution. I've seen the answer here -- so I'll try to use Dijkstra as well, but I would like to understand why I get the ...
  • 300
2 votes
3 answers
112 views

Find long palindrome multiplications in modern C++

I'm a former C# programmer, updating my rusty C++98 (perhaps I should try Rust instead) to modern C++20. My main goal is to get used to C++ standard library types and algorithms. I chose to solve a ...