C++中string类的IO操作详解
正如您知道的,可以使用 cin 和运算符<<来将输入存储到 string 对象中,使用 cout 和运算符<<来显示string 对象,其句法与处理 C-风格字符串相同。但每次读取一行而不是一个单词时,使用的句法不同,程序清单4.10说明了这一点。
// strtype4.cpp -- line input
#include <iostream>
#include <string> // make string class available
#include <cstring> // C-style string library
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); // indicate maximum length
cout << "You entered: " << charr << endl;
cout << "Enter another line of text:\n";
getline(cin, str); // cin now an argument; no length specifier
cout << "You entered: " << str << endl;
cout << "Length of string in charr after input: "
<< strlen(charr) << endl;
cout << "Length of string in str after input: "
<< str.size() << endl;
// cin.get();
return 0;
}
下面是一个运行该程序时的输出示例:
Length of string in charr before input:27
Length of string in str before input:0
Enter a line of text:
peanut butter
You entered: peanut butter
Enter another line of text
blueberry jam
You entered: blueberry jam
Length of string in charr after input: 13
Length of string in str after input:13
在用户输入之前,该程序指出数组charr 中的字符串长度为 27,这比该数组的长度要大。这里要两点需要说明。首先,为初始化的数组的内容是未定义的;其次,函数strlen()从数组的第一个元素开始计算字节数,直到遇到空字符。在这个例子中,在数组末尾的几个字节后才遇到空字符。对于未被初始化的数据,第一个空字符的出现位置是随机的,因此您在运行该程序时,得到的数组长度很可能与此不同。另外,用户输入之前,str 中的字符串长度为0。这是因为未被初始化的 string 对象的长度被自动设置为 0。
下面是将一行输入读取到数组中的代码:
cin.getline(charr,20);这种句点表示法表明,函数 getline()是 istream类的一个类方法(还记得吗,cin是一个istream 对象)正如前面指出的,第一个参数是目标数组;第二个参数数组长度,getine()使用它来避免超越数组的边界。下面是将一行输入读取到 string 对象中的代码:
getline(cin,str);
这里没有使用句点表示法,这表明这个getline()不是类方法。它将cin作为参数,指出到哪里去查找输入。另外,也没有指出字符串长度的参数,因为string对象将根据字符串的长度自动调整自己的大小。那么,为何一个 getline()是istream 的类方法,而另一个不是呢?在引入 string 类之前很久,C++就有istream 类。因此 istream 的设计考虑到了诸如 double 和 int 等基本 C++数据类型,但没有考虑 string 类型,所以 istream 类中,有处理 double、int 和其他基本类型的类方法,但没有处理 string 对象的类方法。由于 istream 类中没有处理 string 对象的类方法,因此您可能会问,下述代码为何可行呢?cin >>str;//read a word into the str string object
对C++感兴趣的朋友点这里:C/C++课程