Open In App
Related Articles

basic_istream::swap() in C++ with Examples

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report
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:
// C++ program to demonstrate
// basic_istream::swap()
  
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
    // Input String gfg1
    istringstream gfg1("Welcome");
  
    // Input String gfg2
    istringstream gfg2("Geeks");
  
    // swap function for swapping
    // both the strings
    swap(gfg1, gfg2);
    cout << gfg1.rdbuf() << " "
         << gfg2.rdbuf() << endl;
  
    return 0;
}

                    
Output:
Geeks Welcome
Program 2:
// C++ program to demonstrate
// basic_istream::swap()
  
#include <bits/stdc++.h>
using namespace std;
  
// Driver Code
int main()
{
    // Input String gfg1
    istringstream gfg1("forGeeks");
  
    // Input String gfg2
    istringstream gfg2("Geeks");
  
    // swap function for swapping
    // both the strings
    gfg1.swap(gfg2);
    cout << gfg1.rdbuf() << " "
         << gfg2.rdbuf() << endl;
  
    return 0;
}

                    
Output:
Geeks forGeeks
Reference: http://www.cplusplus.com/reference/istream/basic_istream/swap/

Last Updated : 28 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads