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
1
vote
2answers
32 views

C++ For Euler Problem 3

Here's my solution for Euler Problem 3: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? I'm using the Euler problems to learn ...
9
votes
2answers
221 views

Radix2 Fast Fourier Transform implemented in C++

Note: If you don't know much about Fourier transform algorithms, a simple review of whether I am doing anything inefficient with C++ in general would be appreciated. I've been working on implementing ...
-3
votes
0answers
27 views

C# to C++ (dependency injection, passing parameters) [on hold]

I'm relatively new to c++ and trying to understand some conversion from c# to c++. I am struggling to understand when to pass a parameter as reference/pointer/smart pointer or what to return. I read ...
4
votes
2answers
262 views

C++ Least cost swapping 2

I've made a solution for a problem which involves changing order of objects having some mass, so it costs a mass of an object A and a mass of an object B to make a swap. The full description of the ...
-4
votes
0answers
34 views

What is the efficient way to write large data into .csv file? [on hold]

I am trying to write the large data into a csv file and the number of rows I am dealing with is in Millions. My code is generating the rows in form of array of doubles. The code is taking vary long ...
3
votes
1answer
41 views

How to optimize the code snippet which has several calls to find method that is to read files and load them into the map?

I wrote a functionality to read files and load them into the map m_fileParams. Then I am searching the specific keys in the map container using ...
5
votes
1answer
62 views

C++ bare getter setter with dispatch

I am trying to achieve a getter/setter behaviour that will reduce code in C++. The interface I would like to achieve is for example: ...
3
votes
3answers
149 views

RAII wrapper for SQLite transactions

I'm using this nice C++ wrapper to work with SQLite3 in my project. I wanted to handle errors gracefully and automatically rollback if piece of code fails, so wrote this simple helper class: ...
-2
votes
0answers
22 views

Alternatives to macro [on hold]

Macros are the root of almost all evil, but in my source base I use a lot of them to shorten or to simplify code. What are common approaches to avoid them? E.g. I wrote a Python wrapper that should be ...
2
votes
1answer
44 views

HackerRank leaderboard challenge Time out

https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem I tried to solve this challenge and optimize the solution but I still have timeout. This my code below The code simply starts ...
1
vote
1answer
34 views

Dijkstra's implementation in c++ with STL priority_queue

This is dijkstras shortest path algorithm implementation in c++ using priority_queue STL. Looking for two things: a) Correctness of algorithm itself and b) Any improvement suggestions. ...
0
votes
0answers
13 views

Leetcode Finding Maximum Area of Island Solution

Problem Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid ...
0
votes
0answers
25 views

Simple agent based model: passing values between agents using smart pointers and vectors

I am modelling a supply chain as an agent based model, where the overall goal is to model how inventory stored in different stages of the supply chain (e.g. in producer and retailer agents) are passed ...
5
votes
5answers
2k views

Least cost swapping in C++

I've made a solution for a problem which involves changing order of objects having some mass, so it costs a mass of an object A and a mass of an object B to make a swap. The program needs to read a ...
2
votes
1answer
29 views

Convert Sorted Array to Height Balanced Binary Search Tree

Algorithm This is my code to convert a sorted vector of integers to a balanced binary tree of the same content. The binary tree is made of these nodes: ...
7
votes
2answers
85 views

Wordsearch solver

The code below shows my take at creating a wordsearch puzzle solver, where the wordearch is embedded into a .txt file that is parsed and fit into a ...
4
votes
1answer
80 views

Function composition in C++

My aim was to create a function which behaves like the operator . in Haskell. Here is what I made: ...
5
votes
2answers
76 views

Chip8 Emulator in C++

I wrote this chip8 emulator as practice recently since my coursework is mostly console programs. I've tested it with roms off Zophar's Domain and it appears to work with my limited testing. I'm hoping ...
-5
votes
0answers
28 views

Why am I getting an “use of undeclared identifier” error? [closed]

I was learning about multidimensional arrays and one of the ways through which we can transverse through each element was through using 2 nested for loops. This code is supposed to change the array ...
1
vote
0answers
26 views

Modelling a research experiment setup [closed]

I'm creating an app in C++ designed to run an experiment. The model I created is here: ...
2
votes
2answers
41 views

Beginner user registration and login program

I'm new to coding and C++, this is one of my first pieces of "software". Could anyone give me some constructive criticism? ...
1
vote
0answers
23 views

Limit framerate [on hold]

I have made this class. Not sure if there is anyway to make it better. It seems to work alright half the time, but other times frames jump up higher then the set limit around 30frames more if i set it ...
-3
votes
0answers
22 views

I need help creating a five card draw program following these rules in C++ I have added what code I have [closed]

Enumerated data types Program uses enumerated data types for the ranks and suits of the cards. Arrays The program includes an array to keep track of all 52 cards. It also has an array for the dealer'...
2
votes
0answers
25 views

C++ Sql-like base operating on csv files

I implemented very constrained trivial implementation of sql querries operating on user files (in csv format). Where possible i tried to use modern C++ features. The fancy goal is to serve MySql ...
3
votes
2answers
56 views

Minimum Number of swaps required to group all ones together in array of 0s and 1s

Problem Statement: Given a binary array data, return the minimum number of swaps required to group all 1’s present in the array together in any place in the array. ...
2
votes
2answers
219 views

