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;
}

  

相关文章:

  • 2021-12-09
  • 2021-12-13
  • 2021-11-21
  • 2021-09-03
  • 2022-02-10
  • 2022-02-25
  • 2021-11-29
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-18
相关资源
相似解决方案