Most viewed questions in C++

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

implementing merge sort in C++

To respond to the question:  std::vectorT> is used to create dynamically sized arrays at runtime.  Ideally, you'd use one of these to get feedback.  If not, they are simple to convert.  For instance, you might make two arrays like this: template <typename T> void merge_sort(std::vector<T>& array) { ...READ MORE

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

The Definitive C++ Book Guide and List

Introductory, no previous programming experience Book Author(s) Description review C++ Primer* * Not ...READ MORE

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

C++ auto keyword. Why is it magic?

Auto was a keyword that C++ "inherited" from C and had been around for a long time but was almost never used because there were only two possibilities: it wasn't allowed or it was assumed by default. C++11 introduced the usage of auto to denote an inferred type. Similarly to how template type deduction works for function templates, auto x = initializer deduces the type of x from the type of initializer.  Consider the following function template: template<class T> int whatever(T t) { ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
499 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
496 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
495 views
0 votes
0 answers

Using "super" in C++

The following phrase is part of my coding approach: class Derived : public Base { ...READ MORE

Jul 13, 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
489 views
0 votes
1 answer

What is compile-time polymorphism and why does it only apply to functions?

"Compile time polymorphism" used to signify function overloading.  It only applies to functions because that's all you can overload. Templates in modern C++ modify this.  One example has previously been provided by Neil Butterworth.  Another technique is template specialisation.  As an example: #include <iostream> #include <string> template <class T> struct my_template { ...READ MORE

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

Cases of static and dynamic binding in C++

When an object's static type is used to associate it with a member function, this is known as static binding (understand the type of its class). When a pointer or reference is associated with a member function based on the dynamic type of the object, this is known as dynamic binding (understand the instance of the variable at runtime). Before continuing, keep in mind that dynamic binding only works with pointers, references, and virtual functions for the base class. Because everything needed to call the function is known at compile time, the first call is a static binding (also known as early binding). Derived1 d1(1, 10); d1.display_data(); You already know that the d1 instance is a Derived1 automatic variable, and that it will call the Derived1::display data method (). The first condition is incorrect: d1 is neither a pointer nor a reference. The second condition isn't acceptable:  There is no virtual Derived1::display data. The second call is for ...READ MORE

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

What really is a deque in STL?

A deque is defined somewhat recursively: fundamentally, ...READ MORE

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

What range of values can integer types store in C++

You may rely on the following minimal ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
467 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
466 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
461 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
461 views
0 votes
1 answer

std::next_permutation Implementation Explanation

Permutations are generated in lexicographical order by ...READ MORE

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

In C++ abs( *a - *b) does not return absolute value of negative number

On the first line, you reallocated *a, and it is now utilising that new value on the second line.  int origa = *a; *a = abs(origa + ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
456 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
449 views
0 votes
0 answers

how the iterator in c++ could be printed?

Suppose, I have declared a vector in C++ like ...READ MORE

Jul 11, 2022 in C++ by Nicholas
• 7,760 points
447 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
445 views
0 votes
1 answer

Binary literals?

Binary literals will be supported in  ...READ MORE

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

C++ vector erase function not working properly

Here is the code I want to use to delete any entries with values more than 2 and fewer than 5. vector<int> myvector{3, 3, 3, 3, 3, 3, ...READ MORE

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

Reverse Contents in Array

My strategy would be as follows: #include <algorithm> #include <iterator> int main() { const int ...READ MORE

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

functions in c++ call by value and call by reference

Calling a function by value copies the argument and stores it in a local variable for use by the function, so the argument is unaffected if the value of the local variable changes.  The argument is passed to the function as a reference rather than a copy, so if the function changes the value of the argument, the argument is also changed.   The void change(int); function prototype informs the compiler that there is a function named change that takes a single int argument and returns void (i.e. nothing).  Because there is no & with the argument, it is called by value.  You have the line change(orig); later in your code, which actually calls the function with. Take a look at the output of ...READ MORE

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

When to use virtual destructors?

When you want to delete an instance of a derived class using a pointer to the base class, virtual destructors come in handy: class Base { // ...READ MORE

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

What exactly does stringstream do?

When converting between strings and other numerical kinds, stringstream can be very useful.  Stringstream is comparable to iostream in terms of usage, so learning it is not difficult. Stringstreams can be used to read strings as well as write data into them.  It mostly works with a string buffer instead of an actual I/O channel. The stringstream class's fundamental member functions are as follows: str() is a function that returns the contents of a buffer as a string. str(string), which changes the buffer's contents to the string argument. Here's an example of how string streams can be used. ostringstream os; os << "dec: " << 15 ...READ MORE

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

Difference between for loop and the range based loop in C++

The distinction between a for loop and ...READ MORE

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

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
421 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
420 views
0 votes
0 answers

Reverse a number without converting it to string in C++

I'm attempting to write a programme in C++ to reverse a number. It works well with numbers like 1234, but when I try to enter a number like 5430, it displays 345, and the same with numbers that begin with zero, such as 0234, which displays 432. Could someone please explain how to deal with zeros at the beginning and end of a sentence? I simply need to save the number without converting it to a string. #include<iostream> using namespace std; int main(){ ...READ MORE

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

How to use enums in C++

This will be sufficient to declare your ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
410 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
409 views
0 votes
0 answers

What is object slicing?

What is object slicing in C++ and ...READ MORE

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

How to choose between map and unordered_map?

Assume I wanted to map data that ...READ MORE

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

What does "#include <iostream>" do?

You must include it in order to read or write to the standard input/output streams. int main( int argc, char * argv[] ...READ MORE

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

Why doesn't C++ have a garbage collector?

First and foremost, I'm not raising this ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
402 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
402 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
398 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
395 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
394 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
391 views
0 votes
1 answer

Why should I use reference variables at all?

References themselves are unrelated to the issue. The issue is because C++ manages object lifetimes differently from run-time systems that employ garbage collectors, such as Java.  There is no standard built-in garbage collector in C++.  Both automatic (within local or global scope) and manual (explicitly allocated/deallocated in heap) object lifetimes are possible in  C++. A C++ reference is nothing more than an object's alias.  It has no knowledge of object lifespan (for the sake of efficiency).  The coder must give it some thought.  A reference bound to a temporary object is an exception; in this situation, the temporary object's lifespan is prolonged to include the lifetime of the bound reference. References play a crucial role in the fundamental ideas of C++, and they are required for 90% of jobs.  Otherwise, pointers must be used, which is typically far worse. You can use references, for instance, when you need to give an object as a function parameter by reference rather than by value: void f(A copyOfObj); ...READ MORE

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

How to use enums in C++

Suppose we have an enum like the following: enum Days ...READ MORE

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