basic_istream::swap() in C++ with Examples
The basic_ios::swap(x) is used swap all data members of the base class except for rdbuf(), and swaps the values of the gcount() counters between *this and x. This basic_ios::swap(x) function is a protected function. Below is the syntax and the header file for the same:
Header File:
#include<iostream>
Syntax:
void swap (basic_istream& x);
Parameter: It accepts the following parameter:
- x : It represents the another object with the same parameter.
Return Value: The method basic_istream::get() doesn’t return anything.
Below are the programs to demonstrate basic_istream::swap():
Program 1:
#include <bits/stdc++.h>
using namespace std;
int main()
{
istringstream gfg1( "Welcome" );
istringstream gfg2( "Geeks" );
swap(gfg1, gfg2);
cout << gfg1.rdbuf() << " "
<< gfg2.rdbuf() << endl;
return 0;
}
|
Program 2:
#include <bits/stdc++.h>
using namespace std;
int main()
{
istringstream gfg1( "forGeeks" );
istringstream gfg2( "Geeks" );
gfg1.swap(gfg2);
cout << gfg1.rdbuf() << " "
<< gfg2.rdbuf() << endl;
return 0;
}
|
Reference: http://www.cplusplus.com/reference/istream/basic_istream/swap/
Last Updated :
28 May, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...