cin之于char[]和string

cin之于char[]和string

书上说,string可以看作char数组:
#include <iostream>

using namespace std;

int main()
{
    const int SIZE = 20;
    string str;
    char ch[SIZE];
   
    cin >> str;
    cin >> ch;
    cout << str << "," << ch << endl;

    system("PAUSE");
    return 0;
}
在使用 cin >> 时,无论是string还是char[],编译器都能通过编译。可是,当使用cin.getline()时:
#include <iostream>

using namespace std;

int main()
{
    const int SIZE = 20;
    string str;
    char ch[SIZE];
   
  
    cin.getline(ch,SIZE);
    cin.getline(str,SIZE);
   
   
    system("PAUSE");
    return 0;
}
编译器无法编译,出错信息:13(行号)  no matching function for call to `std::basic_istream<char, std::char_traits<char> >::getline(std::string&, const int&)'
不是说,string是char的数组吗?可是,为什么会出现没有匹配的函数???

再有:
#include <iostream>
#include <string>
using namespace std;

int main()
{
    const int SIZE = 20;
    string str;
    char ch[SIZE];
    char c;
  
    cin.get(c);
    cin.get(ch);
    cin.get(str);   
   
    system("PAUSE");
    return 0;
}
cin.get()只允许接受char类型的参数,不接受char[]、string类型的参数。所以,从cin.get(ch);开始,就无法编译了。

(本人使用的编译器:Dev-C++ 4.9.9.2)

你可能感兴趣的:(cin之于char[]和string)