
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ istream::operator>>() function
The C++ std::istream::operator>>() function is an extraction operator used to read the formatted data from a stringstream object into variables. It works similar to the >> operator with std::cin, but instead, it extracts data from a string buffer.
This function has 3 polymorphic variants: with using the arithmetic type or stream buffers or manipulators (you can find the syntaxes of all the variants below).
Syntax
Following is the syntax for std::istream::operator>>() function.
istream& operator>> (bool& val); istream& operator>> (short& val); istream& operator>> (unsigned short& val); istream& operator>> (int& val); istream& operator>> (unsigned int& val); istream& operator>> (long& val); istream& operator>> (unsigned long& val); istream& operator>> (long long& val); istream& operator>> (unsigned long long& val); istream& operator>> (float& val); istream& operator>> (double& val); istream& operator>> (long double& val); istream& operator>> (void*& val); or istream& operator>> (streambuf* sb ); or istream& operator>> (istream& (*pf)(istream&)); istream& operator>> (ios& (*pf)(ios&)); istream& operator>> (ios_base& (*pf)(ios_base&));
Parameters
- val − It indicates the object where the value that the extracted characters represent is stored.
- sb − It indicates the pointer to a basic_streambuf object on whose controlled output sequence the characters are copied.
- pf − It indicates a function that takes and returns a stream object.
Return Value
This function returns the basic_istream object (*this).
Exceptions
If an exception is thrown, the object is in a valid state.
Data races
Modifies val or the object pointed by sb.
Example
Let's look at the following example, where we are going to read the single integer.
#include <iostream> #include <sstream> #include <string> int main() { std::string a = "11"; std::stringstream b(a); int x; b >> x; std::cout << "Result : " << x << std::endl; return 0; }
Output
Output of the above code is as follows −
Result : 11
Example
Consider the following example, where we are going to read multiple data types.
#include <iostream> #include <sstream> #include <string> int main() { std::string a = "11 2.3 TutorialsPoint"; std::stringstream b(a); int x; float y; std::string z; b >> x >> y >> z; std::cout << "x : " << x << ", y : " << y << ", z : " << z << std::endl; return 0; }
Output
Following is the output of the above code −
x : 11, y : 2.3, z : TutorialsPoint
Example
In the following example, we are going to read the list if integers.
#include <iostream> #include <sstream> #include <string> #include <vector> int main() { std::string x = "1 2 3 4 5"; std::stringstream y(x); std::vector<int> a; int b; while (y >> b) { a.push_back(b); } std::cout << "Result :"; for (int n : a) { std::cout << " " << n; } std::cout << std::endl; return 0; }
Output
If we run the above code it will generate the following output −
Result : 1 2 3 4 5