Most answered questions in C++

0 votes
0 answers

How do you properly use namespaces in C++?

I come from a Java background where ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
428 views
0 votes
0 answers

What are Containers/Adapters? C++

What exactly are containers/adapters? I am familiar ...READ MORE

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

How do you open a file in C++?

I want to open a file in ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
376 views
0 votes
0 answers

How come an array's address is equal to its value in C?

The pointer values and pointer addresses in ...READ MORE

Aug 8, 2022 in C++ by krishna
• 2,820 points
330 views
0 votes
0 answers

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
365 views
0 votes
0 answers

Nested try...catch inside C++ exception handler?

I may wish to run code that might throw an exception in my exception handler. Is the following C++ structure acceptable?  Are there any drawbacks if so? try { // ... } catch (const ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
322 views
0 votes
0 answers

How can I get double quotes into a string literal?

I used the printf() command to produce the output seen below: printf("She said time flies like an arrow, ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
524 views
0 votes
0 answers

std::string length() and size() member functions

While reading the responses to this topic, ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
678 views
0 votes
0 answers

Insert object at index of vector c++

I have to add a new item to the existing object vector.  Although I am aware that an iterator must be used, I am not entirely sure how it operates. I need to insert a new item by name in the precise index that I found after some searching into an alphabetically sorted vector.  I have this, then. vector<Person>people; int index =54; Person temp; people.push_back(temp);//insert at end of ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
473 views
0 votes
0 answers

How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

I wanna create a program that can ...READ MORE

Aug 5, 2022 in C++ by krishna
• 2,820 points
681 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
451 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
217 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
233 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
918 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
+1 vote
0 answers

Using push_back vs at in a vector in C++

I'm not sure how to use vectors in C++.  It has to do with the vector's push back technique.  I used push back to insert entries into the vector in the first programme.  I used at() to insert entries into the vector in the second application. #include <iostream> #include <vector> #include <string> using namespace std; int main ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
242 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
543 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
326 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
504 views