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
17 votes
4 answers
2k views

Recursive and iterative Euclidean algorithms

Is this code too cryptic? Simple yes or no question. Feedback is optional. ...
7 votes
4 answers
8k views

Simple array implementation without bounds-checking

I am preparing myself for some job interviews. This is a simple array (with the error handling left out for brevity). Any input or suggestions on style and the use of templates would be appreciated. <...
  • 73
4 votes
1 answer
760 views

More generic sorting algorithm: using namespaces

I am currently reading The C++ Programming Language (Special Edition) and am learning about using templates. One of the exercises asks you to program a generic version of quicksort. I am hoping to ...
6 votes
2 answers
290 views

Determining which command line arguments are numeric

I am working on using STL and in general safe C++ practices. Instead of posting large samples of code I am working with a small sample to catch problems before I move onto larger ones. How do you ...
5 votes
1 answer
126 views

Skip_over() algorithm

I'm just curious if this is clear to the average person. ...
  • 1,470
5 votes
4 answers
745 views

My own quicksort algorithm

I will make it more reader friendly after I get the algorithm completed. Does anything with the quick sort algorithm stand out as for why it is not working 100%? This is not homework; I am just ...
3 votes
3 answers
2k views

Compute and print the sum and mean of name and value pairs

I'm just looking for more feedback on style/approach regarding this small problem. Also, if you have tips to make it more readable or C++-like, please let me know. Also, why is the problem asking for ...
8 votes
2 answers
2k views

BitTorrent peer protocol messages

I'm writing a BitTorrent client. Part of the protocol is an exchange of length-prefixed messages between peers. The forms of these messages are described in the official specification and also in the ...
6 votes
4 answers
606 views

Compute random triplets of float pairs and average area of generated triangles

I am trying to get in a lot of practice for interviews so I am trying to write as much code as possible, and getting feed back from excellent programmers like "you". Can I get some general feed back ...
2 votes
1 answer
541 views

Reading a sequence of points and finding the closest one to the first

I was told this is the place to come for input/feedback on your programming. I am always looking to improve either by personal review or outside opinion/help. Would the community here be willing to ...
99 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 ...
1 vote
1 answer
568 views

VC++ Fast Http Download

I am a C# developer, dont know much about vc++ or c++, never used it, for some reasons i have decided to use a c++ dll in my app for downloading content from the web. I dont want to use WebClient. I ...
4 votes
4 answers
839 views

Most elegant way to write this loop

I have some code which looks like this: ...
  • 1,081
6 votes
4 answers
3k views

C++ enums - which one is better?

I have a large namespace, but I don't want enum constants to be a part of it. In cases where an enum is related with a class, I coded enums inside the class so I can reach it using the class name. ...
7 votes
2 answers
2k views

Lexer code in C++

I've got a lexer written in C++ (Visual Studio 2010, so including lambdas and a few other C++0x tricks). This is my first lexing experience, the only other source code interpretation I ever did was ...
  • 849
2 votes
3 answers
139 views

Should I hide or show this implementation?

An animation is composed of frames. I have the following class: ...
  • 135
3 votes
0 answers
1k views

Integrating SFML and Box2D lib, and a Mass Production Shape Factory [closed]

I've just written two new headers: one that integrates Box2D and SFML by a class called BodyRep which creates/ is a graphic representation of any body, and one that ...
  • 297
6 votes
2 answers
4k views

Finding the largest palindrome from the product of two n-digit numbers

The program finds the largest palindrome made from the product of two n-digit numbers, where n is specified by the user. The code included works, however, when the user enters 5 or greater, the ...
  • 173
6 votes
1 answer
331 views

Tree/array mashup

I'm currently trying to prove a data structure which I've created to speed up lookups for integral keys. The specific case I have in mind has the following usage case: At start is populated with n ...
  • 460
9 votes
1 answer
6k views

Generic C++ Factory

