- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
6K+ Views
Here is an example of Read integers from a text file with C++ ifstream.Example#include #include using namespace std; int main() { //initialie the array size int arr[30]; ifstream is("a.txt"); int cnt= 0; int x; // check that array is not already full while (cnt < arr[30] && is >> x) // and read integer from file arr[cnt++] = x; // print the integers stored in the array cout
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
1K+ 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
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
13K+ Views
To get a file’s size in C++ first open the file and seek it to the end. tell() will tell us the current position of the stream, which will be the number of bytes in the file.Example Live Demo#include #include using namespace std; int main() { ifstream in_file("a.txt", ios::binary); in_file.seekg(0, ios::end); int file_size = in_file.tellg(); cout
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
518 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
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
67 Views
The C++ function std::algorithm::lexicographical_compare() tests whether one range is lexicographically less than another or not. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries.Declarationtemplate bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);AlgorithmBegin result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end()) if (result == true) Print v1 is less than v2 result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end()) if (result == false) Print v1 is not less than v2 EndExample Live Demo#include #include #include #include using namespace std; int main(void) { //initialization ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
514 Views
Let us consider the following example, which uses an array of 3 integers −In CExample Live Demo#include const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { printf("Value of var[%d] = %d", i, var[i] ); } return 0; }OutputValue of var[0] = 10 Value of var[1] = 100 Value of var[2] = 200In C++Example Live Demo#include using namespace std; const int MAX = 3; int main () { int var[] = {10, 100, 200}; int i; for (i = 0; i < MAX; i++) { cout
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
315 Views
This is a C++ program in which we generate a undirected random graph for the given edges ‘e’. This algorithm basically implements on a big network and time complexity of this algorithm is O(log(n)).AlgorithmBegin Function GenerateRandomGraphs(), has ‘e’ as the number edges in the argument list. Initialize i = 0 while(i < e) edge[i][0] = rand()%N+1 edge[i][1] = rand()%N+1 Increment I; For i = 0 to N-1 Initialize count = 0 For j = 0 to e-1 ... Read More
![Nishtha Thakur](https://www.tutorialspoint.com/assets/profiles/13598/profile/60_104893-1512719058.jpg)
89 Views
Searching based on locality of reference, depends upon the memory access pattern data elements are reallocated.Here linear searching method is used to search an element.AlgorithmBegin int find(int *intarray, int n, int item) intialize comparisons = 0 for i = 0 to n-1 Increase comparisons if(item == intarray[i]) Print element with its index break if(i == n-1) Print element not found return -1 Print Total comparisons For j = i till i>0 intarray[j] = intarray[j-1] intarray[0] = ... Read More
![Smita Kapse](https://www.tutorialspoint.com/assets/profiles/13597/profile/60_103706-1512718957.jpg)
2K+ Views
Here we will see the Bind function and the placeholders in C++. Sometimes we need to manipulate the operation of some functions as we need. We can use some default parameters to get some essence of manipulating.In C++11, one new feature is introduced, called the bind function. This helps us to do such manipulating in some easier fashion. To use these features, we have to use header file.Bind functions with the help of placeholders helps to determine the positions, and number of arguments to modify the function according to desired outputs.Placeholders are namespaces which detect the position of a ... Read More
![Anvi Jain](https://www.tutorialspoint.com/assets/profiles/13591/profile/60_98631-1512716973.jpg)
368 Views
Here we will see the Shuffle and random_shuffle in C++. Let us see the random_shuffle first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example Live Demo#include using namespace std; int myRandomGenerator(int j) { return rand() % j; } main() { srand(unsigned(time(0))); vector arr; for (int ... Read More