c++中peek函數的用法


peek()用於接下來讀入的那個字符是什么,沒有輸入也沒有丟棄,你可以正常使用cin。

返回值是字符。

使用方法為cin.peek()。

#include <iostream>
#include<ctype.h>
#include<vector>
#include<string>
using namespace std;
int main()
{
    vector<int> a;
    vector<string> b;
    for (int c = 0; c < 3; c++)
    {   
        char temp;
        temp=cin.peek();//獲取下一次輸入的首字符
        if (isdigit(temp))//判斷該字符是否為整數字符
        {
            int temp;
            cin >> temp;
            a.push_back(temp);
        }
        else
        { 
            string temp;
            cin >> temp;
            b.push_back(temp);     
        }
        getchar();//cin>>輸入時沒有丟棄掉空格或換行,要丟棄掉他們。
    }
    return 0;
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM