Trending questions in C++

0 votes
0 answers

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

Is indentation in an if else statement important for code execution, or is it just something to do for cleaner code? while(c != cols) { ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
5 views
0 votes
0 answers

Use of multisets in C++ [duplicate]

I understand the use of sets in ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
5 views
0 votes
0 answers

The static keyword and its various uses in C++

The term static has numerous meanings in ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
4 views
0 votes
0 answers

please help me with max_element function in c++ stl

Please help me understand why this is providing an issue; I want to determine the maximum number of elements without utilising any extra space. Code: #include <bits/stdc++.h> using namespace std; ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
0 answers

How do I reverse a C++ vector?

Is there a vector function in C++ ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
0 answers

Initializing a two dimensional std::vector

So, I have the following: std::vector< std::vector <int> ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
0 answers

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

I'm new to C++ and was wondering ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
0 answers

What does the explicit keyword mean?

What does the explicit keyword mean in ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
3 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

8 hours ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
0 answers

C++ multiset, return key at position?

According to the documentation, the multiset data ...READ MORE

8 hours ago in C++ by Nicholas
• 2,360 points
3 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

8 hours ago in C++ by Nicholas
• 2,360 points
3 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

9 hours ago in C++ by Nicholas
• 2,360 points
3 views
0 votes
0 answers

C++ pointer to objects

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

9 hours ago in C++ by Nicholas
• 2,360 points
3 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

9 hours ago in C++ by Nicholas
• 2,360 points
3 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

9 hours ago in C++ by Nicholas
• 2,360 points
3 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

6 days ago in C++ by Damon
• 3,380 points
17 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

6 days ago in C++ by Damon
• 3,380 points
13 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

6 days ago in C++ by Damon
• 3,380 points
12 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

6 days ago in C++ by Damon
• 3,380 points
11 views
0 votes
1 answer

What range of values can integer types store in C++

You may rely on the following minimal ...READ MORE

6 days ago in C++ by Damon
• 3,380 points
10 views
0 votes
0 answers

c++ constructor and copy constructor

/* code block 1 */ #include <iostream> class A { ...READ MORE

5 days ago in C++ by Nicholas
• 2,360 points
8 views
0 votes
0 answers

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

I'm studying C++ and I never know ...READ MORE

5 days ago in C++ by Nicholas
• 2,360 points
7 views
0 votes
0 answers

Binary literals?

In code, I've seen constants specified in hex format like this: const int has_nukes ...READ MORE

5 days ago in C++ by Nicholas
• 2,360 points
6 views
0 votes
0 answers

In C++ abs( *a - *b) does not return absolute value of negative number

#include <stdio.h> #include <cmath> void update(int *a,int *b) { ...READ MORE

5 days ago in C++ by Nicholas
• 2,360 points
6 views
0 votes
0 answers

How can I get all the unique keys in a multimap

I have a multimap and I want to save all of the unique keys in it in a vector. multimap<char,int> mymm; multimap<char,int>::iterator it; char ...READ MORE

5 days ago in C++ by Nicholas
• 2,360 points
6 views
0 votes
1 answer

What is a Class and Object in C++?

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

6 days ago in C++ by Damon
• 3,380 points
12 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

6 days ago in C++ by Damon
• 3,380 points
10 views
0 votes
1 answer

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

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

6 days ago in C++ by Damon
• 3,380 points
8 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

6 days ago in C++ by Damon
• 3,380 points
6 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

6 days ago in C++ by Damon
• 3,380 points
4 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 in C++ by Damon
• 3,380 points
14 views
0 votes
1 answer

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

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

Jun 20 in C++ by Damon
• 3,380 points
10 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 in C++ by Damon
• 3,380 points
9 views
0 votes
1 answer

How to use enums in C++

This will be sufficient to declare your ...READ MORE

Jun 20 in C++ by Damon
• 3,380 points
8 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 in C++ by Damon
• 3,380 points
7 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 in C++ by Damon
• 3,380 points
6 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 in C++ by Damon
• 3,380 points
5 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 in C++ by Damon
• 3,380 points
5 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 in C++ by Damon
• 3,380 points
4 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 in C++ by Damon
• 3,380 points
17 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 in C++ by Damon
• 3,380 points
14 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 in C++ by Damon
• 3,380 points
13 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 in C++ by Damon
• 3,380 points
13 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 in C++ by Damon
• 3,380 points
8 views
0 votes
0 answers

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

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

Jun 15 in C++ by Nicholas
• 2,360 points
7 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 in C++ by Damon
• 3,380 points
9 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 in C++ by Damon
• 3,380 points
7 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 in C++ by Damon
• 3,380 points
5 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 in C++ by Damon
• 3,380 points
4 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 in C++ by Damon
• 3,380 points
4 views