The Wayback Machine - https://web.archive.org/web/20240920142611/https://www.geeksforgeeks.org/basic_istreamoperator-in-c/
Open In App

basic_istream::operator>> in C++

Last Updated : 22 Jul, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The basic_istream::operator>> is known as the extraction operator. This operator is used to apply on the input string. 
Header File: 
 

<iostream>

Syntax: 
 

basic_istream& operator>>( int& a );
basic_istream& operator>>( unsigned int& a );

Parameters: 
 

  • a : This represents the value where the extracted character are stored.

Return Value: The istream::operator>> returns the basic_istream object.
Below is the program to illustrate std::basic_istream::operator>>:
Program 1: 
 

CPP14




// C++ code for basic_istream::operator>>
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Declare the string "12 12.2 GeeksforGeeks"
    string gfg = "12 12.2 GeeksforGeeks";
 
    istringstream stream(gfg);
    int a;
    float b;
    bool c;
 
    stream >> a >> b >> boolalpha >> c;
    cout << "a = " << a << '\n'
         << "b = " << b << '\n'
         << "c = " << boolalpha << c << endl;
 
    return 0;
}


Output: 

a = 12
b = 12.2
c = false

 

Program 2: 
 

CPP14




// C++ code for basic_istream::operator>>
#include <bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    // Declare the string "144 0.26 GFG"
    string gfg = "144 0.26 GFG";
 
    istringstream stream(gfg);
    int a;
    float b;
    bool c;
 
    stream >> a >> b >> boolalpha >> c;
    cout << "a = " << a << '\n'
         << "b = " << b << '\n'
         << "c = " << boolalpha << c << endl;
 
    return 0;
}


Output: 

a = 144
b = 0.26
c = false

 

Reference: http://www.cplusplus.com/reference/istream/basic_istream/operator%3E%3E/
 



Similar Reads

