1. 字符

1.1 字符类型库:头文件"cctype"

在头文件<cctype>(相当于 C 语言中的<ctype.h>),包含了一下字符处理函数:

FUNCTIONEXAMPLE
int isalpha(int ch);//如果 ch 是字母,返回 1,否则 0
int isdigit(int ch);//如果 ch 是数字,返回 1,否则 0
int isalnum(int ch);//如果 ch 是字母或数字,返回 1,否则 0
int isxdigit(int ch);//如果 ch 是十六进制数字[0-9A-Fa-f],返回 1
int isupper(int ch);//如果 ch 是大写字母,返回 1
int islower(int ch);//如果 ch 是小写字母,返回 1
int toupper(int ch);//转成大写字母
int tolower(int ch);//转成小写
int isspace(int ch);
int ispunct(int ch);//是否是标点符号
int iscntrl(int ch);//是否是 control
int isprint(int ch);//是否是可打印字符
int isgraph(int ch);//是否图形表示

2. 字符串:C-String 和 string 类

回顾一下,我们知道 C++支持两种类型的字符串:

  1. 一种是 C 语言中的 string,定义在头文件’cstring’中(相当于 c 中的 string.h)。这种形式是将字符串表示成以\0结束的字符数组
  2. 另一种是 C++中的 string 类,被定义在头文件’string’中。‘string’是一个定义了一些公共构造函数和成员函数的普通类

2.1 C-String 字面量

一个字符串字面量,例如"hello",被认为是一个以\0结束的字符数组,该数组的长度是字符串字面量长度加上结束字符的长度。

 1#include <iostream>
 2
 3int main(int argc, char *argv[]) {
 4	char *str1 = "hello";
 5	// warning: deprecated conversion from string constant to 'char *';
 6	char *str2 = const_cast<char *>("hello"); // remove the "const"
 7
 8	const char *str3 = "hello";
 9	// *(str3 + 1) = 'a'; // error: assignment of read-only location '*(str3 + 1)'
10
11	char str4[] = "hello";
12	str4[1] = 'a';
13	const char str5[] = "hello";
14	//str5[1] = 'a' // error: assignment of read-only location 'str5[1]
15	return 0;
16}

2.2 C-String 头文件和函数

C-string 可以被声明为char *char []。这是因为 C 语言把一个数组的名字当做指向数组第一个元素的指针。不同于普通数组, 在将 C-string 传递给函数的时候,不需要给出数组长度,因为可以根据终结符计算出来。

定义在头文件中的 C-String 函数

FUNCTIONEXAMPLE
size_t strlen (const char * cstr);char * msg = "Hello"; cout << strlen(msg); // length of string
Copying
char *strcpy(char *dest, const char *src);
char *strncpy(char *desc, char *src, size_t n);
Comparison
int strcmp(const char *cstr1, const char *cstr2);
int strncmp(const char *cstr1, const char *cstr2, size_t n);
Concatenation
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
Searching
char *strchr(char *cstr, int ch);
char *strrchr(char *cstr, int ch);
char *strstr(char *cstr1, char *cstr2);
size_t strspn (const char * cstr, const char * accept);
char * strpbrk (char * cstr, const char * accept);
size_t strcspn (const char * cstr, const char * reject);
Tokenizing
char * strtok (char * cstr, const char * delim)

定义在头文件中的 C-String 函数

FUNCTIONEXAMPLE
int atoi (char * cstr)
double atof (char * cstr)
long atol (char * cstr)
long long atoll (char * cstr)
double strtod (const char * cstr, char** endptr)
float strtof (const char * cstr, char** endptr)
long strtol (const char * cstr, char** endptr)
long double strtold (const char * cstr, char** endptr)
long long strtoll (const char * cstr, char** endptr)
unsigned long long strtoull (const char * cstr, char** endptr)

定义在头文件中的 C-String Input/Output 函数

FUNCTIONEXAMPLE
cin >> varchar *msg; cin >> msg;
cin.getline(char * cstr, unsigned int n); cin.getline(char * cstr, unsigned int n, char delim)char msg[256]; cin.getline(msg, 256); cin.getline(msg, 256, '\n');
int cin.get() Return the next character, casted as an int.
cin.peek() return the next character (casted as an int), but not removing it from the input stream.
cin.ignore(unsigned int n = 1, int delim = EOF) Remove n-1 characters from the input stream, or until delimiter is reached.

C-string 的大小写转换

1inline void strtoupper(char *str) {
2	while (*str) {
3		*str = toupper(*str);
4		++str;
5	}
6}

2.3 C++中的 string 类

string 类定义在头文件’‘中,属于’std’命名空间。string 类是 basic_string模板类的一个 char 类型实例:

1typedef basic_string<char> string;
2typedef basic_string<wchar_t> wstring;

string 类的构造器

 1string ();
 2   // (1) Default constructor: construct an empty string of length 0.
 3string (const string & str);
 4   // (2) Copy constructor: construct by copying str (by value)
 5string (const string & str, size_t pos, size_t len = npos);
 6   // (3) Substring constructor: copy the substring starting at pos, of the len.
 7   // size_t is usually typedef to unsigned int
 8   // npos is a static constant in string (i.e., string::npos),
 9   //   which holds the maximum value of size_t.
10string (const char * cstr);
11   // (4) C-string: construct by copying the C-string.
12string (const char * cstr, size_t len);
13   // (5) C-string buffer: construct by copying the cstr for len
14string (size_t len, char c);
15   // (6) Fill Constructor: fill len with char c
16template <class Iterator>
17string (Iterator first, Iterator last);
18   // (7) Iterator: copy the char in [first, last)
19string (initializer_list<char> initList);
20   // (C++11)(8) Initializer list
21string (string && str) noexcept;
22   // (C++11)(9) Move Constructor

示例:

1string str1("apple");
2string str2 = "orange";
3// '=' is not an assignment, but an implicit call to string's
4// constructor str2("orange");

string 类中的公开函数

 1// Capacity
 2size_t size () const;    // Return the size of the string
 3size_t length () const;  // same as above
 4                         // length() was from earlier version, size() added for compatibility with STL
 5bool empty () const;     // Return true for empty string
 6void clear ();           // Clear to empty string
 7void resize (size_t n, char pad = '\0');  // resize the string to length n,
 8                                          // discard trailing characters, or insert pad char
 9
10string::npos             // static variable for the maximum possible characters in string,
11                         // typically max of size_t (unsigned int)
12size_t max_size () const;    // Return the maximum size of string object
13size_t capacity () const;    // Storage (in terms of characters) currently allocated
14void reserve (size_t n = 0); // Request for minimum of this capacity
15void shrink_to_fit ();