C++ Articles

Page 581 of 597

I/O Redirection in C++

George John
George John
Updated on 30-Jul-2019 819 Views

In C, we can use the freopen() function for redirection purposes. Using this function, we can redirect existing FILE pointer to another stream. The syntax of the freopen is like below:FILE *freopen(const char* filename, const char* mode, FILE *stream)In C++ also, we can do the redirection. In C++, the streams are used. Here we can use our own stream, and also redirect system streams. In C++, there are three types of streams.istream : Stream, that can support input onlyostream : Stream, that can support output onlyiostream : These can be used for input and output.These classes, and file stream classes ...

Read More

Difference between files written in binary and text mode in C++

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 737 Views

Text modeBinary modeIn text mode various character translations are performed i.e;“\r+\f” is converted into “”In binary mode, such translations are not performed.To write in the files:ofstream ofs (“file.txt”);Orofstream ofs;ofs.open(“file.txt”);to write in the files: ofstream ofs(“file.txt”, ios::binary);orofstream ofs;ofs.open(“file.txt”, ios::binary);To add text at the end of the file:Ofstream ofs(“file.txt”, ios::app);orofstream ofs;ofs.open(“file.txt”, ios::app);To add text at the end of the file:Ofstreamofs(“file.txt”, ios::app|ios::binary);or ofstream ofs;ofs.open(“file.txt”, ios::app|ios::binary);To read files:ifstream in (“file.txt”);orifstreamin ; in.open(“file.txt”);To read files: ifstream in (“file.txt”, ios::binary);orifstream in ;in.open(“file.txt”, ios::binary);

Read More

How to read file content into istringstream in C++?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 2K+ Views

Here is a C++ program to read file content into isstringstream in C++.Example#include #include #include using namespace std; int main() {    ifstream is("a.txt", ios::binary );    // get length of file:    is.seekg (0, std::ios::end);    long length = is.tellg();    is.seekg (0, std::ios::beg);    // allocate memory:    char *buffer = new char [length];    // read data as a block:    is.read (buffer,length);    // create string stream of memory contents    istringstream iss( string( buffer ) );    cout

Read More

What is difference between a pointer and reference parameter in C++?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 7K+ Views

PointersPointer variables are used to store the address of variable.SyntaxType *pointer;InitializationType *pointer; Pointer=variable name;ReferencesWhen a parameter is declared as reference, it becomes an alternative name for an existing parameter.SyntaxType &newname=existing name;InitializationType &pointer; Pointer=variable name;The main differences between pointers and reference parameters are −References are used to refer an existing variable in another name whereas pointers are used to store address of variable.References cannot have a null value assigned but pointer can.A reference variable can be referenced by pass by value whereas a pointer can be referenced by pass by reference.A reference must be initialized on declaration while it is not ...

Read More

Proper stack and heap usage in C++?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 782 Views

The stack − All variables declared inside the function will take up memory from the stack. So, any local variable inside a function lives on the stack.The heap − This is unused memory of the program and can be used to allocate the memory dynamically when program runs. So If we want something to live longer than the function that declared it, we must allocate it on the heap.Exampleint main() {    int a; //get memory allocated on stack.    int *ptr=new int[7]; //memory for 7 integers allocated on heap. }The main issue in heap memory is fragmentation whereas memory ...

Read More

A Boolean Matrix Question in C++?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 346 Views

Here we will see one interesting Boolean matrix problem. One Boolean matrix is given which contains 0’s and 1’s. Our goal is to find where 1 is marked. If the 1 is marked at position mat[i, j], then we will make all entries to 1 of the row i and column j. Let us see an example. If the matrix is like below −1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0Then after modification, it will be −1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1AlgorithmmatrixUpdate(matrix[R, ...

Read More

C++ Program for Sum of squares of first n natural numbers?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 691 Views

In this problem we will see how we can get the sum of squares of first n natural numbers. Here we are using one for loop, that runs from 1 to n. In each step we are calculating square of the term and then add it to the sum. This program takes O(n) time to complete. But if we want to solve this in O(1) or constant time, we can use this series formula −AlgorithmsquareNNatural(n)begin    sum := 0    for i in range 1 to n, do       sum := sum + i^2    done    return ...

Read More

C++ tricks for competitive programming (for C++ 11)?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 392 Views

Here we will see some good tricks of C++ programming language that can help us in different area. Like if we want to participate in some competitive programming events, then these tricks will help us to reduce the time for writing codes. Let us see some of these examples one by one.Checking whether a number is odd or even without using % operator. This trick is simple. We can perform bitwise AND operation with the number and 1. If the result is non-zero then this is odd, otherwise this is even. The logic is too simple. All odd numbers have ...

Read More

C++ Program for Comb Sort?

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Jul-2019 423 Views

The basic idea of comb sort and the bubble sort is same. In other words, comb sort is an improvement on the bubble sort. In the bubble sorting technique, the items are compared with the next item in each phase. But for the comb sort, the items are sorted in a specific gap. After completing each phase, the gap is decreased. The decreasing factor or the shrink factor for this sort is 1.3. It means that after completing each phase the gap is divided by 1.3. Time Complexity is O(n log n) for best case. O(n2/2nP) (p is number of ...

Read More

C++ Program to Generate a Sequence of N Characters for a Given Specific Case

Samual Sam
Samual Sam
Updated on 30-Jul-2019 471 Views

This is a C++ program to generate a sequence of N characters for a given specific case.AlgorithmsBegin    function GenerateSequence() generate a Sequence of N Characters for a Given Specific Case:       Use rand() for generating random indexes.       Store the first character directly into the sequence.       If that sequence is used earlier, then it discards that and generates random index again. EndExample#include #include #include using namespace std; void GenerateSequence(char string[], int n, int l, char *sequence) {    int i, j=0, k, in;    for(i = 0; i < n; i++) { ...

Read More
Showing 5801–5810 of 5,962 articles
« Prev 1 579 580 581 582 583 597 Next »
Advertisements