当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ basic_istream::operator>>用法及代码示例


basic_istream::operator>> 被称为提取运算符。此运算符用于应用于输入字符串。头文件:

<iostream>

用法:

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

Parameters: 

  • a:这表示存储提取字符的值。

返回值:istream::operator>> 返回 basic_istream 对象。下面是说明 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;
}
输出:

a = 12
b = 12.2
c = false

程序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;
}
输出:
a = 144
b = 0.26
c = false

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





注:本文由纯净天空筛选整理自bansal_rtk_大神的英文原创作品 basic_istream::operator>> in C++。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。