Most viewed questions in C++

0 votes
1 answer

Ternary operator ?: vs if...else

It's not any faster.  There is one difference when you can initialize a constant variable using an expression: const int x = (a<b) ? b ...READ MORE

Jun 7, 2022 in C++ by Damon
• 4,960 points
788 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
1 answer

str_lib_facilities.h file from Stroustrup's Programming: Principles and Practice Using C++ generating errors

Switching the compiler to compile in c++11 ...READ MORE

Jun 13, 2022 in C++ by Damon
• 4,960 points
768 views
0 votes
1 answer

outputting ascii table in C++

This line doesn't do the right thing: ch ...READ MORE

Jun 13, 2022 in C++ by Damon
• 4,960 points
758 views
0 votes
1 answer

static memory allocation like dynamic memory allocation

This declaration int r, c; cin >> r >> ...READ MORE

Jun 7, 2022 in C++ by Damon
• 4,960 points
734 views
0 votes
1 answer

Error "Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat)"

For pip, use the binary-only option.  ...READ MORE

Jun 14, 2022 in C++ by Damon
• 4,960 points
728 views
0 votes
1 answer

Sorting vector elements in descending order

Because of the goto reset instruction, I believe your sort function has entered an infinite loop.  If you wish to construct a basic bubble-sort algorithm, follow these steps: #include <iostream> #include <utility> #include <vector> void bubble_sort(std::vector<int>& v) { ...READ MORE

Jul 11, 2022 in C++ by Damon
• 4,960 points
725 views
0 votes
1 answer

Lower and Upper Bound in case of Decreasing/Non-ascending vector

Both std::lower bound and std::upper bound must have an increasing (non-decreasing) order as their objective. By giving a comparator as the 4th parameter of the functions, you may modify the meaning of "growing." To work with descending vectors, use std::greater. #include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; int main() { ...READ MORE

Jun 14, 2022 in C++ by Damon
• 4,960 points
716 views
0 votes
1 answer

lower_bound == upper_bound

Lower bound: the initial greater-or-equal element. Upper bound: ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
708 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
678 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
677 views
0 votes
1 answer

Is there an easy way to make a min heap in C++?

Use make heap() and its buddies from algorithm>, or priority queue from queue>.  Make heap and friends are used by priority queue. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
676 views
0 votes
1 answer

Why does C++ need the scope resolution operator?

No. There is no scope resolution operator ...READ MORE

Jun 1, 2022 in C++ by Damon
• 4,960 points
676 views
0 votes
0 answers

declaring a priority_queue in c++ with a custom comparator

I'm attempting to declare a priority queue of nodes using the comparator function bool Compare(Node a, Node b) (which is outside the node class). I presently have the following: priority_queue<Node, vector<Node>, Compare> openSet; I'm receiving Error: "Compare" is not a type name for some reason. Adding priority_queue<Node, vector Node>, bool Compare> to the declaration. gives me the error 'Error: expecting a '>' I've also attempted: priority_queue<Node, vector<Node>, Compare()> openSet; priority_queue<Node, ...READ MORE

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

std::cin input with spaces?

#include <string> std::string input; std::cin >> input; The user want to type "Hello World."  However, cin fails to recognise the space between the two words.  How do I get Cin to see the entire Hello World? I'm trying this using structs, and cin.getline doesn't appear to work.  This is my code: struct cd { ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
670 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
665 views
0 votes
0 answers

Infix to Postfix notation C++

Hello there, Stack.  I'm currently learning C++ and trying to create an RPN converter.  But I'm having difficulties.  Hopefully, I'll be able to describe the issues in detail.  For stacking my operators, I'm using an array.  Let's look at the example "5 + 8."  When it comes down to: else if(infix[i] == '+' or infix[i] == ...READ MORE

Jun 29, 2022 in C++ by Nicholas
• 7,760 points
660 views
0 votes
1 answer

How do I create an array of pointers?

Student** db = new Student*[5]; // To allocate ...READ MORE

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

Why is "using namespace std;" considered bad practice?

This has nothing to do with performan ...READ MORE

Jun 1, 2022 in C++ by Damon
• 4,960 points
645 views
0 votes
1 answer

insert method for doubly linked list C++

I attempted to repair all of your methods, and I believe I succeeded; at least, the current test example prints the proper answer: #include <iostream> #include <vector> using namespace std; struct Node { ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
644 views
0 votes
1 answer

What data structure is inside std::map in C++?

An associative container is std::map. The standard's ...READ MORE

