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结束的字符数组,该数组的长度是字符串字面量长度加上结束字符的长度。

#include <iostream>

int main(int argc, char *argv[]) {
	char *str1 = "hello";
	// warning: deprecated conversion from string constant to 'char *';
	char *str2 = const_cast<char *>("hello"); // remove the "const"

	const char *str3 = "hello";
	// *(str3 + 1) = 'a'; // error: assignment of read-only location '*(str3 + 1)'

	char str4[] = "hello";
	str4[1] = 'a';
	const char str5[] = "hello";
	//str5[1] = 'a' // error: assignment of read-only location 'str5[1]
	return 0;
}

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 的大小写转换

inline void strtoupper(char *str) {
	while (*str) {
		*str = toupper(*str);
		++str;
	}
}

2.3 C++中的 string 类

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

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

string 类的构造器

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

示例:

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

string 类中的公开函数

// Capacity
size_t size () const;    // Return the size of the string
size_t length () const;  // same as above
                         // length() was from earlier version, size() added for compatibility with STL
bool empty () const;     // Return true for empty string
void clear ();           // Clear to empty string
void resize (size_t n, char pad = '\0');  // resize the string to length n,
                                          // discard trailing characters, or insert pad char

string::npos             // static variable for the maximum possible characters in string,
                         // typically max of size_t (unsigned int)
size_t max_size () const;    // Return the maximum size of string object
size_t capacity () const;    // Storage (in terms of characters) currently allocated
void reserve (size_t n = 0); // Request for minimum of this capacity
void shrink_to_fit ();