Most voted questions in C++

+1 vote
0 answers

Using push_back vs at in a vector in C++

I'm not sure how to use vectors in C++.  It has to do with the vector's push back technique.  I used push back to insert entries into the vector in the first programme.  I used at() to insert entries into the vector in the second application. #include <iostream> #include <vector> #include <string> using namespace std; int main ...READ MORE

Jul 22, 2022 in C++ by Nicholas
• 7,760 points
234 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

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

Check if element found in array c++

How can I see if the element ...READ MORE

Nov 17, 2022 in C++ by Ashwini
• 5,430 points
435 views
0 votes
0 answers

Best way to split a vector into two smaller arrays?

I'm attempting to divide a vector into ...READ MORE

Nov 17, 2022 in C++ by Ashwini
• 5,430 points
799 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
336 views
0 votes
0 answers

Read whole ASCII file into C++ std::string

I need to load an entire file ...READ MORE

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

Making a class abstract without any pure virtual methods

I take a class where we watch ...READ MORE

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

Differentiate between function overloading and function overriding

Differentiate between function overloading and function overriding ...READ MORE

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

What is the point of function pointers?

The purpose of function pointers is difficult ...READ MORE

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

What are the differences between struct and class in C++?

I now want to understand the distinctions ...READ MORE

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

What does '&' do in a C++ declaration?

As a C person, I'm attempting to comprehend some C++ code.  The declaration of my function is as follows: int foo(const string &myname) { cout ...READ MORE

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

How to end C++ code

I want my C++ code to terminate  ...READ MORE

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

get absolute value without using abs function nor if statement

How might I obtain an integer's absolute ...READ MORE

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

How do I set up Visual Studio Code to compile C++ code?

Although the Visual Studio Code editor from ...READ MORE

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

What is the difference between std::list<std::pair> and std::map in C++ STL?

What distinguishes std::list<std::pair> from std::map? Does the ...READ MORE

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

Remove elements of a vector inside the loop

I am aware that there are questions like this one, but I was unable to use them to help me decipher my code.  I just want to remove or delete one element from a vector by running a loop over its property.  How can I go about doing that?  I tried the following code, but I got an ambiguous error message: 'operator =' function is unavailable in 'Player’. ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
391 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
328 views
0 votes
0 answers

memory allocation for objects

In C++, a variable like int x ...READ MORE

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

How does "this" cascading work?

The following class interface I have is: ...READ MORE

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

Why was the ampersand chosen as the symbol for references in C++?

Does anyone know why the ampersand was ...READ MORE

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

memory allocation for objects

When we instantiate a variable in C++, ...READ MORE

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

(->) arrow operator and (.) dot operator , class pointer

In C++, we know that given a class pointer, we use the (->) arrow operator to access the members of that class, as seen here: #include <iostream> using namespace std; class myclass{ ...READ MORE

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

What is a stream in C++?

I've been hearing a lot about streams, ...READ MORE

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

How to Access Subgraphs of an Existing Graph in Boost

I read a graph using read graphviz() ...READ MORE

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

Is there a maxheap in the C++ standard library?

I understand that the std::priority queue class ...READ MORE

Aug 11, 2022 in C++ by Nicholas
• 7,760 points
384 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
356 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
338 views
0 votes
0 answers

std::cin input with spaces?

#include <string> std::string input; std::cin >> input; The user want to type "Hello World."  However, cin fails to recognise the space between the two words.  How do I get Cin to see the entire Hello World? I'm trying this using structs, and cin.getline doesn't appear to work.  This is my code: struct cd { ...READ MORE

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

In C, are arrays pointers or used as pointers?

First, look at this code When this is ...READ MORE

Aug 11, 2022 in C++ by krishna
• 2,820 points
254 views
0 votes
0 answers

How to generate a random number in C++?

I'm attempting to develop a dice game, and I need random numbers to represent the sides of the die.  I can make a number between 1 and 6).  Using #include <cstdlib> #include <ctime> #include <iostream> using namespace ...READ MORE

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

declaring a priority_queue in c++ with a custom comparator

I'm attempting to declare a priority queue of nodes using the comparator function bool Compare(Node a, Node b) (which is outside the node class). I presently have the following: priority_queue<Node, vector<Node>, Compare> openSet; I'm receiving Error: "Compare" is not a type name for some reason. Adding priority_queue<Node, vector Node>, bool Compare> to the declaration. gives me the error 'Error: expecting a '>' I've also attempted: priority_queue<Node, vector<Node>, Compare()> openSet; priority_queue<Node, ...READ MORE

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

Confused with object arrays in C++

I have some Java knowledge and now ...READ MORE

Aug 11, 2022 in C++ by krishna
• 2,820 points
178 views
0 votes
0 answers

Is C++ considered weakly typed? Why?

C++ has always struck me as one ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
1,202 views
0 votes
0 answers

Difference between 'struct' and 'typedef struct' in C++?

In C++, is there any difference between: struct Foo ...READ MORE

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

How do you properly use namespaces in C++?

I come from a Java background where ...READ MORE

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

What are Containers/Adapters? C++

What exactly are containers/adapters? I am familiar ...READ MORE

Aug 8, 2022 in C++ by Nicholas
• 7,760 points
282 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
341 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
292 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
339 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
301 views
0 votes
0 answers

How can I get double quotes into a string literal?

I used the printf() command to produce the output seen below: printf("She said time flies like an arrow, ...READ MORE

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

std::string length() and size() member functions

While reading the responses to this topic, ...READ MORE

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

Insert object at index of vector c++

I have to add a new item to the existing object vector.  Although I am aware that an iterator must be used, I am not entirely sure how it operates. I need to insert a new item by name in the precise index that I found after some searching into an alphabetically sorted vector.  I have this, then. vector<Person>people; int index =54; Person temp; people.push_back(temp);//insert at end of ...READ MORE

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

How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)?

I wanna create a program that can ...READ MORE

Aug 5, 2022 in C++ by krishna
• 2,820 points
655 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
974 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
624 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
428 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
499 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
355 views