Most viewed questions in C++

0 votes
1 answer

Passing a 2D array to a C++ function

There are three ways to pass a ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
27,596 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
16,011 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,473 views
0 votes
1 answer

Why can't the switch statement be applied on strings?

The reason for this is due to ...READ MORE

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

How does #include <bits/stdc++.h> work in C++? [duplicate]

#include <bits/stdc++.h> is a precompiled header implementation ...READ MORE

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

Is there any built-in factorial function in c++?

Although no C function is written particularly for computing factorials, the C math package allows you to compute the gamma function. Because (n) Equals (n-1)!  Using tgamma of i+1 on positive integers returns i!. for (int i = 1 ; ...READ MORE

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

How to convert string to char array in C++?

Simplest way I can think of doing ...READ MORE

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

Finding square root without using sqrt function?

I tried to incorporate the technique I had discovered for calculating the square root without utilising the sqrt function into programming.  I finally have this C++ code that runs. #include <iostream> using ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
3,652 views
0 votes
1 answer

Difference between function overloading and method overloading

They are interchangeable. Some people, on the other hand, prefer calling methods, functions that are part of a class, and functions, free functions. //function overloading void foo(int x); void foo(int x, int ...READ MORE

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

how could I use the power function in c/c++ without pow(), functions, or recursion

It is part of a series.  Replace pow() with the previous iteration's value. There is no need for code to call pow ().  Pow(x, 5 * I - 1) and pow(-1, I - 1) may be formed since both have an int exponent dependent on the iterator I from the previous loop iteration. Example: Let f(x, i) = pow(x, 5 * i ...READ MORE

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

How can I terminate an infinite loop in Turbo C?

I get caught in an infinite loop.  How can I break this cycle?  Cntrl-C was attempted, however it had little effect.  I have no idea how to stop it. main() { ...READ MORE

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

Sorting a vector in descending order

Should I  utilize  std::sort(numbers.begin(), numbers.end(), std::greater<int>()); or std::sort(numbers.rbegin(), numbers.rend()); // ...READ MORE

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

When to use extern in C++

This is useful when dealing with global variables.  Global variables are declared in a header so that any source file that contains the header is aware of them, but you only need to "define" them once in one of your source files. To explain, using extern int x; informs the compiler that an int object named x exists elsewhere.  It is not the compiler's responsibility to know where it exists; it just needs to know the type and name so that it may utilise it.  After compiling all of the source files, the linker will resolve all x references to the one definition found in one of the generated source files. For it to operate, the x variable's declaration must have "external linkage," which simply means that it must be defined outside of a function (at what's known as "the file scope") and without the static keyword. header: #ifndef HEADER_H #define HEADER_H // any source file that ...READ MORE

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

What is the difference between operator overloading and operator overriding in C++?

Some people use the latter word to ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
1,684 views
0 votes
0 answers

How to traverse stack in C++?

Is traversing std::stack possible in C++? It is not possible to traverse using the following method.  Because there is no member end in std::stack. std::stack<int> foo; // .. for (__typeof(foo.begin()) it = foo.begin(); ...READ MORE

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

What is dynamic initialization of object in c++?

Dynamic initialization occurs when the initialization value is unknown at compile time.  To initialise the variable, it is calculated at runtime. Example, int factorial(int n) { ...READ MORE

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

How to convert an instance of std::string to lower case

#include <algorithm> #include <cctype> #include <string> std::string data = "Abc"; std::transform(data.begin(), ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
1,445 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,351 views
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,340 views
0 votes
1 answer

Ternary operator, if I avoid writing ' expression 2 ' it works but if I not write 'expression 3 ' it gives an error [duplicate]

6.8 Conditionals with Missing Opponents In a ...READ MORE

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

C++ code file extension? What is the difference between .cc and .cpp [closed]

GNU GCC recognizes all of the following ...READ MORE

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

How to copy turbo c++ output?

How can I replicate the output of Turbo C++?  I had already Googled the issue, but in vain.  It suggests to either print scrn and paste or right click, select all, and paste.  I tried both, but neither worked.  The issue is that it simply copies what is on the current screen.  But I want the entire screen from the start.  (Alt+printscrn is also ineffective).  What should I do in this situation? printScrn Alt+printScrn markall None of them are operational!! I can't assist you if you require this archaic technique of programming for whatever reason, but I'd want to find a solution.  I tried forwarding the output stream to the file in this manner, but it did not work. #include<iostream.h> #include<conio.h> #include<stdlib.h> const int max=50; class dequeue{ int dq[max],r,f,c,x,i; public: dequeue(); void insertRear(); void insertFront(); void ...READ MORE

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

Vector of Vectors to create matrix

Before accessing any elements, you must first set the vector of vectors to the right size.  You may do it this way: // assumes using std::vector for brevity vector<vector<int>> matrix(RR, ...READ MORE

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

How can I get all the unique keys in a multimap

This method worked for me. for( multimap<char,int>::iterator it ...READ MORE

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

What's the difference between constexpr and const?

Variables are protected from modification in your ...READ MORE

Aug 5, 2022 in C++ by Damon
• 4,960 points
1,002 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
993 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 988 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
987 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 to initialize an std::string with a length?

How can I correctly initialise a string if its length is specified at build time? #include <string> int length = 3; string word[length]; //invalid ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
962 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
944 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
936 views
0 votes
0 answers

Difference between upper_bound and lower_bound in stl

I was looking at how the upper bound and lower bound algorithms operate in stl on these pages: lower bound, upper bound, and it's documented the same way on these pages: lower bound, upper bound, and it's documented the same way on these pages: lower bound, upper bound, and it'  upper bound, lower bound Looking at the code from the links, they appear to perform the same thing to me, with the exception of the following lines  lower_bound (line 10): if (*it<val) { ...READ MORE

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

How to use vector::push_back()` with a struct?

How do I push a struct back into a vector? struct point { int ...READ MORE

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

c++ constructor and copy constructor

In a pre-C++17 compiler, this is known as copy elision (test it with C++17 on compiler explorer or wandbox with -std=c++17 vs. -std=c++14 flags).  As of C++17, the compiler must delete numerous instances of copy and move constructors and create objects directly without the need of intermediary objects. Unlike A a { 10 }; the line A a ...READ MORE

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

How do I use 'strlen()' on a C++ string

In C++, there are two standard methods for storing strings.  In this scenario, you specify an array of characters, and 0 signifies the end of the string. #include <cstring> char str[500] = "Hello"; // How ever ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
845 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
841 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
1 answer

Why is Turbo C++ showing this error in DOSBox on my Mac?

Your installation must be defective!  I have a Mac, and I'm typing this on it while using TurboC++.  Consider uninstalling and then reinstalling the programme. Download the package in the same way as you would a.dmg programme from the internet.  (For example, drag and drop the programme into the Applications folder)  Ascertain that your Applications folder is global to your system.  This is what I mean: When in Finder, select the "GO" option from the top menu bar. From the drop down option, choose "Computer." In the newly opened window, click on your hard disc. There is a "Applications" folder there.  That's where you should put TurboC++. Go to Launchpad, and start Turbo C++. ...READ MORE

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

C++ convert from 1 char to string?

I simply need to cast one character to string.  The inverse method is as easy as str[0]. The following suggestions did not work for me: char c = 34; string(1,c); //this doesn't work, the ...READ MORE

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

Difference between Turbo C++ and Borland C++ compiler [closed]

I will try my best to respond, ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
804 views