string类型的定义和初始化有几种形式?分别是什么?
答:4种。
string s1; 默认构造函数 s1 为空串
string s2(s1); 将 s2 初始化为 s1 的一个副本
string s3("value"); 将 s3 初始化为一个字符串字面值副本
string s4(n, 'c'); 将 s4 初始化为字符 'c' 的 n 个副本
getline函数的用途是什么?
答:输出一行字符串。
格式:
cin>>str与getline(cin,str)的区别是什么?
答:string类型的输入操作符在处理空白字符时,读取并忽略有效字符之前的所有空白字符,直到遇到空白字符时结束(空白字符还留在输入流中)。
getline函数的输入操作符在处理空白字符时,不忽略有效字符之前的所有空白字符,直到遇到换行符结束,并丢弃换行符。
string对象中字符的处理?
答:使用cctype函数,改函数在头文件cctype中。
isalnum(c) 如果 c 是字母或数字,则为 True。
isalpha(c) 如果 c 是字母,则为 true。
iscntrl(c) 如果 c 是控制字符,则为 true
isdigit(c) 如果 c 是数字,则为 true。
isgraph(c) 如果 c 不是空格,但可打印,则为 true。
islower(c) 如果 c 是小写字母,则为 true。
isprint(c) 如果 c 是可打印的字符,则为 true。
ispunct(c) 如果 c 是标点符号,则 true。
isspace(c) 如果 c 是空白字符,则为 true。
isupper(c) 如果 c 是大写字母,则 true。
isxdigit(c) 如果是 c 十六进制数,则为 true。
tolower(c) 如果 c 大写字母,返回其小写字母形式,否则直接返回 c。
toupper(c) 如果 c 是小写字母,则返回其大写字母形式,否则直接返回 c。
什么是默认构造函数?
答:在没有显示初始化式时调用的构造函数。它由不带形参的构造函数,或者为所有形参提供实参的构造函数。