Most answered questions in C++

0 votes
1 answer

Suggestions of excellent examples of real C/C++ code

I'd like to particularly bring up memcached.  ...READ MORE

Dec 17, 2022 in C++ by narikkadan
• 63,720 points
566 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,245 views
0 votes
1 answer

How do I create an array of pointers?

Student** db = new Student*[5]; // To allocate ...READ MORE

Aug 5, 2022 in C++ by Damon
• 4,960 points
862 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
591 views
0 votes
1 answer

Create a reverse LinkedList in C++ from a given LinkedList

A simpler solution is to just let the current node point to the previous node while going through your linked list, saving the previous and next nodes as you go. void LinkedList::reversedLinkedList() { if(head == ...READ MORE

Aug 5, 2022 in C++ by Damon
• 4,960 points
690 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
527 views
0 votes
1 answer

Socket Programming in C++

The C++ Standard does not have a ...READ MORE

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

"We do not use C++ exceptions" — What's the alternative? Let it crash?

If you don't utilise exceptions, by definition, ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
477 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,875 views
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
29,988 views
0 votes
1 answer

Examples of good gotos in C or C++

Here's one method I've heard of folks use. But I've never seen it in the wild.  And that only pertains to C since RAII allows you to accomplish this more idiomatically in C++. void foo() { if (!doA()) ...READ MORE

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

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
862 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,872 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
7,162 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,991 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
1,134 views
0 votes
1 answer

What is the difference between public, private, and protected inheritance in C++?

To begin answering that question, let me characterise member accessors in my own terms.  If you already know this, proceed to the section "next:". I'm aware of three types of accessors: public, protected, and private. Let: class Base { public: ...READ MORE

Jul 11, 2022 in C++ by Damon
• 4,960 points
707 views
0 votes
1 answer

C++ string input

To read from cin, use a std::string.  Then you don't have to guess how big your buffer should be. std::string input; cin >> input; cout << intput; If you ...READ MORE

Jul 11, 2022 in C++ by Damon
• 4,960 points
464 views
0 votes
1 answer

Sorting vector elements in descending order

Because of the goto reset instruction, I believe your sort function has entered an infinite loop.  If you wish to construct a basic bubble-sort algorithm, follow these steps: #include <iostream> #include <utility> #include <vector> void bubble_sort(std::vector<int>& v) { ...READ MORE

Jul 11, 2022 in C++ by Damon
• 4,960 points
945 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,530 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
1,083 views
0 votes
1 answer

*this vs this in C++

This is a pointer, and *this is a pointer that has been dereferenced. If you had a function that returned this, it would be a pointer to the current object, but a function that returned *this would be a "clone" of the current object, created on the stack unless you defined the method's return type to be a reference. A small application that demonstrates the difference between working with copies and working with references: #include <iostream> class Foo { public: ...READ MORE

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

Is 'If Else' statement indentation important or not in C++? [duplicate]

White space has no effect on the understanding of code in C and C++.  That is not to say that the programmer should be unconcerned about its misuse. The easiest method to demonstrate what the above code truly represents is to specify all of the inferred braces directly, as seen below.  The 'if then' or 'otherwise' clause only affects one line of code in the if statement with no brackets. This is one of the reasons why people strive to insist on 'proper coding standards' to guarantee that other people can clearly grasp the programmer's flow and meaning. while(c != cols) { ...READ MORE

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

please help me with max_element function in c++ stl

You can substitute max for *max eleme ...READ MORE

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

Use of multisets in C++ [duplicate]

Because a multi-set does not need the storage of single-element objects.  You're considering storing anything in a multi-set, such as a string.  But it is not its intended use.  You may use whatever struct you want and compare it to a single element in the struct. As an example: struct PhoneBookEntry { std::string name; ...READ MORE

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

The static keyword and its various uses in C++

Static variables exist during the "lifetime" of the translation unit in which they are declared, and: It cannot be accessible from any other translation unit if it is in a namespace scope (i.e. outside of functions and classes).  This is referred to as "internal linking" or "static storage lifetime."  (Except for constexpr, do not do this in headers; otherwise, you would wind up with a different variable in each translation unit, which is really confusing.) If it is a variable in a function, it, like any other local variable, cannot be accessed from outside the function.  (This is the mentioned local) Class members have no limited scope owing to static, but they may be referenced from both the class and an instance (like std::string::npos). locations as code: static std::string namespaceScope = "Hello"; void ...READ MORE

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

How do I reverse a C++ vector?

The algorithm header has a method std::reverse for this purpose. #include <vector> #include <algorithm> int main() { std::vector<int> ...READ MORE

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

Initializing a two dimensional std::vector

Assume you wish to start a 2D vector, m*n, with a value of 0. We could do it. #include<iostream> int main(){ int ...READ MORE

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

Is there an easy way to make a min heap in C++?

Use make heap() and its buddies from algorithm>, or priority queue from queue>.  Make heap and friends are used by priority queue. #include <queue> // functional,iostream,ctime,cstdlib using namespace std; int main(int ...READ MORE

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

What does the explicit keyword mean?

To resolve the parameters to a function, the compiler is permitted to do one implicit conversion.  This implies that the compiler can utilise constructors with a single argument to convert from one type to another to find the correct type for a parameter. Here's an example class with a constructor ...READ MORE

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

C++ multiset, return key at position?

A set (or multiset) is typically represented ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
349 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
577 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
1,297 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
622 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,427 views
0 votes
1 answer

Using :: (scope resolution operator) in C++

You're mostly correct regarding cout and cin. ...READ MORE

Jun 27, 2022 in C++ by Damon
• 4,960 points
520 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
8,399 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
4,219 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
2,273 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
672 views
0 votes
1 answer

insert method for doubly linked list C++

I attempted to repair all of your methods, and I believe I succeeded; at least, the current test example prints the proper answer: #include <iostream> #include <vector> using namespace std; struct Node { ...READ MORE

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

What is a Class and Object in C++?

A Class is like a blueprint, an ...READ MORE

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

Explanation of function pointers

It's a little perplexing. Function type and pointer to function type are distinct kinds (no more similar than int and pointer to int).  However, in virtually all cases, a function type decays to a reference to a function type.  In this context, rotting roughly refers to conversion (there is a difference between type conversion and decaying, but you are probably not interested in it right now). What matters is that practically every time you use a function type, you end up with a reference to the function type.  But take note of the nearly - almost every time is not always! And there are rare circumstances where it does not. typedef void(functionPtr)(int); functionPtr fun = function; This code tries to clone one function to another (not the pointer! the function!)  However, this is not feasible since functions in C++ cannot be copied.  The compiler does not let this, and I'm surprised you got it compiled (you say you got linker errors?) Now for the code: typedef void(functionPtr)(int); functionPtr function; function(5); function does ...READ MORE

Jun 21, 2022 in C++ by Damon
• 4,960 points
449 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,443 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
5,497 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
570 views