Trending questions in C++

0 votes
1 answer

What does Tokens do and why they need to be created in C++ programming?

Tokenization is essential in determining what a programme does.  What Bjarne is referring to in respect to C++ code is how tokenization rules alter the meaning of a programme.  We need to know what the tokens are and how they are determined.  Specifically, how can we recognise a single token when it comes among other characters, and how should tokens be delimited if  there is ambiguity? Consider the prefix operators ++ and +, for example. Assume we have just one token + to deal with.  What does the following excerpt mean? int i = 1; ++i; Is the above going to apply unary + on i twice with + only? Or will it only increase it once? Naturally, it's vague.  We require an additional token, thus ++ is introduced as its own "word" in the language. But there is now another (though minor) issue.  What if the programmer just wants to use unary + twice without incrementing?  Rules for token processing are required.  So, if we discover that a white space is always used as a token separator, our programmer may write: int i ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
1,222 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
664 views
0 votes
0 answers

How to initialize an std::string with a length?

How can I correctly initialise a string if its length is specified at build time? #include <string> int length = 3; string word[length]; //invalid ...READ MORE

Jul 27, 2022 in C++ by Nicholas
• 7,760 points
1,328 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
630 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
921 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
745 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
472 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
980 views
0 votes
0 answers

Fast input/output in c++

In a programming question, I need to ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 43 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
755 views
0 votes
0 answers

#defining constants in C++

In various C code, I see constants ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 27 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
718 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
927 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
501 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
499 views
0 votes
0 answers

Is it possible to get a single element of a set in C++ STL?

I have the following C++ code with ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 21 views
0 votes
0 answers

What is the difference between private and protected members of C++ classes?

What distinguishes C++ classes' private and protected ...READ MORE

Aug 26, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 19 views
0 votes
0 answers

Rules for C++ string literals escape character

What are the string literals' escape character rules?  Are all the characters that are escaped listed somewhere? In particular, gedit colours the following three values differently when I use the symbol in a string literal. I was attempting to generate a std::string from a literal consisting of the character 0, the null character (0), and the character 0.  However, the syntax highlighting warned me that this would result in just two characters, such as the letter 0 followed by the null character (\00, aka \0). Is the following the best course of action for the solution to just this one issue: std::string ("0\0" "0", 3) // ...READ MORE

Aug 25, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 42 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
647 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
686 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
748 views
0 votes
0 answers

When should you use 'friend' in C++?

I like the concept of the << ...READ MORE

Aug 25, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 24 views
0 votes
0 answers

Confused with object arrays in C++

I originally studied Java, and I'm currently attempting to transition to C++.  I'm having some trouble getting arrays to function properly. Right now, all I'm attempting to do is build an array of the object "Player" and add one to it.  But I encounter a problem. Player* players = new Player[1]; players[0] = new ...READ MORE

Aug 25, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 19 views
0 votes
0 answers

How to use vector::push_back()` with a struct?

How do I push a struct back into a vector? struct point { int ...READ MORE

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

In C++, what is the difference between a method and a function

I'm making an effort to use the ...READ MORE

Aug 24, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 19 views
0 votes
0 answers

Alternative to itoa() for converting integer to string C++?

If I use itoa() to convert an ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 55 views
0 votes
0 answers

Alternative function in iostream.h for getch() of conio.h?

I'm attempting to hold the screen on ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 44 views
0 votes
0 answers

Printing prime numbers from 1 through 100

The following prime numbers are printed out by this C++ code: 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 73 79 83 89 97. However, I don't believe that is how my book would prefer it to be written.  It makes a reference to the square root of a number.  I thus tried switching my second loop to for (int j=2; jsqrt(i); j++), but it did not produce the desired outcome. How would I modify this code to make it the way my book desires it to be? int main () { ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 45 views
0 votes
0 answers

Why can we not access elements of a tuple by index?

tuple <int, string, int> x=make_tuple(1, "anukul", 100); cout ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 44 views
0 votes
0 answers

Remove a key from a C++ map

I want to take a key out ...READ MORE

Aug 23, 2022 in C++ by anonymous
• 7,760 points

edited Mar 4 44 views
0 votes
0 answers

Passing vector by reference

Using standard C arrays, I would carry out the following: void do_something(int el, int **arr) { ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 44 views
0 votes
0 answers

Generic Classes in C++

Generic Classes was the topic I was reading. I at one point became immobile.  Here is the relevant code: template <class StackType> class stack { ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 43 views
0 votes
0 answers

Why do we need abstract classes in C++?

I recently learnt about polymorphism in my ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 42 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
819 views
0 votes
1 answer

Socket Programming in C++

The C++ Standard does not have a ...READ MORE

Aug 2, 2022 in C++ by Damon
• 4,960 points
896 views
0 votes
0 answers

C++11: How to alias a function?

If I have a class Foo in ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 23 views
0 votes
0 answers

When should you use a class vs a struct in C++?

When using C++, when is it preferable ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 19 views
0 votes
0 answers

c++ custom compare function for std::sort()

I want to create custom compare function ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 18 views
0 votes
0 answers

How do you declare an interface in C++?

What is the best way to build ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 18 views
0 votes
0 answers

Is it more efficient to copy a vector by reserving and copying, or by creating and swapping?

I'm attempting to efficiently duplicate a vector. I see two potential strategies: std::vector<int> copyVecFast1(const std::vector<int>& original) { std::vector<int> newVec; ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 18 views
0 votes
0 answers

How to convert C++ Code to C

I've got some C++ programme. Numerous classes, ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 18 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,692 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
753 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,604 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
546 views
0 votes
0 answers

How can i sort a map by its .second parameter

How can I output all the int ...READ MORE

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

C++ Switch Cases

I was taking an online quiz based on the C++ switch statement.  I came across a question, and while I have a good knowledge of how switch statements function, this particular question made no sense to me.  Could you please explain? Why is the answer D and not ...READ MORE

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

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

I looked up the differences between cout, ...READ MORE

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

How does sizeof know the size of the operand array?

It could be a silly question, but ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points

edited Mar 4 47 views