C++ Primer笔记(3.2.3 )—— 处理string对象中的字符
#include<iostream>
#include<string>
#include<cctype>
/********************************************************************************************/
/* string的操作 */
/* */
/* isalnum(c) ------ 当c是字母或数字时为真 */
/* isalpha(c) ------ 当c是字母时为真 */
/* iscntrl(c) ------ 当c是控制字符时为真 */
/* isdigit(c) ------ 当c是数字时为真 */
/* isgraph(c) ------ 当c不是空格但可打印时为真 */
/* islower(c) ------ 当c是小写字母时为真 */
/* isprint(c) ------ 当c是可打印字符时为真(即c是空格或具有可视形式) */
/* ispunct(c) ------ 当c是标点符号时为真 */
/* isapace(c) ------ 当c是空白时为真(空格、制表符、回车、换行、进纸符的一种) */
/* isupper(c) ------ 当c是大写字母时为真 */
/* isxdigit(c) ------ 当c是十六进制数字时为真 */
/* tolower(c) ------ 如果c是大写字母,输出对应的小写字母,否则原样输出c */
/* toupper(c) ------ 如果c是小写字母,输出对应的大写字母,否则原样输出c */
/********************************************************************************************/
using std::string;
int main()
{
/* ----- 处理每个字符?使用基于范围的for语句 ----- */
//我们可以使用范围for语句把string对象中的字符每行一个输出:
string str("some string");
//每行输出str中的一个字符
for (auto c : str)
std::cout << c << std::endl;
std::cout << std::endl;
//使用for语句和ispunct函数来统计string对象中标点符号的个数:
string s("Hello World!!!");
//punct_cnt的类型和s.size的返回类型一样,2.5.3节有说明
decltype(s.size()) punct_cnt = 0;
//统计s中标点符号的数量
for (auto c : s)
if (ispunct(c))
++punct_cnt;
std::cout << punct_cnt << " punctuation characters in " << s << std::endl;
std::cout << std::endl;
/* ----------------------------------------------- */
/* ----- 使用范围for语句改变字符串中的字符 ----- */
//假设我们想要把字符串改写为大写字母的形式。我们可以使用标准库函数toupper
string str1("Hello World!!!");
//转换成大写形式
for (auto &c : str1) // 对于s中的每个字符(注意:c是引用)
c = toupper(c); // c是一个引用,因此赋值语句将改变s中字符的值
std::cout << "str1为:" << s << std::endl;
std::cout << std::endl;
/* --------------------------------------------- */
/* ----- 只处理一部分字符? ----- */
//要想访问string对象中的单个字符有两种方式:
//一种是使用下标,另一种是使用迭代器(3.4中有说明)
//下标运算符([])接收的输入参数是string::size_type类型的值(3.2.2节),这个参数表示要访问的字符位置
//string对象的下标从0计起。如果string对象s至少包含两个字符,则s[0]是第一个,s[1]是第二个,s[s.size()-1]是最后一个
string str2("Hello");
if (!str2.empty())
std::cout << str2[0] << std::endl;
std::cout << std::endl;
/* ------------------------------ */
/* ----- 使用下标执行迭代 ----- */
//依次处理str3中的字符直至我们处理完全部字符或者遇到一个空白
string str3("some string");
for (decltype(str3.size()) index = 0;
index != str3.size() && !isspace(str3[index]); ++index)
s[index] = toupper(str3[index]); //将当前字符改成大写形式
std::cout << "str3为:" << str3 << std::endl;
std::cout << std::endl;
/* ---------------------------- */
/* ----- 使用下标执行随机访问 ----- */
//在之前的示例中,我们让字符串的下标每次加1从而按顺序把所有哦字符改写成了大写形式。
//其实也能通过计算得到某个下标值,然后哦直接获取对应位置的字符,并不是每次都得从前往后依次访问。
//例如,想要编写一个程序把0到15之间的十进制数转换成对应的十六进制形式,只需要初始化一个字符串令其存放16个十六进制“数字”
const string hexdigits = "0123456789ABCDEF"; //可能的十六进制数字
std::cout << "Enter a series of numbers between 0 and 15"
<< " separated by spaces. Hit ENTER when finished: "
<< std::endl;
string result; //用于保存十六进制的字符串
string::size_type n; //用于保存从输入流读取的数
while (std::cin >> n)
if (n < hexdigits.size()) //忽略无效输入
result += hexdigits[n]; //得到对应的十六进制数字
std::cout << "Your hex number is: " << result << std::endl;
//假设输入的内容如下:
// 12 0 5 15 8 15
//程序输出结果为;
// Your hex number is: C05F8F
/* -------------------------------- */
return 0;
}