Most answered questions in C++

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
616 views
0 votes
1 answer

What is a smart pointer and when should I use one?

A smart pointer is similar to a ...READ MORE

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

Check if element is in the list (contains)

The simplest and quickest method. You could also ...READ MORE

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

How to use std::sort to sort an array in C++

We receive std::begin and std::end in C++0x/11, which are overloaded for arrays: #include <algorithm> int main(){ int v[2000]; ...READ MORE

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

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

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

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

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

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

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

Why would anyone use set instead of unordered_set?

Unordered sets must compensate for their O(1) ...READ MORE

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

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

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

C++ `this` pointer

Pointer variables are used to store the ...READ MORE

May 31, 2022 in C++ by Damon
• 4,960 points
355 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
621 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

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

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

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

why are copy constructors needed and what are the cases where they are very helpful?

A copy constructor is a member function ...READ MORE

May 31, 2022 in C++ by Damon
• 4,960 points
484 views
0 votes
0 answers

Check if element found in array c++

How can I see if the element ...READ MORE

Nov 17, 2022 in C++ by Ashwini
• 5,430 points
460 views
0 votes
0 answers

Best way to split a vector into two smaller arrays?

I'm attempting to divide a vector into ...READ MORE

Nov 17, 2022 in C++ by Ashwini
• 5,430 points
827 views
0 votes
0 answers

Correct way to work with vector of arrays

Could someone please explain how to work ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points
348 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
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
537 views
0 votes
0 answers

Differentiate between function overloading and function overriding

Differentiate between function overloading and function overriding ...READ MORE

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

What is the point of function pointers?

The purpose of function pointers is difficult ...READ MORE

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

What are the differences between struct and class in C++?

I now want to understand the distinctions ...READ MORE

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

What does '&' do in a C++ declaration?

As a C person, I'm attempting to comprehend some C++ code.  The declaration of my function is as follows: int foo(const string &myname) { cout ...READ MORE

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

How to end C++ code

I want my C++ code to terminate  ...READ MORE

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

get absolute value without using abs function nor if statement

How might I obtain an integer's absolute ...READ MORE

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

How do I set up Visual Studio Code to compile C++ code?

Although the Visual Studio Code editor from ...READ MORE

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

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

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

Remove elements of a vector inside the loop

I am aware that there are questions like this one, but I was unable to use them to help me decipher my code.  I just want to remove or delete one element from a vector by running a loop over its property.  How can I go about doing that?  I tried the following code, but I got an ambiguous error message: 'operator =' function is unavailable in 'Player’. ...READ MORE

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

Why is the size of an empty class in C++ not zero?

Why does the following output 1? #include <iostream> class Test { }; int ...READ MORE

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

memory allocation for objects

In C++, a variable like int x ...READ MORE

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

How does "this" cascading work?

The following class interface I have is: ...READ MORE

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

Why was the ampersand chosen as the symbol for references in C++?

Does anyone know why the ampersand was ...READ MORE

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

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

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

(->) arrow operator and (.) dot operator , class pointer

In C++, we know that given a class pointer, we use the (->) arrow operator to access the members of that class, as seen here: #include <iostream> using namespace std; class myclass{ ...READ MORE

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

What is a stream in C++?

I've been hearing a lot about streams, ...READ MORE

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

How to Access Subgraphs of an Existing Graph in Boost

I read a graph using read graphviz() ...READ MORE

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

Is there a maxheap in the C++ standard library?

I understand that the std::priority queue class ...READ MORE

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

how to print \" in C++

I need to print the following string: std::string("-I\"/path/to/dir\" "); Basically, I need to accomplish this since I am generating C++ code using C++ code. I want to use an ofstream to write the above string, so something like ofstream fout; fout << the_string ...READ MORE

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

Efficient way to return a std::vector in c++

When delivering a std::vector in a function, how much data is duplicated, and how much of an optimization will it be to place the std::vector in free-store (on the heap) and provide a pointer instead, i.e. is: std::vector *f() { std::vector *result = new ...READ MORE

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

In C, are arrays pointers or used as pointers?

First, look at this code When this is ...READ MORE

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

How to generate a random number in C++?

I'm attempting to develop a dice game, and I need random numbers to represent the sides of the die.  I can make a number between 1 and 6).  Using #include <cstdlib> #include <ctime> #include <iostream> using namespace ...READ MORE

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

Confused with object arrays in C++

I have some Java knowledge and now ...READ MORE

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

Is C++ considered weakly typed? Why?

C++ has always struck me as one ...READ MORE

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

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
454 views