get()和getline()

#include <iostream>
#include <string>

int main()
{
    const int SIZE = 10;

    //getine()函数会舍弃换行符'\n'
    //字符串的长度可以自动调整
    std::string str1;
    getline(std::cin, str1);
    std::cout << str1 <<std::endl;
    //因为数组最后一个字节存储空字符,所以实际只能获取SIZE-1个字符
    char str2[SIZE];
    std::cin.getline(str2, SIZE-1);
    std::cout << str2 <<std::endl;

    //get()函数会保留换行符'\n'
    char str3[SIZE];
    std::cin.get(str3, SIZE);
    std::cout << str3 <<std::endl;

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40579095/article/details/82014382