A simple stop watch which I want to extend

I am making a very simple stopwatch functionality as below. New will start the timer. Pause will suspend the watch. Start will restart again. Session will return the duration. ...
1
vote
1answer
27 views

SDL Initialization and Management wrappers

I am using SDL2 for window management and rendering, but it can be a little verbose. And because of how SDL2 works under the hood, the SDL_Renderer is tied to image resource loading. So, I am ...
4
votes
1answer
50 views

Invert bits of binary representation of number

This is the code I came up with. I added comments to make the solution more verbose. ...
-3
votes
0answers
30 views

Xor in a given sequence [closed]

given an array a1, a2 .... an; needed to find all pair adjustment substring such that their XOR is 0 (zero); means 1st substring A = a1,a2, a3; 2nd substring B = a4 a5 a5; (it must start right ...
-2
votes
0answers
46 views

Sum of differences of the indexes where the values are the same [closed]

A given array from 0 to N. in this we are calculating sum of differences of indexes where any two indexes have same value. How can I optimize it from \$O(n^2)\$ to \$O(n \log n)\$ or something better? ...
1
vote
0answers
51 views

Work around for querying type of derived class

I have an application where messages are collected and dispatched to various handlers. Messages are derived from the base class MessageBase, handlers derived from <...
3
votes
1answer
55 views

Multithreading Ticket class to wait for parallel task completion

I've implemented a "Ticket" class which is shared as a shared_ptr between multiple threads. The program flow is like this: parallelQuery() is called to start a new query job. A shared instance of ...
4
votes
2answers
66 views

Draw an outline of an upward-pointing triangle

I am beginner in C++ and I was given this assignment by my teacher. The assignment is to print the following shape: ...
-4
votes
0answers
972 views
4
votes
0answers
44 views

C++14 AtomicRoundRobinPool for sharing a pool of keys

The idea here is that we have some resource that is relatively very expensive to generate, but once we have a bunch of them in a pool, we can keep reusing them instead of generating new ones. "HTTP ...
2
votes
0answers
29 views

Code to configure “particle generator” from valid configurations options

My C++ code accomplishes the following: It consists of a class called ParticleGenerator which has three important configuration options: ...
1
vote
1answer
63 views

Fast insert, fast removal and fast access object pool C++ container

I just want to gather some feedback about this container, in my view it solves the problem of common data containers which don't have fast access and fast deletion. I want to admit that I have not ...
3
votes
0answers
50 views

4×4 cofactor in SSE

The cofactor of a 4×4 matrix can be used to convert a "regular geometry" matrix into the matrix that transforms the normals. It's an alternative to the common inverse-transpose pattern. In this post I ...
5
votes
1answer
89 views

A standard-conforming C++17 std::optional implementation

Background I took quite some time to implement a fully standard-conforming std::optional in C++17. It turns out more sophisticated than I initially thought. My ...
7
votes
2answers
913 views

`printf` improvement with C++ variadic templates

I am currently working through learning C++'s variadic templates and I wanted to create an example use of them that is less trivial than the one in my book. I decided to create a modification of ...
4
votes
1answer
52 views

Reading from a stream but reserving N bytes at the end of stream

What I'd like to do is read data from a stream (like ifstream::read), but reserve N = 4 bytes from being returned. The last 4 bytes of the stream are not part of the data itself, but are metadata. In ...
2
votes
2answers
64 views

Implementation of a Singularly LinkedList

I'm new to C++ and trying to learn by implementing data structures in it. Is the code below the most efficient way of implementing the data structure in C++? Does it conform to C++ standards, and how ...
3
votes
1answer
105 views

Snake Game in C++ with OOP need advice

I wrote a Snake Game in C++ and I have practiced some OOP approaches on windows 7. What I need: advice how to improve code Most important how to made what Snake on ...
2
votes
1answer
37 views

C++ singly linked list with iterator

I have written a simple singly linked list in C++. I have previously had a data structure in an interview criticised for being too C-like (no iterators and using new/delete) instead of smart pointers. ...
0
votes
1answer
62 views

Text-based Tetris game in C++

I am learning C++ and I've written a text-based classic Tetris game using OOP design. I would really appreciate it if someone could take a look at it and review the OOP design and tell me if something ...
4
votes
1answer
80 views

Create HTML structure dynamically

We had a long HTML string with information about the makers of our software. However, for i18n this was unwieldy. So I started to refactor, so only each person's description is translateable, not all ...
8
votes
3answers
2k views

HackerRank Implement Queue using two stacks Solution

Challenge Problem Statement - Implement a Queue using two Stacks I implemented dequeue() in \$O(1)\$ at the cost of enqueue() ...
1
vote
1answer
30 views

LeetCode Count number of set bits solution

Question - Write a function that takes an unsigned integer and return the number of '1' bits it has (also known as the Hamming weight). ...
4
votes
1answer
44 views

errorlocation() routine, implemented in C++ based on pseudocode

Recently I was reading a book called Code Complete by Steve McConnell and I came across a chapter called The PseudoCode Programming Process(PPP) where the chapter taught me on how to write Pseudo code ...
3
votes
1answer
58 views

Stereo image matching implementation in Halide

The authors of this paper compute support_points of a 900×700 image in 118 ms. I have implemented their algorithm below in Halide. In my algorithm, the nested for loops over length and width iterate ...