Most voted questions in C++

0 votes
0 answers

Deep copy vs Shallow Copy

What's the distinction between deep and shallow ...READ MORE

Jun 29, 2022 in C++ by Nicholas
• 7,760 points
546 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
1,084 views
0 votes
0 answers

Selection sort in C++ (modified) not working for all cases

I was attempting to tweak the selection sort code in C++ in order to test the results.  My updated code is as follows: #include<bits/stdc++.h> using namespace std; int main() { ...READ MORE

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

What is the STL?

I'm not a C++ coder, thus I ...READ MORE

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

Difference between set.upper_bound() and upper_bound(set.begin(), set.end()) stl

I discovered that set.upper bound() was quicker ...READ MORE

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

Why are exceptions so rarely used in C++

I've been writing in C++ on and ...READ MORE

Jun 29, 2022 in C++ by Damon
• 4,960 points
320 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,560 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,131 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
678 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
9,093 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
558 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
542 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
706 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
444 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
512 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
733 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
495 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
1,004 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
494 views
0 votes
0 answers

Sorting Characters Of A C++ String

Is there a built-in method for sorting characters in a string, or do I have to construct my own? for instance: string word = "dabc"; I would want to ...READ MORE

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

How to use the priority queue STL for objects?

class Person { public: int age; }; I'd want to put objects of the type Person in a priority queue. priority_queue< ...READ MORE

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

Can you use "cin" with string?

To enter a string, I was instructed to use gets(str) rather than cin.  In the programme below, though, I can use cin perfectly well.  Could you please tell me if you can use cin?  Please accept my apologies for my poor English.  The application allows you to enter up to five names and then print them on the screen. The code is as follows: #include <iostream> #include <string.h> using namespace std; int main() { ...READ MORE

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

C++ pointer to objects

Is it always necessary in C++ to  ...READ MORE

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

How can I get the maximum or minimum value in a vector?

In C++, how can I find the greatest or minimum value in a vector? Is it correct to assume that it would be similar with an array? Do I require an iterator?  I tried max element, but I kept receiving errors. vector<int>::const_iterator it; it = max_element(cloud.begin(), cloud.end()); error: request for ...READ MORE

Jun 27, 2022 in C++ by Nicholas
• 7,760 points
557 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
584 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
597 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,366 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
659 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,486 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
547 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,513 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,408 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,340 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
745 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
911 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,505 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
489 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,160 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
472 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,500 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,585 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
605 views
0 votes
0 answers

Difference between 'new operator' and 'operator new'?

What is difference between "new operator" and ...READ MORE

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

Use of "this" keyword in C++ [duplicate]

Yes, it is optional and generally omitted.  However, it may be essential for accessing variables after they have been overridden in the scope: Person::Person() { int age; ...READ MORE

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

What is the name of the "<<" and ">>" operators? [duplicate]

According to cplusplus.com's documentation: This operator (<<) applied to ...READ MORE

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

How to use new[ ] and delete[ ] operator in C++

int main(){ char *str; ...READ MORE

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

abstract class and virtual functions

Is this class considered abstract because it only contains one virtual function?  I could still build an Animal object and use getFoodCost(); No. In C++, a "Abstract Class" is often a class having a single pure virtual function: class Abstract { public: virtual ...READ MORE

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

C++ override inherited methods

Child::init is now concealing Father::init and not overriding it.  In order to receive dynamic dispatch, your init member function must be virtual: virtual void init () { ...READ MORE

Jun 20, 2022 in C++ by Damon
• 4,960 points
561 views