zoukankan      html  css  js  c++  java
  • c++常见输入方法[持续更新]

    字符串输入

    cin>> 使用空格确认字符串结束位置,保留换行符在输入队列当中

    单个字符读取

    cin.get(), cin.get(char)

    每次读取一行:

    cin.getline(char*,int) 丢弃换行符

    cin.get(char*,int)保留换行符在输入队列当中

    恢复输入:

    cin.clear()

    string类读写

    头文件:string.h

    getline(cin,<string>);

     

    字符串操作

    strcpy() 将字符串复制到字符串数组中

    strcat()将字符串附加到字符串数组末尾

    strlen()测字符串数组长度

    基本输入代码:

     1 //使用原始的cin进行输入
     2 void InputWord()
     3 {
     4     char ch;
     5     cin>>ch;
     6     while(ch!='#')
     7     {
     8         cout<<ch;
     9         cin>>ch;
    10     }
    11 }
    12 
    13 //使用cin.get(char)读取字符,包括空格、制表符和换行符
    14 void InputChar()
    15 {
    16     char ch;
    17     cin.get(ch);
    18     while(ch!='#')
    19     {
    20         cout<<ch;
    21         cin.get(ch);
    22     }
    23 }
    24 
    25 //使用文件结尾符
    26 void InputUntilEOF()
    27 {
    28     char ch;
    29     cin.get(ch);
    30     while(cin.fail()==false) /*while(!cin.fail()) 或者 while(cin) 或者 while(cin.get(ch))*/
    31     {
    32         cout<<ch;
    33         cin.get(ch);
    34     }
    35 }
  • 相关阅读:
    二逼平衡树(树套树)
    NOI2010 超级钢琴
    SDOI2011 消耗战
    HNOI2013 游走
    [SDOI2010]外星千足虫
    [UVA 11374]Airport Express
    [Luogu P1354]房间最短路问题
    [Luogu P2296][NOIP 2014]寻找道路
    高精度算法
    洛谷红名+AC150祭
  • 原文地址:https://www.cnblogs.com/lsr-flying/p/4726108.html
Copyright © 2011-2022 走看看