std::basic_istream::gcount() in C++ with Examples
The std::basic_istream::gcount() is used to count the characters in the given string. It returns the number of characters extracted by the last unformatted input operation. The unformatted input operation is returned by these function: get(), getline(), ignore(), peek(), read(), etc. Header File: &lt;iostream&gt; Syntax: streamsize gcount() const P
1 min read
std::basic_istream::getline in C++ with Examples
The std::basic_istream::getline is used to extract the characters from stream until end of line or the extracted character is the delimiting character. The delimiting character is the new line character i.e '\n'. This function will also stop extracting characters if the end-of-file is reached if input is taken using file.Header File: #include &lt;i
2 min read
std::basic_istream::ignore in C++ with Examples
The std::basic_istream::ignore is used to extracts characters from the input string and discards them including delimiting character, i.e., if the end of the file is reached this function stops extracting characters. The delimiting character is the new line character i.e '\n'. This function will also stop extracting characters if the end-of-file is
2 min read
basic_istream::unget() in C++ with Examples
The basic_istream::unget() is used to unget the character and used to decrease the location by one character and makes the extracted character available for used once again. Header File: &lt;iostream&gt; Syntax: basic_istream&amp; unget(); Parameters: The basic_istream::unget() method doesn’t accepts any parameter.Return Value: The function basic_i
2 min read
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&lt;iostream&gt; Syntax: void swap (basic_istream&amp; x);
2 min read
basic_istream::get() in C++ with Examples
The basic_istream::get() is used to get the character. This function returns a one character if available, otherwise it will return end of the file. Header File: &lt;iostream&gt; Syntax: int_type get(); Parameters: The method basic_istream::get() doesn’t accept any parameter. Return Value: The method basic_istream::get() returns a character if avai
2 min read
basic_istream::seekg() in C++ with Examples
The basic_stream::seekg() method is used to set position of the next character to be extracted from the input stream. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include&lt;iostream&gt; Syntax: basic_istream&amp; seekg (pos_type pos); Parameter: pos: It represents the new position in the buf
1 min read
basic_istream::putback() in C++ with Examples
The basic_istream::putback() used to put the character back in the input string. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include&lt;iostream&gt; Syntax: basic_istream& putback (char_type ch); Parameter: ch: It represents the character to be put into the input string back. Return Value: T
2 min read
basic_istream::readsome() in C++ with Examples
The basic_istream::readsome() used to read the data from the buffer and extracts up to n immediately available characters from the input string. This function returns the number of extracted characters. Below is the syntax for the same: Header File: #include&lt;iostream&gt; Syntax: streamsize readsome(char_type* a, streamsize n); Parameters: n: It
2 min read
basic_istream::peek() in C++ with Examples
The std::basic_istream::peek() used to reads the next character from the input stream without extracting it. This function does not accept any parameter, simply returns the next character in the input string. Below is the syntax for the same: Header File: #include&lt;iostream&gt; Syntax: int peek(); Return Value: The std::basic_istream::peek() retu
2 min read
deque::operator= and deque::operator[] in C++ STL
Deque or Double ended queues are sequence containers with the feature of expansion and contraction on both the ends. They are similar to vectors, but are more efficient in case of insertion and deletion of elements at the end, and also the beginning. Unlike vectors, contiguous storage allocation may not be guaranteed. deque::operator= This operator
4 min read
Operator Overloading '&lt;&lt;' and '&gt;&gt;' operator in a linked list class
Prerequisite: Operator Overloading in C++, Linked List in C++ C++ comes with libraries that provide ways for performing Input and Output. In C++, Input and Output are performed as a sequence of bytes, also known as streams. Input and Output stream are managed by the iostream library. cin and cout are the standard objects for the input stream and ou
4 min read
Why overriding both the global new operator and the class-specific operator is not ambiguous?
The below section deals about overload resolution as it helps in the fundamentals of overloading and overriding. Predict the output: C/C++ Code #include &lt;iostream&gt; using namespace std; class Gfg { public: void printHello() { cout &lt;&lt; &quot;hello gfg-class specific&quot; &lt;&lt; endl; } }; void printHello() { cout &lt;&lt; &quot;hello gf
5 min read
vector::operator= and vector::operator[ ] in C++ STL
Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. vector::operator=This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new
4 min read
Operands for sizeof operator
The sizeof operator is used to return the size of its operand, in bytes. This operator always precedes its operand. The operand either may be a data-type or an expression. Let's look at both the operands through proper examples. type-name: The type-name must be specified in parentheses. sizeof(type - name) Let's look at the code: C/C++ Code #includ
2 min read
array::operator[ ] in C++ STL
Array classes are generally more efficient, light-weight, and reliable than C-style arrays. The introduction of array class from C++11 has offered a better alternative for C-style arrays. array::operator[] This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only diffe
2 min read
list::operator= in C++ STL
Lists are containers used in C++ to store data in a non contiguous fashion, Normally, Arrays and Vectors are contiguous in nature, therefore the insertion and deletion operations are costlier as compared to the insertion and deletion option in Lists. list::operator= This operator is used to assign new contents to the container by replacing the exis
2 min read
forward_list::operator= in C++ STL
Forward list in STL implements singly linked list. Introduced from C++11, forward lists are useful than other containers for insertion, removal and moving operations (like sort) and allows time constant insertion and removal of elements.It differs from the list by the fact that forward list keeps track of the location of only next element while lis
2 min read
map::operator[] in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::operator[] This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at(
2 min read
multiset::operator= in C++ STL
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values. multiset::operator= This operator is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents.Syntax : multisetname1 = (multisetname2) Parameter
3 min read
multimap::operator= in C++ STL
multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as parameter to the container written on left side of
2 min read
unordered_map operator= in C++ STL
The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_map to another unordered_map and unordered_map::operator= is the corresponding operator function. There are three versions of this function. The first version takes reference of an unordered_map as an argument and copies it to an unordered_map. The second version performs a move
2 min read
unordered_multiset operator = in C++ STL
The ‘=’ is an operator in C++ STL which copies (or moves) an unordered_multiset to another unordered_multiset and unordered_multiset::operator= is the corresponding operator function. There are three versions of this function: The first version takes reference of an unordered_multiset as an argument and copies it to an unordered_multiset. Syntax:um
2 min read
alignof operator in C++
In C++11 the alignof operator used to returns the alignment, in bytes of the specified type. Syntax: alignof(type) Syntax Explanation: alignof: operator returns the alignment in byte, required for instances of type, which type is either complete type, array type or a reference type. array type: alignment requirement of the element type is returned.
2 min read
bitset operator[] in C++ STL
bitset::operator[] is a built-in function in C++ STL which is used to assign value to any index of a bitset. Syntax: bitset_operator[index] = value Parameter: The parameter index specifies the position at which the value is to be assigned. Return Value: The function returns the value to the bit at the index. Below programs illustrate the bitset::op
2 min read
multimap operator = in C++ STL
The multimap::operator= is a built in C++ STL which assigns new contents to the container, replacing its current content. Syntax: multimap1_name = multimap2_name Parameters: The multimap on the left is the container in which the multimap on the right is to be assigned by destroying the elements of multimap1. Return Value: This function does not ret
2 min read
map operator= in C++ STL
The map::operator= is a built function in C++ STL which assigns contents of a container to a different container, replacing its current content. Syntax: map1_name = map2_name Parameters: The map on the left is the container in which the map on the right is to be assigned by destroying the elements of map1. Return Value: This function does not retur
2 min read
unordered_multimap operator= in C++
The unordered_multimap::operator= is a built-in function in C++ STL which does three types of tasks which are explained below. Syntax (copying elements from different container) : unordered_multimap_name1 operator= (unordered_multimap_name2)Parameters: The function does not accepts any parameter. The container to the right is the one from which the
4 min read
match_results operator[] in C++ STL
The match_results::operator[] is a built function in C++ which used to get the i-th match in the match_result object. It gives the reference to the match at the position given inside the operator.Syntax: smatch_name[N] Note: smatch_name is an object of match_results class. Parameters: It accepts a single parameter N which specifies the Match number
2 min read
match_results operator= in C++
The match_results::operator= is used to replace all the matches in a smatch object with new matches from another smatch object.Syntax: smatch_name1 = (smatch_name2) Note: smatch_name is an object of match_results class. Parameters: The smatch object on the right side gets copied to the one at the left side. Return Value: It does not return anything
2 min read
Article Tags :
Practice Tags :