How to use stringstream with insertion (<<) and extraction (>>) operators in C++

1 Answer

0 votes
#include <iostream>  
#include <sstream> 

int main() {  
    std::stringstream ss;
    
    ss << "c++"; 
    std::cout << "ss: " << ss.str() << "\n";
    
    ss.str("programming"); 
    std::cout << "ss: " << ss.str() << "\n";

    std::stringstream ss1;

    ss << "java php";
    
    std::string s1;
    ss >> s1;
    
    std::string s2;
    ss >> s2;
    
    std::cout << "s1: " << s1 << "\n";
    std::cout << "s2: " << s2 << "\n";
    std::cout << "ss: " << ss.str() << "\n";
    
    std::stringstream().swap(ss);
    ss << "abc";
    ss >> s1;
    std::cout << "ss: " << ss.str() << "\n";
    std::cout << "s1: " << s1 << "\n";

    return 0;
}  




/*
run:

ss: c++
ss: programming
s1: java
s2: phping
ss: java phping
ss: abc
s1: abc

*/

 



answered Jan 14, 2021 by avibootz

Related questions

1 answer 92 views
92 views asked Jan 13, 2023 by avibootz
1 answer 162 views
162 views asked Jan 24, 2021 by avibootz
2 answers 342 views
1 answer 124 views
124 views asked Feb 23, 2024 by avibootz
1 answer 97 views
...