Most voted questions in C++

0 votes
1 answer

Socket Programming in C++

The C++ Standard does not have a ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
521 views
0 votes
1 answer

"We do not use C++ exceptions" — What's the alternative? Let it crash?

If you don't utilise exceptions, by definition, ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
312 views
0 votes
1 answer

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
1,445 views
0 votes
1 answer

Passing a 2D array to a C++ function

There are three ways to pass a ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
27,589 views
0 votes
1 answer

Examples of good gotos in C or C++

Here's one method I've heard of folks use. But I've never seen it in the wild.  And that only pertains to C since RAII allows you to accomplish this more idiomatically in C++. void foo() { if (!doA()) ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
295 views
0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
670 views
0 votes
1 answer

What is the difference between operator overloading and operator overriding in C++?

Some people use the latter word to ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
1,684 views
0 votes
1 answer

Is there any built-in factorial function in c++?

Although no C function is written particularly for computing factorials, the C math package allows you to compute the gamma function. Because (n) Equals (n-1)!  Using tgamma of i+1 on positive integers returns i!. for (int i = 1 ; ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
5,665 views
0 votes
1 answer

What is dynamic initialization of object in c++?

Dynamic initialization occurs when the initialization value is unknown at compile time.  To initialise the variable, it is calculated at runtime. Example, int factorial(int n) { ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
1,574 views
0 votes
1 answer

How do I use 'strlen()' on a C++ string

In C++, there are two standard methods for storing strings.  In this scenario, you specify an array of characters, and 0 signifies the end of the string. #include <cstring> char str[500] = "Hello"; // How ever ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
845 views
0 votes
0 answers

What is meant with "const" at end of function declaration?

I received a book in which it is written: class Foo { public: int ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
291 views
0 votes
0 answers

How can i sort a map by its .second parameter

How can I output all the int ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
450 views
0 votes
0 answers

Check if a string contains a string in C++

I've got a std::string variable. I'd want ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
434 views
0 votes
0 answers

Container Class vs Class - C++

I'm new to programming and have only ...READ MORE

Jul 28, 2022 in C++ by Nicholas
• 7,760 points
216 views
0 votes
0 answers

How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers

As a mathematician, one of my pet peeves of C-derived languages is : (-1) % 8 // comes out as ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
266 views
0 votes
0 answers

What type of members can I add in a c++ abstract class

Hello, let's say I have an abstract class with a few pure abstract functions and a few classes that derive from it, and all of the data from these classes eventually becomes similar, I was wondering if it would be wise, or even possible, to declare a vector under protected in the abstract class to collect the data so something like that. class A { protected: vector <string> ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
266 views
0 votes
0 answers

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
516 views
0 votes
0 answers

Simple C++ swap function

Why is it that if I have a method like this to swap two numbers, it doesn't work[swap], (I know I can accomplish this by defining pointers in the prototype and then passing the addresses of the relevant variables in main()), yet it works for arrays without having to supply pointers and addresses? It does not work. void num_exchange(int m, int n); int main(){ int num1 ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
295 views
0 votes
0 answers

How to initialize an std::string with a length?

How can I correctly initialise a string if its length is specified at build time? #include <string> int length = 3; string word[length]; //invalid ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
962 views
0 votes
0 answers

How to declare an array of strings in C++?

I am trying to iterate over all ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
381 views
0 votes
0 answers

What are the new features in C++17?

C++17 is currently feature complete, therefore major ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
222 views
0 votes
0 answers

Understanding c++ Code: Tower of Hanoi using Recursion

I'm studying about recursion in C++, but the following C++ code used to solve the Tower of Hanoi issue has me baffled. void Hanoi(int m, string start, string middle, ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
423 views
0 votes
0 answers

Storing C++ template function definitions in a .CPP file

I have some template code that I'd rather have saved in a CPP file rather than inline in the header.  I know this is possible if you know which template types will be utilised.  As an example: .h file class foo { public: template <typename ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
596 views
0 votes
0 answers

Adding to a vector of pair

I have a pair vector that looks like this: vector<pair<string,double>> revenue; I'd want to add a string and a double from a map that looks like this: revenue[i].first = "string"; revenue[i].second = map[i].second; However, because revenue is not initialised, it returns an out of bounds error.  So I ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
556 views
0 votes
0 answers

C++ - upcasting and downcasting

As an example: Shouldn't the second d.print() call during upcasting print "base"? Isn't it a "d" derived object that has been upcasted to a base class object? And what advantages does downcasting have? Could you describe upcast and downcast in more detail? #include <iostream> using namespace std; class Base { public: ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
531 views
0 votes
0 answers

Conversion of base 10 to base 6

I attempted to convert a base 10 number to a base 6 number in C, but my code failed two hidden test cases. I don't see any logical flaws in it. Could you do it? //convert base 10 to base 6 #include<stdio.h> int main() { ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
579 views
0 votes
0 answers

What is the difference between a concrete class and an abstract class?

I'm learning C++, but I'm having trouble ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
306 views
0 votes
0 answers

Simple dictionary in C++

Changing some Python code to C++. BASEPAIRS = { "T": "A", "A": "T", ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
416 views
0 votes
0 answers

Why do we use volatile keyword?

I've never used it, but I'm curious ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
230 views
0 votes
0 answers

Is there a default return value of C++ functions?

I'm amazed that a file with the  ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
327 views
0 votes
0 answers

How to concatenate two strings in C++?

I have a private class variable char ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
251 views
0 votes
0 answers

Which is better way to calculate nCr

Approach 1: C(n,r) = n!/(n-r)!r! Approach 2:  I discovered this in Wilf's book Combinatorial Algorithms: C(n,r) may be represented as C(n-1,r) + C (n-1,r-1). e.g. C(7,4) = C(6,4) + ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
601 views
0 votes
0 answers

Visual C++ vs Visual C# , which is the best to learn?

I completed my C++ lessons and practises ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
209 views
0 votes
0 answers

const int *p vs. int const *p - Is const after the type acceptable?

My coworker is 0 for 2 on questions inspired by him (1, 2), so I decided to give him a chance to catch up. Our most recent argument is about the placement of "const" on declarations. He believes it should be placed either in front of the type or after the pointer.  The explanation is that this is what everyone else does, and different styles are likely to be confusing.  Thus, a constant pointer to int and a constant pointer to int would be: const int *i; ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
386 views
0 votes
0 answers

How member functions are implemented in C++?

I read somewhere that member functions in C++ are similar to regular functions but with an additional implicit this parameter. As a result, I assumed that this software would be unable to discriminate between two functions.  However, the software executed successfully.  So, was the above statement incorrect? #include <iostream> class MyCls { ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
195 views
0 votes
0 answers

What is the difference between Java and C++?

What is the difference between Java and ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
245 views
0 votes
0 answers

Default inheritance access specifier

If I have two classes A and ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
341 views
0 votes
0 answers

What are access specifiers? Should I inherit with private, protected or public?

I'm not sure what access modifiers signify ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
232 views
0 votes
0 answers

C++ Switch Cases

I was taking an online quiz based on the C++ switch statement.  I came across a question, and while I have a good knowledge of how switch statements function, this particular question made no sense to me.  Could you please explain? Why is the answer D and not ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
773 views
0 votes
0 answers

XOR Operation Intuition

Except for one, every element in an array of numbers appears twice.  Locate that solitary one. Keep in mind that your algorithm should have a linear runtime complexity.  Could you do that without using any more memory? class Solution { public: int ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
233 views
0 votes
0 answers

How to use vector::push_back()` with a struct?

How do I push a struct back into a vector? struct point { int ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
917 views
0 votes
0 answers

Run C++ in command prompt - Windows

What may be the cause? I know ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
491 views
0 votes
0 answers

Accumulate function in Vector (STL) giving negative sum

I'm receiving a negative total when I run the code below (-294967296). #include<iostream> #include<vector> #include<numeric> using namespace std; int main() { ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
481 views
0 votes
0 answers

What is the fastest way to transpose a matrix in C++?

I have a reasonably large matrix that I need to transpose.  Assume, for example, that my matrix is a b c d e f g h ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
542 views
0 votes
0 answers

dynamic_cast and static_cast in C++

 I'm perplexed by the dynamic cast keyword in C++. struct A { virtual ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
350 views
0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
352 views
0 votes
0 answers

Simple example of threading in C++

Could someone maybe provide a basic C++ ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
324 views
0 votes
0 answers

Successor of INT_MAX and INT_MIN

I have two inquiries. Is 1e9 smaller than ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
475 views
0 votes
0 answers

Converting Binary to Decimal built-in function

In C++, I need to do a ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
503 views
0 votes
0 answers

How to create a dynamic array of integers

How can I use the new keyword ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
243 views