Trending questions in C++

0 votes
0 answers

Do you use Qt and why do you use it?

Cons and advantages? How long does it ...READ MORE

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

What's the Use of '\r' escape sequence?

I have C code like this: #include<stdio.h> int main() { ...READ MORE

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

Inbuilt __gcd(A,B) function in C++

I recently learned about a c++ specia ...READ MORE

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

Purpose of Unions in C and C++

I had previously used unions with ease, but when I read this post and learned that this code union ARGB { uint32_t colour; ...READ MORE

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

what is containership or nesting in C++?

According to what I believed, a class ...READ MORE

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

How to use enums in C++

Suppose we have an enum like the following: enum Days ...READ MORE

Jul 7, 2022 in C++ by Nicholas
• 7,760 points
541 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,419 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
1,474 views
0 votes
0 answers

Infix to Postfix notation C++

Hello there, Stack.  I'm currently learning C++ and trying to create an RPN converter.  But I'm having difficulties.  Hopefully, I'll be able to describe the issues in detail.  For stacking my operators, I'm using an array.  Let's look at the example "5 + 8."  When it comes down to: else if(infix[i] == '+' or infix[i] == ...READ MORE

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

How to access static members of a class?

I'm learning C++ and Qt, but even the simplest code that I copy and paste from a book produces problems. On Ubuntu 10.04, I'm using g++4.4.2 with the QtCreator IDE.  Is there a distinction between the syntax of the g++ compiler and those of other compilers?  When I try to access static members, for example, something always goes wrong. #include <iostream> using namespace std; class A { ...READ MORE

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

Why doesn't C++ have a garbage collector?

First and foremost, I'm not raising this ...READ MORE

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

How to choose between map and unordered_map?

Assume I wanted to map data that ...READ MORE

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

Differences between C++ string == and compare()?

I just read some recommendations on using std::string ...READ MORE

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

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

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

Reverse a number without converting it to string in C++

I'm attempting to write a programme in C++ to reverse a number. It works well with numbers like 1234, but when I try to enter a number like 5430, it displays 345, and the same with numbers that begin with zero, such as 0234, which displays 432. Could someone please explain how to deal with zeros at the beginning and end of a sentence? I simply need to save the number without converting it to a string. #include<iostream> using namespace std; int main(){ ...READ MORE

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

What does the C++ standard state the size of int, long type to be?

I'm seeking for specific information on the sizes of basic C++ types.  I understand that it is determined by the architecture (16 bits, 32 bits, or 64 bits) and the compiler. But are there any C++ standards? On a 32-bit architecture, I'm using Visual Studio 2008.  This is what I get: char : 1 byte short : 2 ...READ MORE

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
413 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
1,224 views
0 votes
0 answers

Static vs dynamic type checking in C++

I'm curious in the differences between static ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
546 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, 2022 in C++ by Damon
• 4,960 points
1,231 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
927 views
0 votes
0 answers

C++ Singleton design pattern

I recently came upon a C++ realization/implementation of the Singleton design pattern.  It looked like this (I took it from a real-life example): // a lot of methods are omitted ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
384 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 1,240 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
502 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
565 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
571 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
1,100 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
680 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
565 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
502 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
596 views
0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
426 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
584 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
536 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
1,284 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
781 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
511 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
511 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
446 views
0 votes
0 answers

Why use #define instead of a variable

What is the purpose of the #define ...READ MORE

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

lower_bound == upper_bound

Lower bound: the initial greater-or-equal element. Upper bound: ...READ MORE

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

How to use c++ swap function?

It is OK to write swap(a,b); There is an issue when I write swap(&c[0],&d[0]);.  Can anyone explain why? #include<iostream> #include<algorithm> using namespace std; int main(void){ ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
341 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
463 views