Zodiac Wang
  • Home
  • Categories
  • Tags
  • Archives

cin-cinget-cingetline的区别

Table of Contents

  • 1  std::istream::get
  • 2  std::istream::getline
  • 3  std::getline (string)
  • 4  cin >> 操作符
  • 5  get 和 getline 的区别

普通输入处理 主要有以下几种方式

  • std::istream::get istream 的 get 方法
  • std::istream::getline istream 的 getline 方法
  • std::getline (string) string 类的 getline 方法
  • cin >> cin 配合流操作符

std::istream::get¶

原型

//single character (1)
int get(); //只返回 int 也就是 char
istream& get (char& c);

//c-string (2)
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);

//stream buffer (3)
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);
  • http://www.cplusplus.com/reference/istream/istream/get/

第一句原型返回读取到的 int 即 char 或者 EOF,其他函数返回 *this 也就是一个 istream& 所以可以串联读取

例

int func()
{
    char ch = cin.get(); //对应第一句原型
    char ch_1, ch_2;
    cin.get(ch_1).get(ch_2); //对应第二句原型 可以连续读取是因为返回类型为 istream&
    char s[100];
    cin.get(s, 11); //对应第三句原型
    char s_1[100];
    cin.get(s_1, 11, ' '); //对应第四句原型

    cout << ch << endl;
    cout << ch_1 << " " << ch_2 << endl;
    cout << s << endl;
    cout << s_1 << endl;
    return 0;
}

运行结果

Please, enter a sentence: think different think different
t
h i
nk differe
nt

实际上,将 cin 换成其他 istream 流也是可以的 比如 ifstream

std::istream::getline¶

原型

istream& getline (char* s, streamsize n );
istream& getline (char* s, streamsize n, char delim );

函数返回 *this 即 istream&

  • http://www.cplusplus.com/reference/istream/istream/getline/
int func_1()
{
    char name[256], title[256];

    cout << "Please, enter your name: ";
    cin.getline(name, 256, ' ');

    cout << "Please, enter your favourite movie: ";
    cin.getline(title, 256);

    cout << name << "'s favourite movie is " << title;

    return 0;
}

运行结果

Please, enter your name: wang tao
Please, enter your favourite movie: wang's favourite movie is tao

std::getline (string)¶

原型

  • http://www.cplusplus.com/reference/string/string/getline/
// (1) 到指定 delim 处结束读取
istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
// (2) 到 '\n' 处结束读取
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

函数返回 *this 即 istream&

int func_2()
{
    string name;

    cout << "Please, enter your full name: ";
    getline(cin, name);
    cout << "Hello, " << name << "!\n";

    return 0;
}

运行结果

Please, enter your full name: wang tao
Hello, wang tao!

cin >> 操作符¶

根据后面变量的类型读取数据。 输入结束条件:遇到Enter、Space、Tab键. 对结束符的处理 :丢弃缓冲区中使得输入结束的结束符(Enter、Space、Tab)。

如果不想跳过空格 可以通过 noskipws 流控制符来控制

cin >> noskipws >> str;

get 和 getline 的区别¶

get 不会读取输入流中的 delim 字符,即 delim 字符还留在输入流中,需要手动读取并丢弃。

getline 会读取并丢弃 delim 即输入流中不再有 delim 字符,且读取字符串中也没有 delim 字符。

即不管是 get 还是 getline 读取到的内容里都是不包含 delim 字符的,区别在于输入流中是否还保留 delim 字符

int func_3()
{
    cout << "Please, enter your full name: ";
    char name[256];
    cin.get(name, 256, ' ');
    cout << "Hello, " << name << "!\n" << (char)cin.get() << endl;

    cout << "Please, enter your full name: ";
    string namestr;
    getline(cin, namestr, ' ');
    cout << "Hello, " << name << "!\n" << (char)cin.get() << endl;
    return 0;
}
Please, enter your full name: wang tao
Hello, wang!

Please, enter your full name: wang tao
Hello, wang!
t

cin.get 和 cin.getline 都只能将字符串读取到 char* 即char 数组中,而且为了安全必须指定 streamsize。

string 类中的 getline 就可以将 istream 流中的输入读取到 string 中。

因为 cin.get 大部分重载返回 istream 引用,所以手动丢弃 delim 时可以这么写

cin.get(ch, 10).get();

除此以外,C 中还有一些输入函数,比如 getchar scanf 等就不讨论了。

full code

# include <iostream>
# include <string>

using namespace std;


int func()
{
    cout << "Please, enter a sentence: ";
    char ch = cin.get(); //对应第一句原型
    char ch_1, ch_2;
    cin.get(ch_1).get(ch_2); //对应第二句原型 可以连续读取时应为返回类型为 istream&
    char s[100];
    cin.get(s, 11); //对应第三句原型
    char s_1[100];
    cin.get(s_1, 11, ' '); //对应第四句原型

    cout << ch << endl;
    cout << ch_1 << " " << ch_2 << endl;
    cout << s << endl;
    cout << s_1 << endl;
    return 0;
}

int func_1()
{
    char name[256], title[256];

    cout << "Please, enter your name: ";
    cin.getline(name, 256, ' ');

    cout << "Please, enter your favourite movie: ";
    cin.getline(title, 256);

    cout << name << "'s favourite movie is " << title;

    return 0;
}

int func_2()
{
    string name;

    cout << "Please, enter your full name: ";
    getline(cin, name);
    cout << "Hello, " << name << "!\n";

    return 0;
}

int func_3()
{
    cout << "Please, enter your full name: ";
    char name[256];
    cin.get(name, 256, ' ');
    cout << "Hello, " << name << "!\n" << (char)cin.get() << endl;

    cout << "Please, enter your full name: ";
    string namestr;
    getline(cin, namestr, ' ');
    cout << "Hello, " << name << "!\n" << (char)cin.get() << endl;
    return 0;
}

int main()
{
    func();
    func_1();
    func_2();
    func_3();
    system("pause");
    return 0;
}

  • « C++中几种类型之间的相互转换
  • 两个内存泄漏的例子 »

Published

5 18, 2019

Category

posts

Tags

  • cpp 8

Contact

  • Zodiac Wang - A Fantastic Learner
  • Powered by Pelican. Theme: Elegant