Most answered questions in C++

0 votes
1 answer

std::greater on a an std::pair of a double and a class

std::greater is simply a wrapper for a ...READ MORE

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

C++ "Object" class

No, there is no generic base class&nb ...READ MORE

Jun 2, 2022 in C++ by Damon
• 4,960 points
724 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
1,142 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
714 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
2,565 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
1,279 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
1,524 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
1,147 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
1,288 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
798 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
1,352 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
1,588 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
909 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
3,392 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,829 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
806 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
1,153 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
869 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
778 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
1,223 views
0 votes
0 answers

undefined reference to `get_string' cs50 library

i am having this problem it tells ...READ MORE

Sep 16, 2023 in C++ by Mostafa

edited Mar 5 245 views
0 votes
0 answers

fatal error: cs50.h (c language library) : No such file or directory

I am trying to use cs50 library ...READ MORE

Sep 16, 2023 in C++ by Mostafa

edited Mar 5 277 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
978 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
1,362 views
0 votes
0 answers

#defining constants in C++

In various C code, I see constants ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 116 views
0 votes
0 answers

Is it possible to get a single element of a set in C++ STL?

I have the following C++ code with ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 116 views
0 votes
0 answers

Fast input/output in c++

In a programming question, I need to ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 140 views
0 votes
0 answers

What is the difference between private and protected members of C++ classes?

What distinguishes C++ classes' private and protected ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 107 views
0 votes
0 answers

Confused with object arrays in C++

I originally studied Java, and I'm currently attempting to transition to C++.  I'm having some trouble getting arrays to function properly. Right now, all I'm attempting to do is build an array of the object "Player" and add one to it.  But I encounter a problem. Player* players = new Player[1]; players[0] = new ...READ MORE

Aug 25, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 103 views
0 votes
0 answers

When should you use 'friend' in C++?

I like the concept of the << ...READ MORE

Aug 25, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 114 views
0 votes
0 answers

Rules for C++ string literals escape character

What are the string literals' escape character rules?  Are all the characters that are escaped listed somewhere? In particular, gedit colours the following three values differently when I use the symbol in a string literal. I was attempting to generate a std::string from a literal consisting of the character 0, the null character (0), and the character 0.  However, the syntax highlighting warned me that this would result in just two characters, such as the letter 0 followed by the null character (\00, aka \0). Is the following the best course of action for the solution to just this one issue: std::string ("0\0" "0", 3) // ...READ MORE

Aug 25, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 136 views
0 votes
0 answers

In C++, what is the difference between a method and a function

I'm making an effort to use the ...READ MORE

Aug 24, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 89 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
825 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
1,041 views
0 votes
0 answers

c++ custom compare function for std::sort()

I want to create custom compare function ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 164 views
0 votes
0 answers

Alternative function in iostream.h for getch() of conio.h?

I'm attempting to hold the screen on ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 177 views
0 votes
0 answers

Why can we not access elements of a tuple by index?

tuple <int, string, int> x=make_tuple(1, "anukul", 100); cout ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 133 views
0 votes
0 answers

Remove a key from a C++ map

I want to take a key out ...READ MORE

Aug 23, 2022 in C++ by anonymous
• 7,760 points

edited Mar 4 131 views
0 votes
0 answers

Generic Classes in C++

Generic Classes was the topic I was reading. I at one point became immobile.  Here is the relevant code: template <class StackType> class stack { ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 135 views
0 votes
0 answers

How do you declare an interface in C++?

What is the best way to build ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 99 views
0 votes
0 answers

Is it more efficient to copy a vector by reserving and copying, or by creating and swapping?

I'm attempting to efficiently duplicate a vector. I see two potential strategies: std::vector<int> copyVecFast1(const std::vector<int>& original) { std::vector<int> newVec; ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 89 views
0 votes
0 answers

How to convert C++ Code to C

I've got some C++ programme. Numerous classes, ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 84 views
0 votes
0 answers

Passing vector by reference

Using standard C arrays, I would carry out the following: void do_something(int el, int **arr) { ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 145 views
0 votes
0 answers

When should you use a class vs a struct in C++?

When using C++, when is it preferable ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 114 views
0 votes
0 answers

Alternative to itoa() for converting integer to string C++?

If I use itoa() to convert an ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 168 views
0 votes
0 answers

Printing prime numbers from 1 through 100

The following prime numbers are printed out by this C++ code: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 73 79 83 89 97. However, I don't believe that is how my book would prefer it to be written.  It makes a reference to the square root of a number.  I thus tried switching my second loop to for (int j=2; jsqrt(i); j++), but it did not produce the desired outcome. How would I modify this code to make it the way my book desires it to be? int main () { ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 135 views
0 votes
0 answers

Why do we need abstract classes in C++?

I recently learnt about polymorphism in my ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 124 views
0 votes
0 answers

C++11: How to alias a function?

If I have a class Foo in ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 95 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
1,100 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
970 views