Latest questions in C++

0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

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

Where are the Microsoft Visual C++ 2015-2022 Redistributable (x64) packages installed?

They are, in my opinion, at their best. C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Redist\MSVC It's worth noting ...READ MORE

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

C++ string in classes

The std namespace contains the string class. You need change the class to something like this: class Language { public: ...READ MORE

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

Easiest way to convert int to string in C++

std::stoi (and variations for each numeric type) ...READ MORE

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

What is a lambda expression in C++11?

In C++11, what is a lambda expression? A: It's the object of an autogenerated class with overloading operator() const under the hood.  Closure is a type of object that is produced by the compiler.  This 'closure' idea is similar to C++11's bind notion.  Lambdas, on the other hand, usually produce better code.  Full inlining is also possible with calls through closures. Q: When do you think I'd utilise one? A: Define "simple and tiny logic" and request that the compiler generate the code from the preceding question.  You tell the compiler the expressions you wish to be inside the operator ().  The compiler will produce everything else for you. Q: What kind of problem do they tackle that couldn't be solved before they were introduced? A: It's some form of syntactic sugar, like using operators instead of functions for custom add, subtract, and other operations... However, wrapping 1-3 lines of genuine logic to some classes, and so on, saves additional lines of needless code!  Some engineers believe that if the number of lines is reduced, there is a lower likelihood of mistakes (which I agree with). Example of usage auto x = [=](int arg1){printf("%i", ...READ MORE

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

Mac M1 C++20: Missing std::convertible_to and std::forward_iterator concepts

Clang from Homebrew is a good option (or compile one by yourself).  You may also look at the features that have been incorporated on Clang's official website. ❯ clang++ --version Homebrew clang version 13.0.0 Target: arm64-apple-darwin21.1.0 Thread ...READ MORE

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

Has Windows an integrated built-in C/C++ compiler package?

Microsoft does not offer a compiler or ...READ MORE

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

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

Ceil function: how can we implement it ourselves?

You can disassemble the components of an IEEE754 floating point number and put the logic together yourself: #include <cstring> float my_ceil(float f) { ...READ MORE

Jun 13, 2022 in C++ by Damon
• 4,960 points

edited Jun 14, 2022 by Hemant 987 views
0 votes
1 answer

casting int to char using C++ style casting [duplicate]

Even though the precision is lost, yo ...READ MORE

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

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

std::next_permutation Implementation Explanation

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

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

How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

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

What is the difference between std::__gcd and std::gcd?

I done some research about this. The ...READ MORE

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

C++ - Decimal to binary converting

The.to string() function of std::bitset returns a std::string containing a binary text representation with leading-zero padding. Use std::bitset32> to get 32-character strings from 32-bit integers, or std::bitset32> to get 32-character strings from 32-bit integers. #include <iostream> #include <bitset> int main() { ...READ MORE

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

Why we actually need runtime polymorphism?

One of the most significant elements of ...READ MORE

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

What is the best way to use a HashMap in C++?

The ordered and unordered map containers (std::map and std::unordered map) are included in the standard library.  The items in an ordered map are sorted by key, and insert and access are in O (log n).  For ordered maps, the standard library often use red black trees.  However, this is only an implementation detail.  Insert and access are in O in an unordered map (1).  It is simply another term for a hashtable. An illustration using (ordered) std::map: #include <map> #include <iostream> #include <cassert> int main(int argc, char ...READ MORE

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

How does virtual inheritance solve the "diamond" (multiple inheritance) ambiguity?

You desire: (Achievable with virtual inheritance) ...READ MORE

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

In C++, what is a virtual base class?

When employing multiple inheritance, virtual base classes are used to prevent several "instances" of a particular class from appearing in an inheritance hierarchy. Consider the following example: class A { public: void Foo() {} ...READ MORE

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

Are virtual functions the only way to achieve Runtime Polymorphism in C++?

fprintf is a polymorphism function in the C programming language. It can print to a file, stdout, a printer, a socket, or whatever else the system can represent as a stream if you supply it different handles. FILE* file = fopen("output.txt", "w"); ...READ MORE

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

C++ binary operator overloading

The language defines several fundamental types, such as int and double.  Objects are instances of fundamental kinds. Let's pretend you have: struct Foo { ... }; You can use non-member functions to overload the operator+ function. Foo operator+(Foo , ...READ MORE

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

Constructor Overloading in C++

In your function Object() { [native code] }, Node(Game(),v); does not work as expected.  It simply creates a temporary without actually using it, and has no effect.  When control passes over the ;, it immediately destroys the temporary. Initializing the members in each function Object() { [native code] } is the correct way to go.  You could put their shared code in a private init() member function and call it from each function Object() { [native code] }, as shown below: class Foo { public: ...READ MORE

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

Use of min and max functions in C++

The functions fmin and fmax are designed ...READ MORE

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

static memory allocation like dynamic memory allocation

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

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
347 views
0 votes
0 answers

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

The code below shows how to call a function in both methods.  Please explain the major differences or meanings of call by value and call by reference to me.  1.Make a value-based call.  2.Call based on a reference.  The call by value method is demonstrated in the following code. In a comment, I expressed my reservations. #include<iostream> int main(){ void change(int);//why function prototype is before ...READ MORE

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

Use of min and max functions in C++

Are std::min and std::max better than fmin ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
340 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
568 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

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
432 views
0 votes
0 answers

Constructor Overloading in C++

My C++ overloading does not work as I expect it to: #include "Node.h" #include <iostream> Node::Node() { cout ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
241 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
436 views
0 votes
1 answer

Function default argument value depending on argument name in C++ [duplicate]

When the function is called with no argument for the corresponding parameter, the default argument is evaluated.  In a default argument, a parameter must not appear as a potentially evaluated expression.  A function's parameters declared before a default argument are in scope and can obscure the namespace and class member name. It provides the following example: int h(int a, ...READ MORE

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

The new syntax "= default" in C++11

A defaulted default function Object() { [native code] } is defined as a user-defined default function Object() { [native code] } with an empty compound statement and no initialization list. I'll give you an example to demonstrate the difference: #include <iostream> using namespace std; class A { public: ...READ MORE

Jun 7, 2022 in C++ by Damon
• 4,960 points
316 views
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
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
476 views
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
872 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
320 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
614 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
271 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,573 views