For my project, I needed a way to instantiate different object types during runtime using "string names", for this I designed a generic factory that is created for each object hierarchy type (...
  • 370
3 votes
2 answers
476 views

Iterating over unequal unordered pairs in one collection

This code kinda hurts my feelings -- everything else in my code is done in neat one-liners, exploiting algorithms and, sometimes boost::bind, except for this piece. ...
  • 133
8 votes
3 answers
2k views

Custom integer class for calculating crypto algorithms

How can I improve upon this integer class that I wrote? I am/will be using this for some calculation intensive crypto algorithms, mainly POLY1305AES. I might even write RSA using this. I know that ...
5 votes
4 answers
473 views

Truthy assignments

I often argue with the voices about the correctness of having assignments within an if statement (or equivalent), meaning having this: ...
10 votes
4 answers
742 views

Critique my first C++ "Hello World" program

This is my first attempt to write a simple C++ program: ...
  • 508
8 votes
1 answer
531 views

Use of external memory or a custom allocator

I'm creating a class which uses a custom buffer. I want to offer the possibility to pass an external memory address (for higher interoperability between languages) or (for convenience) to specify a ...
  • 300
6 votes
1 answer
2k views

Fixed point combinator in C++1x

This has been tested and compiled under Visual Studio 2010. Are there any serious problems with this implementation? PS. This implementation is fully lambda expression based. If I can make ...
2 votes
1 answer
3k views

How is my SFML sprite Move() function?

I'm making a pong game with SFML and in the process made a function that takes a Time, Speed, ...
  • 297
2 votes
3 answers
868 views

Finding social network of a friend

Following is the code which finds the social network of a friend (i.e. friends of friends and so on). Friends definition is, ff W1 is friend of W2, then there should be Levenshtein distance equal to 1....
  • 121
6 votes
2 answers
842 views

Bit-twiddling for a custom image format

The following code is mainly concerned with the conversion from byte/bit stream to numericals such as int, unsigned int, short int, unsigned short int, float, and etc. These functions are heavily used ...
27 votes
2 answers
3k views

C++ API for interfacing with Lua

I wanted a good way to move objects back and forth between Lua and C++, and I didn't want to use anything like LuaBind or the other available libraries I could find, so I instead wrote this. It's ...
  • 463
7 votes
3 answers
7k views

Sales tax calculator

Please review the sales tax problem which has been designed using strategy pattern. The Problem: Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical ...
3 votes
1 answer
635 views

Conditional parsing, non-standard approach

Conditional parsing (aka read some tokens and return true or false). As a solution to this SO question, I wrote following code: ...
  • 190
5 votes
1 answer
1k views

An implementation of "finally" in C++0x

I've made a quick finally type in C++: ...
  • 223
15 votes
3 answers
14k views

Simple text-to-binary converter written in C++

I am learning C++ on my own and was hoping for some feedback on this simple text-to-binary converter. The program runs and I haven't encountered any errors with it yet. Any input is welcome. ...
  • 175
5 votes
2 answers
10k views

Decoding binary coded decimal (BCD) value

I have 3 questions, all of them are concerned with binary coded decimal (BCD) conversion. My coding snippets are listed as follows. But I don't like my algorithm because it takes too long when dealing ...
6 votes
2 answers
3k views

Avoiding the 'possible loss of data' compiler warning

I'm writing a file I/O DLL in C++ which is going to be used from VB6. The interface is already fixed, and uses INT64/Currency as the general integer data type. Now I have this function: ...
  • 1,081
8 votes
1 answer
10k views

Combining two 32-bit integers into one 64-bit integer

long long combine(unsigned int high, unsigned int low) { return ((unsigned long long) high) << 32 || low; } Is there a better name for the operation? Is ...
  • 1,081
20 votes
3 answers
19k views

makefile dependency generation

I need automatic dependency generation to be done in my makefile. So, I have tried to write one, after googling for similar ones. I have found some ways to do that, ...
user avatar
4 votes
1 answer
314 views

Text-based vertical scroller game

I'm making a simple text game using the pdCurses library and a few other minor things like a vertical scroller in which you avoid the randomly-generated walls. There are two walls on left and right ...
  • 297
6 votes
2 answers
1k views

Flattening a doubly linked list

I am a Java dev and trying to prepare for interviews in C++. Can someone please review the below solution for me? Assume that you are given the head and tail pointers of a doubly linked list where ...
  • 333
4 votes
3 answers
2k views

Does my Rectangle intersect code work?

I just want to see if any part of one "rectangle" overlaps the other. Is this the correct code? ...
  • 49
2 votes
2 answers
12k views

Fast import of numbers stored in a CSV file

I would like to efficiently import real numbers stored in CSV format into an array. Is there a way to modify my code to make it faster? Also, is there a way to scan the file and compute the number ...
  • 123
19 votes
3 answers
12k views

Generic C++ exception catch handler macro

I have this set of legacy C++ projects with a large number of public functions. At the start, none of those publicly exposed functions had try..catch insulation ...
  • 395
8 votes
2 answers
1k views

C-style array class

I often use C API's in C++ that force me to use C-style arrays. I got sick of constantly using a dynamic vector with &vec[0], so I wrote this C-style array ...
  • 391
10 votes
1 answer
525 views

Emulator for representing hardware and operating systems

This is an emulator I am currently re-writing for my Operating Systems course. It is a simple emulator that is supposed to represent hardware, OS, etc. It is strictly for learning purposes and I want ...
  • 765
4 votes
1 answer
3k views

Traversing a BST in iterative inorder

Traverse a binary search tree in iterative inorder. Note that the iterative methods below contain the meat of the code; the remaining code is included for completeness. ...
  • 339
2 votes
1 answer
172 views

How can i make my Get_Numb_of_Digits_in_a_number() function more accurate?

Here's my function but it sometimes has unpredicted results with entering different numbers but with the same number of digits getting different results. I figure it has something to do with a ...
  • 297
7 votes
2 answers
1k views

Homemade Stopwatch class

Here's some explanation for accuracy and neg_accuracy: It's meant to keep all the values in terms of what the user specified he ...
  • 297
8 votes
2 answers
2k views

Composition or Private Inheritance for Implementing Builder?

Over on StackOverflow, I was asked if I could come up with an example where private inheritance would be preferred to composition (in C++). The following is the situation I described, and I was ...
5 votes
1 answer
1k views

C++ template meta sql

I wrote a wrapper around boost-mpl and transformed it to an sql engine. It is a compile time sql thing, not related to normal databases. I have not finished it completely. I have some problem with the ...