Most viewed questions in C++

0 votes
0 answers

What is the meaning of "generic programming" in c++?

What does generic programming in C++ mean? I'm ...READ MORE

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

How do you open a file in C++?

I want to open a file in ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
374 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
373 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

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
0 answers

how to print \" in C++

I need to print the following string: std::string("-I\"/path/to/dir\" "); Basically, I need to accomplish this since I am generating C++ code using C++ code. I want to use an ofstream to write the above string, so something like ofstream fout; fout << the_string ...READ MORE

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

How can I get the size of a C++ function?

In C++, how can I find the size& ...READ MORE

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

Efficient way to return a std::vector in c++

When delivering a std::vector in a function, how much data is duplicated, and how much of an optimization will it be to place the std::vector in free-store (on the heap) and provide a pointer instead, i.e. is: std::vector *f() { std::vector *result = new ...READ MORE

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

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

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

How to convert a char array to a string?

Converting a C++ string to a char ...READ MORE

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

How does c++ std::vector work?

How do the addition and removal of ...READ MORE

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

C++ `this` pointer

Pointer variables are used to store the ...READ MORE

May 31, 2022 in C++ by Damon
• 4,960 points
355 views
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,420 points
353 views
0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

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

dynamic_cast and static_cast in C++

 I'm perplexed by the dynamic cast keyword in C++. struct A { virtual ...READ MORE

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

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

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

Correct way to work with vector of arrays

Could someone please explain how to work ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points
348 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

Default inheritance access specifier

If I have two classes A and ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
341 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
340 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
0 answers

Why is the size of an empty class in C++ not zero?

Why does the following output 1? #include <iostream> class Test { }; int ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
337 views
0 votes
0 answers

Check if a string is palindrome

I need to write a software that ...READ MORE

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

Is the std::set iteration order always ascending according to the C++ specification?

According to what I've read, std::set in ...READ MORE

Jun 29, 2022 in C++ by Nicholas
• 7,760 points
337 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
336 views
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
335 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
333 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
0 answers

How come an array's address is equal to its value in C?

The pointer values and pointer addresses in ...READ MORE

Aug 8, 2022 in C++ by krishna
• 2,820 points
329 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
0 answers

Is there a default return value of C++ functions?

I'm amazed that a file with the  ...READ MORE

Jul 25, 2022 in C++ by Nicholas
• 7,760 points
325 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
325 views
0 votes
0 answers

Simple example of threading in C++

Could someone maybe provide a basic C++ ...READ MORE

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

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

May 31, 2022 in C++ by Damon
• 4,960 points
322 views
0 votes
0 answers

Nested try...catch inside C++ exception handler?

I may wish to run code that might throw an exception in my exception handler. Is the following C++ structure acceptable?  Are there any drawbacks if so? try { // ... } catch (const ...READ MORE

Aug 5, 2022 in C++ by Nicholas
• 7,760 points
321 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
321 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
321 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
320 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
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
318 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
316 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
0 answers

C++ printing boolean, what is displayed?

I print a bool to an output stream like ...READ MORE

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

What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE

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

Program to calculate square root c++

Making a C++ application to determine a number's square root.  The built-in arithmetic operator "sqrt" is not used in this application.  There are two variables: one for the user-inputted integer and the other for the number's square root.  This software does not function very well, and I am certain that there is a more effective method to accomplish it: Here is my full code: #include <iostream> using namespace ...READ MORE

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