May 31, 2022 in C++ by Damon
• 4,960 points
620 views
0 votes
1 answer

Simple linked list in C++

This is the most basic example I can think of in this situation, and it has not been tested.  Please keep in mind that this violates some C++ best practises and deviates from the norm (initialize lists, separation of declaration and definition, and so on).  But those aren't topics I can discuss here. #include <iostream> using namespace std; class LinkedList{ ...READ MORE

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

How to find if a given key exists in a C++ std::map

I'm trying to check if a given ...READ MORE

Jul 14, 2022 in C++ by Nicholas
• 7,760 points
614 views
0 votes
1 answer

How to pass in command line arguments when using ideone?

It appears that you won't be able ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
607 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
600 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

Read whole ASCII file into C++ std::string

I need to load an entire file ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points
592 views
0 votes
1 answer

Using getline() in C++

If you use getline() after cin >> anything, you must first flush the newline character from the buffer.  You can achieve this by using the cin.ignore() It would be something like this: string messageVar; cout ...READ MORE

Jun 1, 2022 in C++ by Damon
• 4,960 points
587 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work.  We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base.  There is, however, a syntax sugar that "copies" all fa1 from the base to the derived. class A { public: void fa1(); ...READ MORE

Jun 7, 2022 in C++ by Damon
• 4,960 points
586 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
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work. . We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base. There is, however, a syntax sugar that "copies" all fa1 from base to derived, removing all the hidden semantics: class A { public: void fa1(); ...READ MORE

Jun 7, 2022 in C++ by Damon
• 4,960 points
569 views
0 votes
1 answer

What is the difference between public, private, and protected inheritance in C++?

To begin answering that question, let me characterise member accessors in my own terms.  If you already know this, proceed to the section "next:". I'm aware of three types of accessors: public, protected, and private. Let: class Base { public: ...READ MORE

Jul 11, 2022 in C++ by Damon
• 4,960 points
565 views
0 votes
0 answers

Code Not working in VS Code but works in OnlineGDB

I am practicing about primeSieve with C++ language in VS Code 1.57.1. Can ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
559 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

What is the difference between ifstream, ofstream and fstream?

I stumbled discovered ifstream, ofstream, and fstream ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
550 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
541 views
0 votes
0 answers

When and why do I need to use cin.ignore() in C++?

In C++, I developed a simple application that requested the user to enter a number and then a string.  Surprisingly, when I ran the application, it never paused to ask for the string.  It simply ignored it.  After conducting some research on StackOverflow, I discovered that I needed to include the following line: cin.ignore(256, '\n'); before the line with the string input  That addressed the problem and allowed the software to run.  My issue is why C++ need the cin.ignore() line, and how can I forecast when I will need to use it. Here's the software I created: #include <iostream> #include <string> using namespace std; int main() { ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
541 views
0 votes
1 answer

abstract class and virtual functions

Is this class considered abstract because it only contains one virtual function?  I could still build an Animal object and use getFoodCost(); No. In C++, a "Abstract Class" is often a class having a single pure virtual function: class Abstract { public: virtual ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
539 views
0 votes
1 answer

Create a reverse LinkedList in C++ from a given LinkedList

A simpler solution is to just let the current node point to the previous node while going through your linked list, saving the previous and next nodes as you go. void LinkedList::reversedLinkedList() { if(head == ...READ MORE

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

Making a class abstract without any pure virtual methods

I take a class where we watch ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
536 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
530 views
0 votes
1 answer

How do I reverse a C++ vector?

The algorithm header has a method std::reverse for this purpose. #include <vector> #include <algorithm> int main() { std::vector<int> ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
527 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
522 views
0 votes
1 answer

please help me with max_element function in c++ stl

You can substitute max for *max eleme ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
522 views
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
520 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
1 answer

The Definitive C++ Book Guide and List

For Beginner (includes those without coding experience) Programming: ...READ MORE

Jun 6, 2022 in C++ by pranav
• 2,590 points
513 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 use setprecision in C++

I'm new to C++ and all I want to do is print my point number up to two digits.  For example, if the number is 3.444, the output should be 3.44, and if the number is 99999.4234, the output should be 99999.42.  How am I going to do that?  The value changes over time.  This is my code. #include <iomanip.h> #include <iomanip> int main() { ...READ MORE

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

Intersection of two std::unordered_map

I have two std::unordered_map std::unordered_map<int, int> mp1; std::unordered_map<int, int> ...READ MORE

May 31, 2022 in C++ by Nicholas
• 7,760 points
503 views