c++primer plus上的一个示例,我不明其中的两个地方,发上来,大家帮我讲讲
1.为什么要包含 string 和 cstring 两个头文件呢?
这里的string是不是就是指 string.c 这个文件呢?
2.
getline(cin,str);   //这个函数并没有用类名调用,所以不是类中的方法,那它是
//是什么呢?难道不是类的成员函数吗,在哪里定义的呢?
 这一句,getline前面没有句点,所以不是类的成员函数,那它又是什么呢?在何处定义的呢?#include <iostream>
#include <string> //为何要包含两个头文件呢?
#include <cstring>int main()
{
using namespace std;
char charr[20];
string str; cout << "length of string in charr before input: "
<< strlen(charr) << endl;
cout << "length of string in str before input: "
<< str.size() << endl; cout << "enter a line of text:\n";
cin.getline(charr,20);
cout << "you entered: " << charr << endl;
cout << "enter another line of text\n"; getline(cin,str);   //这个函数并没有用类名调用,所以不是类中的方法,那它是
//是什么呢?难道不是类的成员函数吗,在哪里定义的呢? cout << "you entered: " << str << endl; cout << "length of string in charr after input:\n"
<< strlen(charr) << endl;
cout << "length of string in str after input: "
<< str.size() << endl; return 0;
}

解决方案 »

  1.   

    getline是个函数在string头文件中定义
      

  2.   

    c++中的头文件(不包括C的)的引用时不需要写.h的,#include<string>这个里面声明是string类
    而#include<cstring>表示使用的是C库中string.h,所以前面用'c'区别,里面包含了getline等函数
      

  3.   

    说得很准。cstring库里面对应那些char 的东西
      

  4.   

    cin.getline() 这种调用方法我明白。
    但是没有对象名的调用,我不明白。
    getline(),是谁在调用这个函数呢?