c++ basic_strings

通用字符串类
该basic_string的是String类的任何字符类型(见字符串描述)的泛化。

模板参数
图表
字符类型。
该字符串是通过这种类型的字符序列形成的。
这应是一个非数组POD类型。
性状
性格特征类定义了basic_string的对象(见char_traits)用字的基本属性。
特点:: char_type应是一样的图表。
别名为成员类型的basic_string::的traits_type。
ALLOC
分配器对象类型用来定义存储分配模式。默认情况下,分配器类模板时,它定义了简单的内存分配模式,是价值无关。
别名为成员类型的basic_string:: allocator_type。
注:因为第一个模板参数是不是别名为任何成员类型,图用于整个引用来引用此类型。
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/1fd8acee-24cb-4e63-946b-de46f868624e.htm
http://www.cplusplus.com/reference/string/basic_string/?kw=basic_string


#include <string>
#include <iostream>



//构造函数
void basic_stringConstructor(void);

//增加了字符的字符串的末尾
void basic_string_append(void);

//分配新的字符值到字符串的内容
void basic_string_assign(void);

//在返回字符串中指定位置的参考元素
void basic_string_at(void);

//返回一个迭代解决的第一个元素在字符串中
void basic_string_begin(void);

//字符串作为C风格的内容转换,空值终止,串
void basic_string_c_str(void);

//返回可存储在一个字符串元素的最大数量不增加串的存储器分配
void basic_string_capacity(void);

//删除字符串中的所有元素
void basic_string_clear(void);

//进行比较以指定字符串的字符串,以确定该两个字符串相等或者如果一个字典顺序小于另一个
void basic_string_compare(void);

//副本在从源字符串到目标字符数组索引位置最指定数目的字符
void basic_string_copy(void);

//副本在从源字符串到目标字符数组索引位置最指定数目的字符
void basic_string__Copy_s(void);

//字符串的内容转换为字符数组
void basic_string_data(void);

//测试该字符串是否是包含的字符或没有
void basic_string_empty(void);

//返回地址字符串中的成功的最后一个元素的位置的迭代器
void basic_string_end(void);

//删除从指定位置的元素或一个范围中的字符串的元素的
void basic_string_erase(void);

//搜索在前进方向的字符串为一个字符串的第一个出现的字符的指定序列相匹配
void basic_string_find(void);

//通过搜索的第一个字符的字符串不是指定字符串的任何元素
void basic_string_find_first_not_of(void);

//通过搜索的第一个字符的字符串指定字符串的任何元素相匹配
void basic_string_find_first_of(void);

//通过搜索的最后一个字符的字符串不是指定字符串的任何元素
void basic_string_find_last_not_of(void);

//通过搜索的最后一个字符字符串,是一个指定字符串的元素
void basic_string_find_last_of(void);

//返回分配器对象的副本,用于构造串
void basic_string_get_allocator(void);

//在插入指定位置的元素或多个元素或者一个范围元素融入串
void basic_string_insert(void);

//返回字符串中的元素的当前数目
void basic_string_length(void);

//返回一个字符串应该包含字符的最大数量
void basic_string_max_size(void);

//将一个元素添加到字符串的末尾
void basic_string_push_back(void);

//返回一个迭代器的第一个元素的字符串逆转
void basic_string_rbegin(void);

//返回一个指向刚刚超越了逆转字符串中的最后一个元素的迭代
void basic_string_rend(void);

//在一个字符串替换元素的指定位置与来自其它范围或字符串或C字符串复制指定的字符或字符
void basic_string_replace(void);

//设置字符串与数的能力至少一样大指定数目
void basic_string_reserve(void);

//指定一个新的大小为一个字符串,添加或删除元素的要求
void basic_string_resize(void);

//搜索在向后方向的字符串的子字符串的第一次出现的字符的指定序列相匹配
void basic_string_rfind(void);

//返回字符串中的元素的当前数目
void basic_string_size(void);

//副本最多某些字符数的一个字符串的子字符串从指定的位置开始
void basic_string_substr(void);

//交换两个字符串的内容
void basic_string_swap(void);

int main()
{
	//basic_stringConstructor();
	//basic_string_append();
	//basic_string_assign();
	//basic_string_at();
	//basic_string_begin();
	//basic_string_c_str();
	//basic_string_capacity();
	//basic_string_clear();
	//basic_string_compare();
	//basic_string_copy();
	//basic_string__Copy_s();
	//basic_string_data();
	//basic_string_empty();
	//basic_string_end();
	//basic_string_erase();
	//basic_string_find();
	//basic_string_find_first_not_of();
	//basic_string_find_first_of();
	//basic_string_find_last_not_of();
	//basic_string_find_last_of();
	//basic_string_get_allocator();
	//basic_string_insert();
	//basic_string_length();
	//basic_string_max_size();
	//basic_string_push_back();
	//basic_string_rbegin();
	//basic_string_rend();
	//basic_string_replace();
	//basic_string_reserve();
	//basic_string_resize();
	//basic_string_rfind();
	//basic_string_size();
	//basic_string_substr();
	basic_string_swap();
	return 0;
}

//构造函数
void basic_stringConstructor(void)
{
	using namespace std;

	// The first member function initializing with a C-string
	const char *cstr1a = "Hello Out There.";
	basic_string <char> str1a(cstr1a, 5);
	cout << "The string initialized by C-string cstr1a is: "
		<< str1a << "." << endl;

	// The second member function initializing with a string
	string  str2a("How Do You Do?");
	basic_string <char> str2b(str2a, 7, 7);
	cout << "The string initialized by part of the string cstr2a is: "
		<< str2b << "." << endl;

	// The third member function initializing a string
	// with a number of characters of a specific value
	basic_string <char> str3a(5, '9');
	cout << "The string initialized by five number 9s is: "
		<< str3a << endl;

	// The fourth member function creates an empty string
	// and string with a specified allocator
	basic_string <char> str4a;
	string str4b;
	basic_string <char> str4c(str4b.get_allocator());
	if (str4c.empty())
		cout << "The string str4c is empty." << endl;
	else
		cout << "The string str4c is not empty." << endl;

	// The fifth member function initializes a string from
	// another range of characters
	string str5a("Hello World");
	basic_string <char> str5b(str5a.begin() + 5, str5a.end());
	cout << "The string initialized by another range is: "
		<< str5b << "." << endl;

	return;
	/*
	The string initialized by C-string cstr1a is: Hello.
	The string initialized by part of the string cstr2a is: You Do?.
	The string initialized by five number 9s is: 99999
	The string str4c is empty.
	The string initialized by another range is:  World.
	请按任意键继续. . .
	*/
}

//增加了字符的字符串的末尾
void basic_string_append(void)
{
	using namespace std;

	// The first member function
	// appending a C-string to a string
	string str1a("Hello ");
	cout << "The original string str1 is: " << str1a << endl;
	const char *cstr1a = "Out There ";
	cout << "The C-string cstr1a is: " << cstr1a << endl;
	str1a.append(cstr1a);
	cout << "Appending the C-string cstr1a to string str1 gives: "
		<< str1a << "." << endl << endl;

	// The second member function
	// appending part of a C-string to a string
	string str1b("Hello ");
	cout << "The string str1b is: " << str1b << endl;
	const char *cstr1b = "Out There ";
	cout << "The C-string cstr1b is: " << cstr1b << endl;
	str1b.append(cstr1b, 3);
	cout << "Appending the 1st part of the C-string cstr1b "
		<< "to string str1 gives: " << str1b << "."
		<< endl << endl;

	// The third member function
	// appending part of one string to another
	string str1c("Hello "), str2c("Wide World ");
	cout << "The string str2c is: " << str2c << endl;
	str1c.append(str2c, 5, 5);
	cout << "The appended string str1 is: "
		<< str1c << "." << endl << endl;

	// The fourth member function
	// appending one string to another in two ways,
	// comparing append and operator [ ]
	string str1d("Hello "), str2d("Wide "), str3d("World ");
	cout << "The  string str2d is: " << str2d << endl;
	str1d.append(str2d);
	cout << "The appended string str1d is: "
		<< str1d << "." << endl;
	str1d += str3d;
	cout << "The doubly appended strig str1 is: "
		<< str1d << "." << endl << endl;

	// The fifth member function
	// appending characters to a string
	string str1e("Hello ");
	str1e.append(4, '!');
	cout << "The string str1 appended with exclamations is: "
		<< str1e << endl << endl;

	// The sixth member function
	// appending a range of one string to another
	string str1f("Hello "), str2f("Wide World ");
	cout << "The string str2f is: " << str2f << endl;
	str1f.append(str2f.begin() + 5, str2f.end() - 1);
	cout << "The appended string str1 is: "
		<< str1f << "." << endl << endl;

	return;
	/*
	The original string str1 is: Hello
	The C-string cstr1a is: Out There
	Appending the C-string cstr1a to string str1 gives: Hello Out There .

	The string str1b is: Hello
	The C-string cstr1b is: Out There
	Appending the 1st part of the C-string cstr1b to string str1 gives: Hello Out.

	The string str2c is: Wide World
	The appended string str1 is: Hello World.

	The  string str2d is: Wide
	The appended string str1d is: Hello Wide .
	The doubly appended strig str1 is: Hello Wide World .

	The string str1 appended with exclamations is: Hello !!!!

	The string str2f is: Wide World
	The appended string str1 is: Hello World.

	请按任意键继续. . .
	*/
}

//分配新的字符值到字符串的内容
void basic_string_assign(void)
{
	using namespace std;

	// The first member function assigning the
	// characters of a C-string to a string
	string str1a;
	const char *cstr1a = "Out There";
	cout << "The C-string cstr1a is: " << cstr1a << "." << endl;
	str1a.assign(cstr1a);
	cout << "Assigning the C-string cstr1a to string str1 gives: "
		<< str1a << "." << endl << endl;

	// The second member function assigning a specific
	// number of the of characters a C-string to a string
	string  str1b;
	const char *cstr1b = "Out There";
	cout << "The C-string cstr1b is: " << cstr1b << endl;
	str1b.assign(cstr1b, 3);
	cout << "Assigning the 1st part of the C-string cstr1b "
		<< "to string str1 gives: " << str1b << "."
		<< endl << endl;

	// The third member function assigning a specific number
	// of the characters from one string to another string 
	string str1c("Hello "), str2c("Wide World ");
	cout << "The string str2c is: " << str2c << endl;
	str1c.assign(str2c, 5, 5);
	cout << "The newly assigned string str1 is: "
		<< str1c << "." << endl << endl;

	// The fourth member function assigning the characters
	// from one string to another string in two equivalent
	// ways, comparing the assign and operator =
	string str1d("Hello"), str2d("Wide"), str3d("World");
	cout << "The original string str1 is: " << str1d << "." << endl;
	cout << "The string str2d is: " << str2d << endl;
	str1d.assign(str2d);
	cout << "The string str1 newly assigned with string str2d is: "
		<< str1d << "." << endl;
	cout << "The string str3d is: " << str3d << "." << endl;
	str1d = str3d;
	cout << "The string str1 reassigned with string str3d is: "
		<< str1d << "." << endl << endl;

	// The fifth member function assigning a specific 
	// number of characters of a certain value to a string
	string str1e("Hello ");
	str1e.assign(4, '!');
	cout << "The string str1 assigned with eclamations is: "
		<< str1e << endl << endl;

	// The sixth member function assigning the value from
	// the range of one string to another string
	string str1f("Hello "), str2f("Wide World ");
	cout << "The string str2f is: " << str2f << endl;
	str1f.assign(str2f.begin() + 5, str2f.end() - 1);
	cout << "The string str1 assigned a range of string str2f is: "
		<< str1f << "." << endl << endl;

	return;
	/*
	The C-string cstr1a is: Out There.
	Assigning the C-string cstr1a to string str1 gives: Out There.

	The C-string cstr1b is: Out There
	Assigning the 1st part of the C-string cstr1b to string str1 gives: Out.

	The string str2c is: Wide World
	The newly assigned string str1 is: World.

	The original string str1 is: Hello.
	The string str2d is: Wide
	The string str1 newly assigned with string str2d is: Wide.
	The string str3d is: World.
	The string str1 reassigned with string str3d is: World.

	The string str1 assigned with eclamations is: !!!!

	The string str2f is: Wide World
	The string str1 assigned a range of string str2f is: World.

	请按任意键继续. . .

	*/
}

//在返回字符串中指定位置的参考元素
void basic_string_at(void)
{
	using namespace std;
	string str1("Hello world"), str2("Goodbye world");
	const string  cstr1("Hello there"), cstr2("Goodbye now");
	cout << "The original string str1 is: " << str1 << endl;
	cout << "The original string str2 is: " << str2 << endl;

	// Element access to the non const strings
	basic_string <char>::reference refStr1 = str1[6];
	basic_string <char>::reference refStr2 = str2.at(3);

	cout << "The character with an index of 6 in string str1 is: "
		<< refStr1 << "." << endl;
	cout << "The character with an index of 3 in string str2 is: "
		<< refStr2 << "." << endl;

	// Element access to the const strings
	basic_string <char>::const_reference crefStr1 = cstr1[cstr1.length()];
	basic_string <char>::const_reference crefStr2 = cstr2.at(8);

	if (crefStr1 == '\0')
		cout << "The null character is returned as a valid reference."
		<< endl;
	else
		cout << "The null character is not returned." << endl;
	cout << "The character with index 8 in the const string cstr2 is: "
		<< crefStr2 << "." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The original string str2 is: Goodbye world
	The character with an index of 6 in string str1 is: w.
	The character with an index of 3 in string str2 is: d.
	The null character is returned as a valid reference.
	The character with index 8 in the const string cstr2 is: n.
	请按任意键继续. . .
	*/
}

//返回一个迭代解决的第一个元素在字符串中
void basic_string_begin(void)
{
	using namespace std;
	string str1("No way out."), str2;
	basic_string <char>::iterator strp_Iter, str1_Iter, str2_Iter;
	basic_string <char>::const_iterator str1_cIter;

	str1_Iter = str1.begin();
	cout << "The first character of the string str1 is: "
		<< *str1_Iter << endl;
	cout << "The full original string str1 is: " << str1 << endl;

	// The dereferenced iterator can be used to modify a character
	*str1_Iter = 'G';
	cout << "The first character of the modified str1 is now: "
		<< *str1_Iter << endl;
	cout << "The full modified string str1 is now: " << str1 << endl;

	// The following line would be an error because iterator is const
	// *str1_cIter = 'g';

	// For an empty string, begin is equivalent to end
	if (str2.begin() == str2.end())
		cout << "The string str2 is empty." << endl;
	else
		cout << "The string str2 is not empty." << endl;

	return;
	/*
	The first character of the string str1 is: N
	The full original string str1 is: No way out.
	The first character of the modified str1 is now: G
	The full modified string str1 is now: Go way out.
	The string str2 is empty.
	请按任意键继续. . .
	*/
}

//字符串作为C风格的内容转换,空值终止,串
void basic_string_c_str(void)
{
	using namespace std;

	string  str1("Hello world");
	cout << "The original string object str1 is: "
		<< str1 << endl;
	cout << "The length of the string object str1 = "
		<< str1.length() << endl << endl;

	// Converting a string to an array of characters
	const char *ptr1 = 0;
	ptr1 = str1.data();
	cout << "The modified string object ptr1 is: " << ptr1
		<< endl;
	cout << "The length of character array str1 = "
		<< strlen(ptr1) << endl << endl;

	// Converting a string to a C-style string
	const char *c_str1 = str1.c_str();
	cout << "The C-style string c_str1 is: " << c_str1
		<< endl;
	cout << "The length of C-style string str1 = "
		<< strlen(c_str1) << endl << endl;

	return;
	/*
	The original string object str1 is: Hello world
	The length of the string object str1 = 11

	The modified string object ptr1 is: Hello world
	The length of character array str1 = 11

	The C-style string c_str1 is: Hello world
	The length of C-style string str1 = 11

	请按任意键继续. . .
	*/
}

//返回可存储在一个字符串元素的最大数量不增加串的存储器分配
void basic_string_capacity(void)
{
	using namespace std;
	string  str1("Hello world");
	cout << "The original string str1 is: " << str1 << endl;

	// The size and length member functions differ in name only
	basic_string <char>::size_type sizeStr1, lenStr1;
	sizeStr1 = str1.size();
	lenStr1 = str1.length();

	basic_string <char>::size_type capStr1, max_sizeStr1;
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	cout << "The current size of original string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of original string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of original string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of original string str1 is: "
		<< max_sizeStr1 << "." << endl << endl;

	str1.erase(6, 5);
	cout << "The modified string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	lenStr1 = str1.length();
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	// after erasing part of the original string
	cout << "The current size of modified string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of modified string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of modified string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of modified string str1 is: "
		<< max_sizeStr1 << "." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The current size of original string str1 is: 11.
	The current length of original string str1 is: 11.
	The capacity of original string str1 is: 15.
	The max_size of original string str1 is: 4294967294.

	The modified string str1 is: Hello
	The current size of modified string str1 is: 6.
	The current length of modified string str1 is: 6.
	The capacity of modified string str1 is: 15.
	The max_size of modified string str1 is: 4294967294.
	请按任意键继续. . .
	*/
}

//删除字符串中的所有元素
void basic_string_clear(void)
{
	using namespace std;
	string  str1("Hello world"), str2;
	basic_string <char>::iterator str_Iter;
	cout << "The original string str1 is: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	str1.clear();
	cout << "The modified string str1 is: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	//For an empty string, begin is equivalent to end
	if (str1.begin() == str1.end())
		cout << "Nothing printed above because "
		<< "the string str1 is empty." << endl;
	else
		cout << "The string str1 is not empty." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The modified string str1 is:
	Nothing printed above because the string str1 is empty.
	请按任意键继续. . .
	*/
}

//进行比较以指定字符串的字符串,以确定该两个字符串相等或者如果一个字典顺序小于另一个
void basic_string_compare(void)
{
	using namespace std;

	// The first member function compares
	// an operand string to a parameter string
	int comp1;
	string s1o("CAB");
	string s1p("CAB");
	cout << "The operand string is: " << s1o << endl;
	cout << "The parameter string is: " << s1p << endl;
	comp1 = s1o.compare(s1p);
	if (comp1 < 0)
		cout << "The operand string is less than "
		<< "the parameter string." << endl;
	else if (comp1 == 0)
		cout << "The operand string is equal to "
		<< "the parameter string." << endl;
	else
		cout << "The operand string is greater than "
		<< "the parameter string." << endl;
	cout << endl;

	// The second member function compares part of
	// an operand string to a parameter string
	int comp2a, comp2b;
	string s2o("AACAB");
	string s2p("CAB");
	cout << "The operand string is: " << s2o << endl;
	cout << "The parameter string is: " << s2p << endl;
	comp2a = s2o.compare(2, 3, s2p);
	if (comp2a < 0)
		cout << "The last three characters of "
		<< "the operand string\n are less than "
		<< "the parameter string." << endl;
	else if (comp2a == 0)
		cout << "The last three characters of "
		<< "the operand string\n are equal to "
		<< "the parameter string." << endl;
	else
		cout << "The last three characters of "
		<< "the operand string\n is greater than "
		<< "the parameter string." << endl;

	comp2b = s2o.compare(0, 3, s2p);
	if (comp2b < 0)
		cout << "The first three characters of "
		<< "the operand string\n are less than "
		<< "the parameter string." << endl;
	else if (comp2b == 0)
		cout << "The first three characters of "
		<< "the operand string\n are equal to "
		<< "the parameter string." << endl;
	else
		cout << "The first three characters of "
		<< "the operand string\n is greater than "
		<< "the parameter string." << endl;
	cout << endl;

	// The third member function compares part of
	// an operand string to part of a parameter string
	int comp3a;
	string s3o("AACAB");
	string s3p("DCABD");
	cout << "The operand string is: " << s3o << endl;
	cout << "The parameter string is: " << s3p << endl;
	comp3a = s3o.compare(2, 3, s3p, 1, 3);
	if (comp3a < 0)
		cout << "The three characters from position 2 of "
		<< "the operand string are less than\n "
		<< "the 3 characters parameter string "
		<< "from position 1." << endl;
	else if (comp3a == 0)
		cout << "The three characters from position 2 of "
		<< "the operand string are equal to\n "
		<< "the 3 characters parameter string "
		<< "from position 1." << endl;
	else
		cout << "The three characters from position 2 of "
		<< "the operand string is greater than\n "
		<< "the 3 characters parameter string "
		<< "from position 1." << endl;
	cout << endl;

	// The fourth member function compares
	// an operand string to a parameter C-string
	int comp4a;
	string s4o("ABC");
	const char* cs4p = "DEF";
	cout << "The operand string is: " << s4o << endl;
	cout << "The parameter C-string is: " << cs4p << endl;
	comp4a = s4o.compare(cs4p);
	if (comp4a < 0)
		cout << "The operand string is less than "
		<< "the parameter C-string." << endl;
	else if (comp4a == 0)
		cout << "The operand string is equal to "
		<< "the parameter C-string." << endl;
	else
		cout << "The operand string is greater than "
		<< "the parameter C-string." << endl;
	cout << endl;

	// The fifth member function compares part of
	// an operand string to a parameter C-string
	int comp5a;
	string s5o("AACAB");
	const char* cs5p = "CAB";
	cout << "The operand string is: " << s5o << endl;
	cout << "The parameter string is: " << cs5p << endl;
	comp5a = s5o.compare(2, 3, s2p);
	if (comp5a < 0)
		cout << "The last three characters of "
		<< "the operand string\n are less than "
		<< "the parameter C-string." << endl;
	else if (comp5a == 0)
		cout << "The last three characters of "
		<< "the operand string\n are equal to "
		<< "the parameter C-string." << endl;
	else
		cout << "The last three characters of "
		<< "the operand string\n is greater than "
		<< "the parameter C-string." << endl;
	cout << endl;

	// The sixth member function compares part of
	// an operand string to part of an equal length of
	// a parameter C-string
	int comp6a;
	string s6o("AACAB");
	const char* cs6p = "ACAB";
	cout << "The operand string is: " << s6o << endl;
	cout << "The parameter C-string is: " << cs6p << endl;
	comp6a = s6o.compare(1, 3, cs6p, 3);
	if (comp6a < 0)
		cout << "The 3 characters from position 1 of "
		<< "the operand string are less than\n "
		<< "the first 3 characters of the parameter C-string."
		<< endl;
	else if (comp6a == 0)
		cout << "The 3 characters from position 2 of "
		<< "the operand string are equal to\n "
		<< "the first 3 characters of the parameter C-string."
		<< endl;
	else
		cout << "The 3 characters from position 2 of "
		<< "the operand string is greater than\n "
		<< "the first 3 characters of the parameter C-string."
		<< endl;
	cout << endl;

	return;
	/*
	The operand string is: CAB
	The parameter string is: CAB
	The operand string is equal to the parameter string.

	The operand string is: AACAB
	The parameter string is: CAB
	The last three characters of the operand string
	are equal to the parameter string.
	The first three characters of the operand string
	are less than the parameter string.

	The operand string is: AACAB
	The parameter string is: DCABD
	The three characters from position 2 of the operand string are equal to
	the 3 characters parameter string from position 1.

	The operand string is: ABC
	The parameter C-string is: DEF
	The operand string is less than the parameter C-string.

	The operand string is: AACAB
	The parameter string is: CAB
	The last three characters of the operand string
	are equal to the parameter C-string.

	The operand string is: AACAB
	The parameter C-string is: ACAB
	The 3 characters from position 2 of the operand string are equal to
	the first 3 characters of the parameter C-string.

	请按任意键继续. . .
	*/
}

//副本在从源字符串到目标字符数组索引位置最指定数目的字符
void basic_string_copy(void)
{
	using namespace std;
	string str1("Hello World");
	basic_string <char>::iterator str_Iter;
	char array1[20] = { 0 };
	char array2[10] = { 0 };
	basic_string <char>::pointer array1Ptr = array1;
	basic_string <char>::value_type *array2Ptr = array2;

	cout << "The original string str1 is: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	//basic_string <char>::size_type nArray1;
	// Note: string::copy is potentially unsafe, consider
	// using string::_Copy_s instead.
	/*
	nArray1 = str1.copy(array1Ptr, 12);  // C4996
	cout << "The number of copied characters in array1 is: "
		<< nArray1 << endl;
	cout << "The copied characters array1 is: " << array1 << endl;
	*/

	//basic_string <char>::size_type nArray2;
	// Note: string::copy is potentially unsafe, consider
	// using string::_Copy_s instead.
	/*
	nArray2 = str1.copy(array2Ptr, 5, 6);  // C4996
	cout << "The number of copied characters in array2 is: "
		<< nArray2 << endl;
	cout << "The copied characters array2 is: " << array2Ptr << endl;
	*/

	return;
	/*
	编译报错  来自MSDN
	The original string str1 is: Hello World
	The number of copied characters in array1 is: 11
	The copied characters array1 is: Hello World
	The number of copied characters in array2 is: 5
	The copied characters array2 is: World

	*/
}

//副本在从源字符串到目标字符数组索引位置最指定数目的字符
void basic_string__Copy_s(void)
{
	using namespace std;
	string str1("Hello World");
	basic_string<char>::iterator str_Iter;
	const int array1_size = 20;
	char array1[array1_size] = { 0 };
	const int array2_size = 10;
	char array2[array2_size] = { 0 };
	basic_string<char>::pointer array1Ptr = array1;
	basic_string<char>::value_type *array2Ptr = array2;

	cout << "The original string str1 is: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	basic_string<char>::size_type nArray1;
	nArray1 = str1._Copy_s(array1Ptr, array1_size, 12);
	cout << "The number of copied characters in array1 is: "
		<< nArray1 << endl;
	cout << "The copied characters array1 is: " << array1 << endl;

	basic_string<char>::size_type nArray2;
	nArray2 = str1._Copy_s(array2Ptr, array2_size, 5, 6);
	cout << "The number of copied characters in array2 is: "
		<< nArray2 << endl;
	cout << "The copied characters array2 is: " << array2Ptr << endl;

	return;
	/*
	The original string str1 is: Hello World
	The number of copied characters in array1 is: 11
	The copied characters array1 is: Hello World
	The number of copied characters in array2 is: 5
	The copied characters array2 is: World
	请按任意键继续. . .
	*/
}

//字符串的内容转换为字符数组
void basic_string_data(void)
{
	using namespace std;

	string str1("Hello world");
	cout << "The original string object str1 is: "
		<< str1 << endl;
	cout << "The length of the string object str1 = "
		<< str1.length() << endl << endl;

	// Converting a string to an array of characters
	const char *ptr1 = 0;
	ptr1 = str1.data();
	cout << "The modified string object ptr1 is: " << ptr1
		<< endl;
	cout << "The length of character array str1 = "
		<< strlen(ptr1) << endl << endl;

	// Converting a string to a C-style string
	const char *c_str1 = str1.c_str();
	cout << "The C-style string c_str1 is: " << c_str1
		<< endl;
	cout << "The length of C-style string str1 = "
		<< strlen(c_str1) << endl << endl;

	return;
	/*
	The original string object str1 is: Hello world
	The length of the string object str1 = 11

	The modified string object ptr1 is: Hello world
	The length of character array str1 = 11

	The C-style string c_str1 is: Hello world
	The length of C-style string str1 = 11

	请按任意键继续. . .
	*/
}

//测试该字符串是否是包含的字符或没有
void basic_string_empty(void)
{
	using namespace std;

	bool b1, b2;

	string str1("Hello world");
	cout << "The original string object str1 is: " << str1 << endl;
	b1 = str1.empty();
	if (b1)
		cout << "The string object str1 is empty." << endl;
	else
		cout << "The string object str1 is not empty." << endl;
	cout << endl;

	// An example of an empty string object
	string str2;
	b2 = str2.empty();
	if (b2)
		cout << "The string object str2 is empty." << endl;
	else
		cout << "The string object str2 is not empty." << endl;

	return;
	/*
	The original string object str1 is: Hello world
	The string object str1 is not empty.

	The string object str2 is empty.
	请按任意键继续. . .
	*/
}

//返回地址字符串中的成功的最后一个元素的位置的迭代器
void basic_string_end(void)
{
	using namespace std;
	string str1("No way out."), str2;
	basic_string <char>::iterator str_Iter, str1_Iter, str2_Iter;
	basic_string <char>::const_iterator str1_cIter;

	str1_Iter = str1.end();
	str1_Iter--;
	str1_Iter--;
	cout << "The last character-letter of the string str1 is: " << *str1_Iter << endl;
	cout << "The full orginal string str1 is: " << str1 << endl;

	// end used to test when an iterator has reached the end of its string
	cout << "The string is now: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	// The dereferenced iterator can be used to modify a character
	*str1_Iter = 'T';
	cout << "The last character-letter of the modified str1 is now: "
		<< *str1_Iter << endl;
	cout << "The modified string str1 is now: " << str1 << endl;

	// The following line would be an error because iterator is const
	// *str1_cIter = 'T';

	// For an empty string, end is equivalent to begin
	if (str2.begin() == str2.end())
		cout << "The string str2 is empty." << endl;
	else
		cout << "The stringstr2  is not empty." << endl;

	return;
	/*
	The last character-letter of the string str1 is: t
	The full orginal string str1 is: No way out.
	The string is now: No way out.
	The last character-letter of the modified str1 is now: T
	The modified string str1 is now: No way ouT.
	The string str2 is empty.
	请按任意键继续. . .
	*/
}

//删除从指定位置的元素或一个范围中的字符串的元素的
void basic_string_erase(void)
{
	using namespace std;

	// The 1st member function using a range demarcated
	// by iterators
	string str1("Hello world");
	basic_string <char>::iterator str1_Iter;
	cout << "The original string object str1 is: "
		<< str1 << "." << endl;
	str1_Iter = str1.erase(str1.begin() + 3, str1.end() - 1);
	cout << "The first element after those removed is: "
		<< *str1_Iter << "." << endl;
	cout << "The modified string object str1 is: " << str1
		<< "." << endl << endl;

	// The 2nd member function erasing a char pointed to 
	// by an iterator
	string str2("Hello World");
	basic_string <char>::iterator str2_Iter;
	cout << "The original string object str2 is: " << str2
		<< "." << endl;
	str2_Iter = str2.erase(str2.begin() + 5);
	cout << "The first element after those removed is: "
		<< *str2_Iter << "." << endl;
	cout << "The modified string object str2 is: " << str2
		<< "." << endl << endl;

	// The 3rd member function erasing a number of chars 
	// after a char
	string str3("Hello computer"), str3m;
	basic_string <char>::iterator str3_Iter;
	cout << "The original string object str3 is: "
		<< str3 << "." << endl;
	str3m = str3.erase(6, 8);
	cout << "The modified string object str3m is: "
		<< str3m << "." << endl;

	return;
	/*
	The original string object str1 is: Hello world.
	The first element after those removed is: d.
	The modified string object str1 is: Held.

	The original string object str2 is: Hello World.
	The first element after those removed is: W.
	The modified string object str2 is: HelloWorld.

	The original string object str3 is: Hello computer.
	The modified string object str3m is: Hello .
	请按任意键继续. . .
	*/
}

//搜索在前进方向的字符串为一个字符串的第一个出现的字符的指定序列相匹配
void basic_string_find(void)
{
	using namespace std;

	// The first member function
	// searches for a single character in a string
	string str1("Hello Everyone");
	cout << "The original string str1 is: " << str1 << endl;
	basic_string <char>::size_type indexCh1a, indexCh1b;

	indexCh1a = str1.find("e", 3);
	if (indexCh1a != string::npos)
		cout << "The index of the 1st 'e' found after the 3rd"
		<< " position in str1 is: " << indexCh1a << endl;
	else
		cout << "The character 'e' was not found in str1 ." << endl;

	indexCh1b = str1.find("x");
	if (indexCh1b != string::npos)
		cout << "The index of the 'x' found in str1 is: "
		<< indexCh1b << endl << endl;
	else
		cout << "The Character 'x' was not found in str1."
		<< endl << endl;

	// The second member function searches a string
	// for a substring as specified by a C-string
	string str2("Let me make this perfectly clear.");
	cout << "The original string str2 is: " << str2 << endl;
	basic_string <char>::size_type indexCh2a, indexCh2b;

	const char *cstr2 = "perfect";
	indexCh2a = str2.find(cstr2, 5);
	if (indexCh2a != string::npos)
		cout << "The index of the 1st element of 'perfect' "
		<< "after\n the 5th position in str2 is: "
		<< indexCh2a << endl;
	else
		cout << "The substring 'perfect' was not found in str2 ."
		<< endl;

	const char *cstr2b = "imperfectly";
	indexCh2b = str2.find(cstr2b, 0);
	if (indexCh2b != string::npos)
		cout << "The index of the 1st element of 'imperfect' "
		<< "after\n the 5th position in str3 is: "
		<< indexCh2b << endl;
	else
		cout << "The substring 'imperfect' was not found in str2 ."
		<< endl << endl;

	// The third member function searches a string
	// for a substring as specified by a C-string
	string str3("This is a sample string for this program");
	cout << "The original string str3 is: " << str3 << endl;
	basic_string <char>::size_type indexCh3a, indexCh3b;

	const char *cstr3a = "sample";
	indexCh3a = str3.find(cstr3a);
	if (indexCh3a != string::npos)
		cout << "The index of the 1st element of sample "
		<< "in str3 is: " << indexCh3a << endl;
	else
		cout << "The substring 'perfect' was not found in str3 ."
		<< endl;

	const char *cstr3b = "for";
	indexCh3b = str3.find(cstr3b, indexCh3a + 1, 2);
	if (indexCh3b != string::npos)
		cout << "The index of the next occurrence of 'for' is in "
		<< "str3 begins at: " << indexCh3b << endl << endl;
	else
		cout << "There is no next occurrence of 'for' in str3 ."
		<< endl << endl;

	// The fourth member function searches a string
	// for a substring as specified by a string
	string str4("clearly this perfectly unclear.");
	cout << "The original string str4 is: " << str4 << endl;
	basic_string <char>::size_type indexCh4a, indexCh4b;

	string str4a("clear");
	indexCh4a = str4.find(str4a, 5);
	if (indexCh4a != string::npos)
		cout << "The index of the 1st element of 'clear' "
		<< "after\n the 5th position in str4 is: "
		<< indexCh4a << endl;
	else
		cout << "The substring 'clear' was not found in str4 ."
		<< endl;

	string str4b("clear");
	indexCh4b = str4.find(str4b);
	if (indexCh4b != string::npos)
		cout << "The index of the 1st element of 'clear' "
		<< "in str4 is: "
		<< indexCh4b << endl;
	else
		cout << "The substring 'clear' was not found in str4 ."
		<< endl << endl;

	return;
	/*
	The original string str1 is: Hello Everyone
	The index of the 1st 'e' found after the 3rd position in str1 is: 8
	The Character 'x' was not found in str1.

	The original string str2 is: Let me make this perfectly clear.
	The index of the 1st element of 'perfect' after
	the 5th position in str2 is: 17
	The substring 'imperfect' was not found in str2 .

	The original string str3 is: This is a sample string for this program
	The index of the 1st element of sample in str3 is: 10
	The index of the next occurrence of 'for' is in str3 begins at: 24

	The original string str4 is: clearly this perfectly unclear.
	The index of the 1st element of 'clear' after
	the 5th position in str4 is: 25
	The index of the 1st element of 'clear' in str4 is: 0
	请按任意键继续. . .
	*/
}

//通过搜索的第一个字符的字符串不是指定字符串的任何元素
void basic_string_find_first_not_of(void)
{
	using namespace std;

	// The first member function
	// searches for a single character in a string
	string str1("xddd-1234-abcd");
	cout << "The original string str1 is: " << str1 << endl;
	basic_string <char>::size_type indexCh1a, indexCh1b;
	static const basic_string <char>::size_type npos = -1;

	indexCh1a = str1.find_first_not_of("d", 2);
	if (indexCh1a != npos)
		cout << "The index of the 1st 'd' found after the 3rd"
		<< " position in str1 is: " << indexCh1a << endl;
	else
		cout << "The character 'd' was not found in str1 ." << endl;

	indexCh1b = str1.find_first_not_of("x");
	if (indexCh1b != npos)
		cout << "The index of the 'non x' found in str1 is: "
		<< indexCh1b << endl << endl;
	else
		cout << "The character 'non x' was not found in str1."
		<< endl << endl;

	// The second member function searches a string
	// for a substring as specified by a C-string
	string str2("BBB-1111");
	cout << "The original string str2 is: " << str2 << endl;
	basic_string <char>::size_type indexCh2a, indexCh2b;

	const char *cstr2 = "B1";
	indexCh2a = str2.find_first_not_of(cstr2, 6);
	if (indexCh2a != npos)
		cout << "The index of the 1st occurrence of an "
		<< "element of 'B1' in str2 after\n the 6th "
		<< "position is: " << indexCh2a << endl;
	else
		cout << "Elements of the substring 'B1' were not"
		<< "\n found in str2 after the 6th position."
		<< endl;

	const char *cstr2b = "B2";
	indexCh2b = str2.find_first_not_of(cstr2b);
	if (indexCh2b != npos)
		cout << "The index of the 1st element of 'B2' "
		<< "after\n the 0th position in str2 is: "
		<< indexCh2b << endl << endl;
	else
		cout << "The substring 'B2' was not found in str2 ."
		<< endl << endl << endl;

	// The third member function searches a string
	// for a substring as specified by a C-string
	string str3("444-555-GGG");
	cout << "The original string str3 is: " << str3 << endl;
	basic_string <char>::size_type indexCh3a, indexCh3b;

	const char *cstr3a = "45G";
	indexCh3a = str3.find_first_not_of(cstr3a);
	if (indexCh3a != npos)
		cout << "The index of the 1st occurrence of an "
		<< "element in str3\n other than one of the "
		<< "characters in '45G' is: " << indexCh3a
		<< endl;
	else
		cout << "Elements in str3 contain only characters "
		<< " in the string '45G'. "
		<< endl;

	const char *cstr3b = "45G";
	indexCh3b = str3.find_first_not_of(cstr3b, indexCh3a + 1, 2);
	if (indexCh3b != npos)
		cout << "The index of the second occurrence of an "
		<< "element of '45G' in str3\n after the 0th "
		<< "position is: " << indexCh3b << endl << endl;
	else
		cout << "Elements in str3 contain only characters "
		<< " in the string  '45G'. "
		<< endl << endl;

	// The fourth member function searches a string
	// for a substring as specified by a string
	string str4("12-ab-12-ab");
	cout << "The original string str4 is: " << str4 << endl;
	basic_string <char>::size_type indexCh4a, indexCh4b;

	string str4a("ba3");
	indexCh4a = str4.find_first_not_of(str4a, 5);
	if (indexCh4a != npos)
		cout << "The index of the 1st non occurrence of an "
		<< "element of 'ba3' in str4 after\n the 5th "
		<< "position is: " << indexCh4a << endl;
	else
		cout << "Elements other than those in the substring"
		<< " 'ba3' were not found in the string str4."
		<< endl;

	string str4b("12");
	indexCh4b = str4.find_first_not_of(str4b);
	if (indexCh4b != npos)
		cout << "The index of the 1st non occurrence of an "
		<< "element of '12' in str4 after\n the 0th "
		<< "position is: " << indexCh4b << endl;
	else
		cout << "Elements other than those in the substring"
		<< " '12' were not found in the string str4."
		<< endl;

	return;
	/*
	The original string str1 is: xddd-1234-abcd
	The index of the 1st 'd' found after the 3rd position in str1 is: 4
	The index of the 'non x' found in str1 is: 1

	The original string str2 is: BBB-1111
	Elements of the substring 'B1' were not
	found in str2 after the 6th position.
	The index of the 1st element of 'B2' after
	the 0th position in str2 is: 3

	The original string str3 is: 444-555-GGG
	The index of the 1st occurrence of an element in str3
	other than one of the characters in '45G' is: 3
	The index of the second occurrence of an element of '45G' in str3
	after the 0th position is: 7

	The original string str4 is: 12-ab-12-ab
	The index of the 1st non occurrence of an element of 'ba3' in str4 after
	the 5th position is: 5
	The index of the 1st non occurrence of an element of '12' in str4 after
	the 0th position is: 2
	请按任意键继续. . .
	*/
}

//通过搜索的第一个字符的字符串指定字符串的任何元素相匹配
void basic_string_find_first_of(void)
{
	using namespace std;

	// The first member function
	// searches for a single character in a string
	string str1("abcd-1234-abcd-1234");
	cout << "The original string str1 is: " << str1 << endl;
	basic_string <char>::size_type indexCh1a, indexCh1b;
	static const basic_string <char>::size_type npos = -1;

	indexCh1a = str1.find_first_of("d", 5);
	if (indexCh1a != npos)
		cout << "The index of the 1st 'd' found after the 5rd"
		<< " position in str1 is: " << indexCh1a << endl;
	else
		cout << "The character 'd' was not found in str1 ." << endl;

	indexCh1b = str1.find_first_of("x");
	if (indexCh1b != npos)
		cout << "The index of the 'x' found in str1 is: "
		<< indexCh1b << endl << endl;
	else
		cout << "The character 'x' was not found in str1."
		<< endl << endl;

	// The second member function searches a string
	// for a substring as specified by a C-string
	string str2("ABCD-1234-ABCD-1234");
	cout << "The original string str2 is: " << str2 << endl;
	basic_string <char>::size_type indexCh2a, indexCh2b;

	const char *cstr2 = "B1";
	indexCh2a = str2.find_first_of(cstr2, 6);
	if (indexCh2a != npos)
		cout << "The index of the 1st occurrence of an "
		<< "element of 'B1' in str2 after\n the 6th "
		<< "position is: " << indexCh2a << endl;
	else
		cout << "Elements of the substring 'B1' were not "
		<< "found in str2 after the 10th position."
		<< endl;

	const char *cstr2b = "D2";
	indexCh2b = str2.find_first_of(cstr2b);
	if (indexCh2b != npos)
		cout << "The index of the 1st element of 'D2' "
		<< "after\n the 0th position in str2 is: "
		<< indexCh2b << endl << endl;
	else
		cout << "The substring 'D2' was not found in str2 ."
		<< endl << endl << endl;

	// The third member function searches a string
	// for a substring as specified by a C-string
	string str3("123-abc-123-abc-456-EFG-456-EFG");
	cout << "The original string str3 is: " << str3 << endl;
	basic_string <char>::size_type indexCh3a, indexCh3b;

	const char *cstr3a = "5G";
	indexCh3a = str3.find_first_of(cstr3a);
	if (indexCh3a != npos)
		cout << "The index of the 1st occurrence of an "
		<< "element of '5G' in str3 after\n the 0th "
		<< "position is: " << indexCh3a << endl;
	else
		cout << "Elements of the substring '5G' were not "
		<< "found in str3\n after the 0th position."
		<< endl;

	const char *cstr3b = "5GF";
	indexCh3b = str3.find_first_of(cstr3b, indexCh3a + 1, 2);
	if (indexCh3b != npos)
		cout << "The index of the second occurrence of an "
		<< "element of '5G' in str3\n after the 0th "
		<< "position is: " << indexCh3b << endl << endl;
	else
		cout << "Elements of the substring '5G' were not "
		<< "found in str3\n after the first occurrrence."
		<< endl << endl;

	// The fourth member function searches a string
	// for a substring as specified by a string
	string str4("12-ab-12-ab");
	cout << "The original string str4 is: " << str4 << endl;
	basic_string <char>::size_type indexCh4a, indexCh4b;

	string str4a("ba3");
	indexCh4a = str4.find_first_of(str4a, 5);
	if (indexCh4a != npos)
		cout << "The index of the 1st occurrence of an "
		<< "element of 'ba3' in str4 after\n the 5th "
		<< "position is: " << indexCh4a << endl;
	else
		cout << "Elements of the substring 'ba3' were not "
		<< "found in str4\n after the 0th position."
		<< endl;

	string str4b("a2");
	indexCh4b = str4.find_first_of(str4b);
	if (indexCh4b != npos)
		cout << "The index of the 1st occurrence of an "
		<< "element of 'a2' in str4 after\n the 0th "
		<< "position is: " << indexCh4b << endl;
	else
		cout << "Elements of the substring 'a2' were not "
		<< "found in str4\n after the 0th position."
		<< endl;

	return;
	/*
	The original string str1 is: abcd-1234-abcd-1234
	The index of the 1st 'd' found after the 5rd position in str1 is: 13
	The character 'x' was not found in str1.

	The original string str2 is: ABCD-1234-ABCD-1234
	The index of the 1st occurrence of an element of 'B1' in str2 after
	the 6th position is: 11
	The index of the 1st element of 'D2' after
	the 0th position in str2 is: 3

	The original string str3 is: 123-abc-123-abc-456-EFG-456-EFG
	The index of the 1st occurrence of an element of '5G' in str3 after
	the 0th position is: 17
	The index of the second occurrence of an element of '5G' in str3
	after the 0th position is: 22

	The original string str4 is: 12-ab-12-ab
	The index of the 1st occurrence of an element of 'ba3' in str4 after
	the 5th position is: 9
	The index of the 1st occurrence of an element of 'a2' in str4 after
	the 0th position is: 1
	请按任意键继续. . .
	*/
}

//通过搜索的最后一个字符的字符串不是指定字符串的任何元素
void basic_string_find_last_not_of(void)
{
	using namespace std;

	// The first member function
	// searches for a single character in a string
	string str1("dddd-1dd4-abdd");
	cout << "The original string str1 is: " << str1 << endl;
	basic_string <char>::size_type indexCh1a, indexCh1b;
	static const basic_string <char>::size_type npos = -1;

	indexCh1a = str1.find_last_not_of("d", 7);
	if (indexCh1a != npos)
		cout << "The index of the last non 'd'\n found before the "
		<< "7th position in str1 is: " << indexCh1a << endl;
	else
		cout << "The non 'd' character was not found ." << endl;

	indexCh1b = str1.find_last_not_of("d");
	if (indexCh1b != npos)
		cout << "The index of the non 'd' found in str1 is: "
		<< indexCh1b << endl << endl;
	else
		cout << "The Character 'non x' was not found in str1."
		<< endl << endl;

	// The second member function searches a string
	// for a substring as specified by a C-string
	string str2("BBB-1111");
	cout << "The original string str2 is: " << str2 << endl;
	basic_string <char>::size_type indexCh2a, indexCh2b;

	const char *cstr2 = "B1";
	indexCh2a = str2.find_last_not_of(cstr2, 6);
	if (indexCh2a != npos)
		cout << "The index of the last occurrence of a "
		<< "element\n not of 'B1' in str2 before the 6th "
		<< "position is: " << indexCh2a << endl;
	else
		cout << "Elements not of the substring 'B1' were not "
		<< "\n found in str2 before the 6th position."
		<< endl;

	const char *cstr2b = "B-1";
	indexCh2b = str2.find_last_not_of(cstr2b);
	if (indexCh2b != npos)
		cout << "The index of the last element not "
		<< "in 'B-1'\n is: "
		<< indexCh2b << endl << endl;
	else
		cout << "The elements of the substring 'B-1' were "
		<< "not found in str2 ."
		<< endl << endl;

	// The third member function searches a string
	// for a substring as specified by a C-string
	string str3("444-555-GGG");
	cout << "The original string str3 is: " << str3 << endl;
	basic_string <char>::size_type indexCh3a, indexCh3b;

	const char *cstr3a = "45G";
	indexCh3a = str3.find_last_not_of(cstr3a);
	if (indexCh3a != npos)
		cout << "The index of the last occurrence of an "
		<< "element in str3\n other than one of the "
		<< "characters in '45G' is: " << indexCh3a
		<< endl;
	else
		cout << "Elements in str3 contain only characters "
		<< " in the string  '45G'. "
		<< endl;

	const char *cstr3b = "45G";
	indexCh3b = str3.find_last_not_of(cstr3b, 6, indexCh3a - 1);
	if (indexCh3b != npos)
		cout << "The index of the penultimate occurrence of an "
		<< "element\n not in '45G' in str3 is: "
		<< indexCh3b << endl << endl;
	else
		cout << "Elements in str3 contain only characters "
		<< " in the string '45G'. "
		<< endl << endl;

	// The fourth member function searches a string
	// for a substring as specified by a string
	string str4("12-ab-12-ab");
	cout << "The original string str4 is: " << str4 << endl;
	basic_string <char>::size_type indexCh4a, indexCh4b;

	string str4a("b-a");
	indexCh4a = str4.find_last_not_of(str4a, 5);
	if (indexCh4a != npos)
		cout << "The index of the last occurrence of an "
		<< "element not\n in 'b-a' in str4 before the 5th "
		<< "position is: " << indexCh4a << endl;
	else
		cout << "Elements other than those in the substring"
		<< " 'b-a' were not found in the string str4."
		<< endl;

	string str4b("12");
	indexCh4b = str4.find_last_not_of(str4b);
	if (indexCh4b != npos)
		cout << "The index of the last occurrence of an "
		<< "element not in '12'\n in str4 before the end "
		<< "position is: " << indexCh4b << endl;
	else
		cout << "Elements other than those in the substring"
		<< " '12'\n were not found in the string str4."
		<< endl;

	return;
	/*
	The original string str1 is: dddd-1dd4-abdd
	The index of the last non 'd'
	found before the 7th position in str1 is: 5
	The index of the non 'd' found in str1 is: 11

	The original string str2 is: BBB-1111
	The index of the last occurrence of a element
	not of 'B1' in str2 before the 6th position is: 3
	The elements of the substring 'B-1' were not found in str2 .

	The original string str3 is: 444-555-GGG
	The index of the last occurrence of an element in str3
	other than one of the characters in '45G' is: 7
	The index of the penultimate occurrence of an element
	not in '45G' in str3 is: 3

	The original string str4 is: 12-ab-12-ab
	The index of the last occurrence of an element not
	in 'b-a' in str4 before the 5th position is: 1
	The index of the last occurrence of an element not in '12'
	in str4 before the end position is: 10
	请按任意键继续. . .
	*/
}

//通过搜索的最后一个字符字符串,是一个指定字符串的元素
void basic_string_find_last_of(void)
{
	using namespace std;

	// The first member function
	// searches for a single character in a string
	string str1("abcd-1234-abcd-1234");
	cout << "The original string str1 is: " << str1 << endl;
	basic_string <char>::size_type indexCh1a, indexCh1b;
	static const basic_string <char>::size_type npos = -1;

	indexCh1a = str1.find_last_of("d", 14);
	if (indexCh1a != npos)
		cout << "The index of the last 'd' found before the 14th"
		<< " position in str1 is: " << indexCh1a << endl;
	else
		cout << "The character 'd' was not found in str1 ." << endl;

	indexCh1b = str1.find_first_of("x");
	if (indexCh1b != npos)
		cout << "The index of the 'x' found in str1 is: "
		<< indexCh1b << endl << endl;
	else
		cout << "The character 'x' was not found in str1."
		<< endl << endl;

	// The second member function searches a string
	// for a substring as specified by a C-string
	string str2("ABCD-1234-ABCD-1234");
	cout << "The original string str2 is: " << str2 << endl;
	basic_string <char>::size_type indexCh2a, indexCh2b;

	const char *cstr2 = "B1";
	indexCh2a = str2.find_last_of(cstr2, 12);
	if (indexCh2a != npos)
		cout << "The index of the last occurrence of an "
		<< "element of 'B1' in str2 before\n the 12th "
		<< "position is: " << indexCh2a << endl;
	else
		cout << "Elements of the substring 'B1' were not "
		<< "found in str2 before the 12th position."
		<< endl;

	const char *cstr2b = "D2";
	indexCh2b = str2.find_last_of(cstr2b);
	if (indexCh2b != npos)
		cout << "The index of the last element of 'D2' "
		<< "after\n the 0th position in str2 is: "
		<< indexCh2b << endl << endl;
	else
		cout << "The substring 'D2' was not found in str2 ."
		<< endl << endl << endl;

	// The third member function searches a string
	// for a substring as specified by a C-string
	string str3("456-EFG-456-EFG");
	cout << "The original string str3 is: " << str3 << endl;
	basic_string <char>::size_type indexCh3a;

	const char *cstr3a = "5E";
	indexCh3a = str3.find_last_of(cstr3a, 8, 8);
	if (indexCh3a != npos)
		cout << "The index of the last occurrence of an "
		<< "element of '5E' in str3 before\n the 8th "
		<< "position is: " << indexCh3a << endl << endl;
	else
		cout << "Elements of the substring '5G' were not "
		<< "found in str3\n before the 8th position."
		<< endl << endl;

	// The fourth member function searches a string
	// for a substring as specified by a string
	string str4("12-ab-12-ab");
	cout << "The original string str4 is: " << str4 << endl;
	basic_string <char>::size_type indexCh4a, indexCh4b;

	string str4a("ba3");
	indexCh4a = str4.find_last_of(str4a, 8);
	if (indexCh4a != npos)
		cout << "The index of the last occurrence of an "
		<< "element of 'ba3' in str4 before\n the 8th "
		<< "position is: " << indexCh4a << endl;
	else
		cout << "Elements of the substring 'ba3' were not "
		<< "found in str4\n after the 0th position."
		<< endl;

	string str4b("a2");
	indexCh4b = str4.find_last_of(str4b);
	if (indexCh4b != npos)
		cout << "The index of the last occurrence of an "
		<< "element of 'a2' in str4 before\n the 0th "
		<< "position is: " << indexCh4b << endl;
	else
		cout << "Elements of the substring 'a2' were not "
		<< "found in str4\n after the 0th position."
		<< endl;

	return;
	/*
	The original string str1 is: abcd-1234-abcd-1234
	The index of the last 'd' found before the 14th position in str1 is: 1
	The character 'x' was not found in str1.

	The original string str2 is: ABCD-1234-ABCD-1234
	The index of the last occurrence of an element of 'B1' in str2 before
	the 12th position is: 11
	The index of the last element of 'D2' after
	the 0th position in str2 is: 16

	The original string str3 is: 456-EFG-456-EFG
	The index of the last occurrence of an element of '5E' in str3 before
	the 8th position is: 4

	The original string str4 is: 12-ab-12-ab
	The index of the last occurrence of an element of 'ba3' in str4 before
	the 8th position is: 4
	The index of the last occurrence of an element of 'a2' in str4 before
	the 0th position is: 9
	请按任意键继续. . .
	*/
}

//返回分配器对象的副本,用于构造串
void basic_string_get_allocator(void)
{
	using namespace std;
	// The following lines declare objects
	// that use the default allocator.
	string s1;
	basic_string <char> s2;
	basic_string <char, char_traits< char >, allocator< char > > s3;

	// s4 will use the same allocator class as s1
	basic_string <char> s4(s1.get_allocator());

	basic_string <char>::allocator_type xchar = s1.get_allocator();
	// You can now call functions on the allocator class xchar used by s1

	return;
}

//在插入指定位置的元素或多个元素或者一个范围元素融入串
void basic_string_insert(void)
{
	using namespace std;

	// The first member function inserting a C-string
	// at a given position
	basic_string <char> str1a("way");
	const char *cstr1a = "a";
	str1a.insert(0, cstr1a);
	cout << "The string with a C-string inserted at position 0 is: "
		<< str1a << "." << endl;

	// The second member function inserting a C-string
	// at a given position for a specified number of elements
	basic_string <char> str2a("Good");
	const char *cstr2a = "Bye Bye Baby";
	str2a.insert(4, cstr2a, 3);
	cout << "The string with a C-string inserted at the end is: "
		<< str2a << "." << endl;

	// The third member function inserting a string
	// at a given position
	basic_string <char> str3a("Bye");
	string str3b("Good");
	str3a.insert(0, str3b);
	cout << "The string with a string inserted at position 0 is: "
		<< str3a << "." << endl;

	// The fourth member function inserting part of
	// a string at a given position
	basic_string <char> str4a("Good ");
	string str4b("Bye Bye Baby");
	str4a.insert(5, str4b, 8, 4);
	cout << "The string with part of a string inserted at position 4 is: "
		<< str4a << "." << endl;


	// The fifth member function inserts a number of characters
	// at a specified position in the string
	string str5("The number is: .");
	str5.insert(15, 3, '3');
	cout << "The string with characters inserted is: "
		<< str5 << endl;

	// The sixth member function inserts a character
	// at a specified position in the string
	string str6("ABCDFG");
	basic_string <char>::iterator str6_Iter = (str6.begin() + 4);
	str6.insert(str6_Iter, 'e');
	cout << "The string with a character inserted is: "
		<< str6 << endl;

	// The seventh member function inserts a range
	// at a specified position in the string
	string str7a("ABCDHIJ");
	string str7b("abcdefgh");
	basic_string <char>::iterator str7a_Iter = (str7a.begin() + 4);
	str7a.insert(str7a_Iter, str7b.begin() + 4, str7b.end() - 1);
	cout << "The string with a character inserted from a range is: "
		<< str7a << endl;

	// The eigth member function inserts a number of
	// characters at a specified position in the string
	string str8("ABCDHIJ");
	basic_string <char>::iterator str8_Iter = (str8.begin() + 4);
	str8.insert(str8_Iter, 3, 'e');
	cout << "The string with a character inserted from a range is: "
		<< str8 << endl;

	return;
	/*
	The string with a C-string inserted at position 0 is: away.
	The string with a C-string inserted at the end is: GoodBye.
	The string with a string inserted at position 0 is: GoodBye.
	The string with part of a string inserted at position 4 is: Good Baby.
	The string with characters inserted is: The number is: 333.
	The string with a character inserted is: ABCDeFG
	The string with a character inserted from a range is: ABCDefgHIJ
	The string with a character inserted from a range is: ABCDeeeHIJ
	请按任意键继续. . .
	*/
}

//返回字符串中的元素的当前数目
void basic_string_length(void)
{
	using namespace std;
	string str1("Hello world");
	cout << "The original string str1 is: " << str1 << endl;

	// The size and length member functions differ in name only
	basic_string <char>::size_type sizeStr1, lenStr1;
	sizeStr1 = str1.size();
	lenStr1 = str1.length();

	basic_string <char>::size_type capStr1, max_sizeStr1;
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	cout << "The current size of original string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of original string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of original string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of original string str1 is: "
		<< max_sizeStr1 << "." << endl << endl;

	str1.erase(6, 5);
	cout << "The modified string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	lenStr1 = str1.length();
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	// after erasing part of the original string
	cout << "The current size of modified string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of modified string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of modified string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of modified string str1 is: "
		<< max_sizeStr1 << "." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The current size of original string str1 is: 11.
	The current length of original string str1 is: 11.
	The capacity of original string str1 is: 15.
	The max_size of original string str1 is: 4294967294.

	The modified string str1 is: Hello
	The current size of modified string str1 is: 6.
	The current length of modified string str1 is: 6.
	The capacity of modified string str1 is: 15.
	The max_size of modified string str1 is: 4294967294.
	请按任意键继续. . .
	*/
}

//返回一个字符串应该包含字符的最大数量
void basic_string_max_size(void)
{
	using namespace std;
	string str1("Hello world");
	cout << "The original string str1 is: " << str1 << endl;

	// The size and length member functions differ in name only
	basic_string <char>::size_type sizeStr1, lenStr1;
	sizeStr1 = str1.size();
	lenStr1 = str1.length();

	basic_string <char>::size_type capStr1, max_sizeStr1;
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	cout << "The current size of original string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of original string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of original string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of original string str1 is: "
		<< max_sizeStr1 << "." << endl << endl;

	str1.erase(6, 5);
	cout << "The modified string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	lenStr1 = str1.length();
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	// after erasing part of the original string
	cout << "The current size of modified string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of modified string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of modified string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of modified string str1 is: "
		<< max_sizeStr1 << "." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The current size of original string str1 is: 11.
	The current length of original string str1 is: 11.
	The capacity of original string str1 is: 15.
	The max_size of original string str1 is: 4294967294.

	The modified string str1 is: Hello
	The current size of modified string str1 is: 6.
	The current length of modified string str1 is: 6.
	The capacity of modified string str1 is: 15.
	The max_size of modified string str1 is: 4294967294.
	请按任意键继续. . .
	*/
}

//将一个元素添加到字符串的末尾
void basic_string_push_back(void)
{
	using namespace std;
	string str1("abc");
	basic_string <char>::iterator str_Iter, str1_Iter;

	cout << "The original string str1 is: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	// str1.push_back ( 'd' );
	str1_Iter = str1.end();
	str1_Iter--;
	cout << "The last character-letter of the modified str1 is now: "
		<< *str1_Iter << endl;

	cout << "The modified string str1 is: ";
	for (str_Iter = str1.begin(); str_Iter != str1.end(); str_Iter++)
		cout << *str_Iter;
	cout << endl;

	return;
	/*
	The original string str1 is: abc
	The last character-letter of the modified str1 is now: c
	The modified string str1 is: abc
	请按任意键继续. . .
	*/
}

//返回一个迭代器的第一个元素的字符串逆转
void basic_string_rbegin(void)
{
	using namespace std;
	string str1("Able was I ere I saw Elba"), str2;
	basic_string <char>::reverse_iterator str_rIter, str1_rIter, str2_rIter;
	basic_string <char>::const_reverse_iterator str1_rcIter;

	str1_rIter = str1.rbegin();
	// str1_rIter--;
	cout << "The first character-letter of the reversed string str1 is: "
		<< *str1_rIter << endl;
	cout << "The full reversed string str1 is:\n ";
	for (str_rIter = str1.rbegin(); str_rIter != str1.rend(); str_rIter++)
		cout << *str_rIter;
	cout << endl;

	// The dereferenced iterator can be used to modify a character
	*str1_rIter = 'A';
	cout << "The first character-letter of the modified str1 is now: "
		<< *str1_rIter << endl;
	cout << "The full modified reversed string str1 is now:\n ";
	for (str_rIter = str1.rbegin(); str_rIter != str1.rend(); str_rIter++)
		cout << *str_rIter;
	cout << endl;

	// The following line would be an error because iterator is const
	// *str1_rcIter = 'A';

	// For an empty string, begin is equivalent to end
	if (str2.rbegin() == str2.rend())
		cout << "The string str2 is empty." << endl;
	else
		cout << "The stringstr2  is not empty." << endl;

	return;
	/*
	The first character-letter of the reversed string str1 is: a
	The full reversed string str1 is:
	ablE was I ere I saw elbA
	The first character-letter of the modified str1 is now: A
	The full modified reversed string str1 is now:
	AblE was I ere I saw elbA
	The string str2 is empty.
	请按任意键继续. . .
	*/
}

//返回一个指向刚刚超越了逆转字符串中的最后一个元素的迭代
void basic_string_rend(void)
{
	using namespace std;
	string str1("Able was I ere I saw Elba"), str2;
	basic_string <char>::reverse_iterator str_rIter, str1_rIter, str2_rIter;
	basic_string <char>::const_reverse_iterator str1_rcIter;

	str1_rIter = str1.rend();
	str1_rIter--;
	cout << "The last character-letter of the reversed string str1 is: "
		<< *str1_rIter << endl;
	cout << "The full reversed string str1 is:\n ";
	for (str_rIter = str1.rbegin(); str_rIter != str1.rend(); str_rIter++)
		cout << *str_rIter;
	cout << endl;

	// The dereferenced iterator can be used to modify a character
	*str1_rIter = 'o';
	cout << "The last character-letter of the modified str1 is now: "
		<< *str1_rIter << endl;
	cout << "The full modified reversed string str1 is now:\n ";
	for (str_rIter = str1.rbegin(); str_rIter != str1.rend(); str_rIter++)
		cout << *str_rIter;
	cout << endl;

	// The following line would be an error because iterator is const
	// *str1_rcIter = 'T';

	// For an empty string, end is equivalent to begin
	if (str2.rbegin() == str2.rend())
		cout << "The string str2 is empty." << endl;
	else
		cout << "The stringstr2  is not empty." << endl;

	return;
	/*
	The last character-letter of the reversed string str1 is: A
	The full reversed string str1 is:
	ablE was I ere I saw elbA
	The last character-letter of the modified str1 is now: o
	The full modified reversed string str1 is now:
	ablE was I ere I saw elbo
	The string str2 is empty.
	请按任意键继续. . .
	*/
}

//在一个字符串替换元素的指定位置与来自其它范围或字符串或C字符串复制指定的字符或字符
void basic_string_replace(void)
{
	using namespace std;

	// The first two member functions replace
	// part of the operand string with
	// characters from a parameter string or C-string
	string result1a, result1b;
	string s1o("AAAAAAAA");
	string s1p("BBB");
	const char* cs1p = "CCC";
	cout << "The operand string s1o is: " << s1o << endl;
	cout << "The parameter string s1p is: " << s1p << endl;
	cout << "The parameter C-string cs1p is: " << cs1p << endl;
	result1a = s1o.replace(1, 3, s1p);
	cout << "The result of s1o.replace ( 1 , 3 , s1p )\n is "
		<< "the string: " << result1a << "." << endl;
	result1b = s1o.replace(5, 3, cs1p);
	cout << "The result of s1o.replace ( 5 , 3 , cs1p )\n is "
		<< "the string: " << result1b << "." << endl;
	cout << endl;

	// The third & fourth member function replace
	// part of the operand string with characters
	// form part of a parameter string or C-string
	string result2a, result2b;
	string s2o("AAAAAAAA");
	string s2p("BBB");
	const char* cs2p = "CCC";
	cout << "The operand string s2o is: " << s2o << endl;
	cout << "The parameter string s1p is: " << s2p << endl;
	cout << "The parameter C-string cs2p is: " << cs2p << endl;
	result2a = s2o.replace(1, 3, s2p, 1, 2);
	cout << "The result of s2o.replace (1, 3, s2p, 1, 2)\n is "
		<< "the string: " << result2a << "." << endl;
	result2b = s2o.replace(4, 3, cs2p, 1);
	cout << "The result of s2o.replace (4 ,3 ,cs2p)\n is "
		<< "the string: " << result2b << "." << endl;
	cout << endl;

	// The fifth member function replaces
	// part of the operand string with characters
	string result3a;
	string s3o("AAAAAAAA");
	char ch3p = 'C';
	cout << "The operand string s3o is: " << s3o << endl;
	cout << "The parameter character c1p is: " << ch3p << endl;
	result3a = s3o.replace(1, 3, 4, ch3p);
	cout << "The result of s3o.replace(1, 3, 4, ch3p)\n is "
		<< "the string: " << result3a << "." << endl;
	cout << endl;

	// The sixth & seventh member functions replace
	// part of the operand string, delineated with iterators,
	// with a parameter string or C-string
	string s4o("AAAAAAAA");
	string s4p("BBB");
	const char* cs4p = "CCC";
	cout << "The operand string s4o is: " << s4o << endl;
	cout << "The parameter string s4p is: " << s4p << endl;
	cout << "The parameter C-string cs4p is: " << cs4p << endl;
	basic_string<char>::iterator IterF0, IterL0;
	IterF0 = s4o.begin();
	IterL0 = s4o.begin() + 3;
	string result4a, result4b;
	result4a = s4o.replace(IterF0, IterL0, s4p);
	cout << "The result of s1o.replace (IterF0, IterL0, s4p)\n is "
		<< "the string: " << result4a << "." << endl;
	result4b = s4o.replace(IterF0, IterL0, cs4p);
	cout << "The result of s4o.replace (IterF0, IterL0, cs4p)\n is "
		<< "the string: " << result4b << "." << endl;
	cout << endl;

	// The 8th member function replaces
	// part of the operand string delineated with iterators
	// with a number of characters from a parameter C-string
	string s5o("AAAAAAAF");
	const char* cs5p = "CCCBB";
	cout << "The operand string s5o is: " << s5o << endl;
	cout << "The parameter C-string cs5p is: " << cs5p << endl;
	basic_string<char>::iterator IterF1, IterL1;
	IterF1 = s5o.begin();
	IterL1 = s5o.begin() + 4;
	string result5a;
	result5a = s5o.replace(IterF1, IterL1, cs5p, 4);
	cout << "The result of s5o.replace (IterF1, IterL1, cs4p ,4)\n is "
		<< "the string: " << result5a << "." << endl;
	cout << endl;

	// The 9th member function replaces
	// part of the operand string delineated with iterators
	// with specified characters
	string s6o("AAAAAAAG");
	char ch6p = 'q';
	cout << "The operand string s6o is: " << s6o << endl;
	cout << "The parameter character ch6p is: " << ch6p << endl;
	basic_string<char>::iterator IterF2, IterL2;
	IterF2 = s6o.begin();
	IterL2 = s6o.begin() + 3;
	string result6a;
	result6a = s6o.replace(IterF2, IterL2, 4, ch6p);
	cout << "The result of s6o.replace (IterF1, IterL1, 4, ch6p)\n is "
		<< "the string: " << result6a << "." << endl;
	cout << endl;

	// The 10th member function replaces
	// part of the operand string delineated with iterators
	// with part of a parameter string delineated with iterators
	string s7o("OOOOOOO");
	string s7p("PPPP");
	cout << "The operand string s7o is: " << s7o << endl;
	cout << "The parameter string s7p is: " << s7p << endl;
	basic_string<char>::iterator IterF3, IterL3, IterF4, IterL4;
	IterF3 = s7o.begin() + 1;
	IterL3 = s7o.begin() + 3;
	IterF4 = s7p.begin();
	IterL4 = s7p.begin() + 2;
	string result7a;
	result7a = s7o.replace(IterF3, IterL3, IterF4, IterL4);
	cout << "The result of s7o.replace (IterF3 ,IterL3 ,IterF4 ,IterL4)\n is "
		<< "the string: " << result7a << "." << endl;
	cout << endl;

	return;
	/*
	The parameter character c1p is: C
	The result of s3o.replace(1, 3, 4, ch3p)
	is the string: ACCCCAAAA.

	The operand string s4o is: AAAAAAAA
	The parameter string s4p is: BBB
	The parameter C-string cs4p is: CCC
	The result of s1o.replace (IterF0, IterL0, s4p)
	is the string: BBBAAAAA.
	The result of s4o.replace (IterF0, IterL0, cs4p)
	is the string: CCCAAAAA.

	The operand string s5o is: AAAAAAAF
	The parameter C-string cs5p is: CCCBB
	The result of s5o.replace (IterF1, IterL1, cs4p ,4)
	is the string: CCCBAAAF.

	The operand string s6o is: AAAAAAAG
	The parameter character ch6p is: q
	The result of s6o.replace (IterF1, IterL1, 4, ch6p)
	is the string: qqqqAAAAG.

	The operand string s7o is: OOOOOOO
	The parameter string s7p is: PPPP
	The result of s7o.replace (IterF3 ,IterL3 ,IterF4 ,IterL4)
	is the string: OPPOOOO.

	请按任意键继续. . .
	*/
}

//设置字符串与数的能力至少一样大指定数目
void basic_string_reserve(void)
{
	using namespace std;
	string str1("Hello world");
	cout << "The original string str1 is: " << str1 << endl;

	basic_string <char>::size_type sizeStr1, sizerStr1;
	sizeStr1 = str1.size();
	basic_string <char>::size_type capStr1, caprStr1;
	capStr1 = str1.capacity();

	// Compare size & capacity of the original string
	cout << "The current size of original string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The capacity of original string str1 is: "
		<< capStr1 << "." << endl << endl;

	// Compare size & capacity of the string
	// with added capacity
	str1.reserve(40);
	sizerStr1 = str1.size();
	caprStr1 = str1.capacity();

	cout << "The string str1with augmented capacity is: "
		<< str1 << endl;
	cout << "The current size of string str1 is: "
		<< sizerStr1 << "." << endl;
	cout << "The new capacity of string str1 is: "
		<< caprStr1 << "." << endl << endl;

	// Compare size & capacity of the string
	// with downsized capacity
	str1.reserve();
	basic_string <char>::size_type sizedStr1;
	basic_string <char>::size_type capdStr1;
	sizedStr1 = str1.size();
	capdStr1 = str1.capacity();

	cout << "The string str1 with downsized capacity is: "
		<< str1 << endl;
	cout << "The current size of string str1 is: "
		<< sizedStr1 << "." << endl;
	cout << "The reduced capacity of string str1 is: "
		<< capdStr1 << "." << endl << endl;

	return;
	/*
	The original string str1 is: Hello world
	The current size of original string str1 is: 11.
	The capacity of original string str1 is: 15.

	The string str1with augmented capacity is: Hello world
	The current size of string str1 is: 11.
	The new capacity of string str1 is: 47.

	The string str1 with downsized capacity is: Hello world
	The current size of string str1 is: 11.
	The reduced capacity of string str1 is: 47.

	请按任意键继续. . .
	*/
}

//指定一个新的大小为一个字符串,添加或删除元素的要求
void basic_string_resize(void)
{
	using namespace std;
	string  str1("Hello world");
	cout << "The original string str1 is: " << str1 << endl;

	basic_string <char>::size_type sizeStr1;
	sizeStr1 = str1.size();
	basic_string <char>::size_type capStr1;
	capStr1 = str1.capacity();

	// Compare size & capacity of the original string
	cout << "The current size of original string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The capacity of original string str1 is: "
		<< capStr1 << "." << endl << endl;

	// Use resize to increase size by 2 elements: exclamations
	str1.resize(str1.size() + 2, '!');
	cout << "The resized string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	capStr1 = str1.capacity();

	// Compare size & capacity of a string after resizing
	cout << "The current size of resized string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The capacity of resized string str1 is: "
		<< capStr1 << "." << endl << endl;

	// Use resize to increase size by 20 elements:
	str1.resize(str1.size() + 20);
	cout << "The resized string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	capStr1 = str1.capacity();

	// Compare size & capacity of a string after resizing
	// note capacity increases automatically as required
	cout << "The current size of modified string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The capacity of modified string str1 is: "
		<< capStr1 << "." << endl << endl;

	// Use resize to downsize by 28 elements:
	str1.resize(str1.size() - 28);
	cout << "The downsized string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	capStr1 = str1.capacity();

	// Compare size & capacity of a string after downsizing
	cout << "The current size of downsized string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The capacity of downsized string str1 is: "
		<< capStr1 << "." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The current size of original string str1 is: 11.
	The capacity of original string str1 is: 15.

	The resized string str1 is: Hello world!!
	The current size of resized string str1 is: 13.
	The capacity of resized string str1 is: 15.

	The resized string str1 is: Hello world!!
	*/
}

//搜索在向后方向的字符串的子字符串的第一次出现的字符的指定序列相匹配
void basic_string_rfind(void)
{
	using namespace std;

	// The first member function
	// searches for a single character in a string
	string str1("Hello Everyone");
	cout << "The original string str1 is: " << str1 << endl;
	basic_string <char>::size_type indexCh1a, indexCh1b;
	static const basic_string <char>::size_type npos = -1;

	indexCh1a = str1.rfind("e", 9);
	if (indexCh1a != npos)
		cout << "The index of the 1st 'e' found before the 9th"
		<< " position in str1 is: " << indexCh1a << endl;
	else
		cout << "The character 'e' was not found in str1 ." << endl;

	indexCh1b = str1.rfind("x");
	if (indexCh1b != npos)
		cout << "The index of the 'x' found in str1 is: "
		<< indexCh1b << endl << endl;
	else
		cout << "The character 'x' was not found in str1."
		<< endl << endl;

	// The second member function searches a string
	// for a substring as specified by a C-string
	string str2("Let me make this perfectly clear.");
	cout << "The original string str2 is: " << str2 << endl;
	basic_string <char>::size_type indexCh2a, indexCh2b;

	const char *cstr2 = "perfect";
	indexCh2a = str2.rfind(cstr2, 30);
	if (indexCh2a != npos)
		cout << "The index of the 1st element of 'perfect' "
		<< "before\n the 30th position in str2 is: "
		<< indexCh2a << endl;
	else
		cout << "The substring 'perfect' was not found in str2 ."
		<< endl;

	const char *cstr2b = "imperfectly";
	indexCh2b = str2.rfind(cstr2b, 30);
	if (indexCh2b != npos)
		cout << "The index of the 1st element of 'imperfect' "
		<< "before\n the 5th position in str3 is: "
		<< indexCh2b << endl;
	else
		cout << "The substring 'imperfect' was not found in str2 ."
		<< endl << endl;

	// The third member function searches a string
	// for a substring as specified by a C-string
	string str3("It is a nice day. I am happy.");
	cout << "The original string str3 is: " << str3 << endl;
	basic_string <char>::size_type indexCh3a, indexCh3b;

	const char *cstr3a = "nice";
	indexCh3a = str3.rfind(cstr3a);
	if (indexCh3a != npos)
		cout << "The index of the 1st element of 'nice' "
		<< "in str3 is: " << indexCh3a << endl;
	else
		cout << "The substring 'nice' was not found in str3 ."
		<< endl;

	const char *cstr3b = "am";
	indexCh3b = str3.rfind(cstr3b, indexCh3a + 25, 2);
	if (indexCh3b != npos)
		cout << "The index of the next occurrance of 'am' in "
		<< "str3 begins at: " << indexCh3b << endl << endl;
	else
		cout << "There is no next occurrence of 'am' in str3 ."
		<< endl << endl;

	// The fourth member function searches a string
	// for a substring as specified by a string
	string str4("This perfectly unclear.");
	cout << "The original string str4 is: " << str4 << endl;
	basic_string <char>::size_type indexCh4a, indexCh4b;

	string str4a("clear");
	indexCh4a = str4.rfind(str4a, 15);
	if (indexCh4a != npos)
		cout << "The index of the 1st element of 'clear' "
		<< "before\n the 15th position in str4 is: "
		<< indexCh4a << endl;
	else
		cout << "The substring 'clear' was not found in str4 "
		<< "before the 15th position." << endl;

	string str4b("clear");
	indexCh4b = str4.rfind(str4b);
	if (indexCh4b != npos)
		cout << "The index of the 1st element of 'clear' "
		<< "in str4 is: "
		<< indexCh4b << endl;
	else
		cout << "The substring 'clear' was not found in str4 ."
		<< endl << endl;

	return;
	/*
	The original string str1 is: Hello Everyone
	The index of the 1st 'e' found before the 9th position in str1 is: 8
	The character 'x' was not found in str1.

	The original string str2 is: Let me make this perfectly clear.
	The index of the 1st element of 'perfect' before
	the 30th position in str2 is: 17
	The substring 'imperfect' was not found in str2 .

	The original string str3 is: It is a nice day. I am happy.
	The index of the 1st element of 'nice' in str3 is: 8
	The index of the next occurrance of 'am' in str3 begins at: 20

	The original string str4 is: This perfectly unclear.
	The substring 'clear' was not found in str4 before the 15th position.
	The index of the 1st element of 'clear' in str4 is: 17
	请按任意键继续. . .

	*/
}

//返回字符串中的元素的当前数目
void basic_string_size(void)
{
	using namespace std;
	string str1("Hello world");
	cout << "The original string str1 is: " << str1 << endl;

	// The size and length member functions differ in name only
	basic_string <char>::size_type sizeStr1, lenStr1;
	sizeStr1 = str1.size();
	lenStr1 = str1.length();

	basic_string <char>::size_type capStr1, max_sizeStr1;
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	cout << "The current size of original string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of original string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of original string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of original string str1 is: "
		<< max_sizeStr1 << "." << endl << endl;

	str1.erase(6, 5);
	cout << "The modified string str1 is: " << str1 << endl;

	sizeStr1 = str1.size();
	lenStr1 = str1.length();
	capStr1 = str1.capacity();
	max_sizeStr1 = str1.max_size();

	// Compare size, length, capacity & max_size of a string
	// after erasing part of the original string
	cout << "The current size of modified string str1 is: "
		<< sizeStr1 << "." << endl;
	cout << "The current length of modified string str1 is: "
		<< lenStr1 << "." << endl;
	cout << "The capacity of modified string str1 is: "
		<< capStr1 << "." << endl;
	cout << "The max_size of modified string str1 is: "
		<< max_sizeStr1 << "." << endl;

	return;
	/*
	The original string str1 is: Hello world
	The current size of original string str1 is: 11.
	The current length of original string str1 is: 11.
	The capacity of original string str1 is: 15.
	The max_size of original string str1 is: 4294967294.

	The modified string str1 is: Hello
	The current size of modified string str1 is: 6.
	The current length of modified string str1 is: 6.
	The capacity of modified string str1 is: 15.
	The max_size of modified string str1 is: 4294967294.
	请按任意键继续. . .
	*/
}

//副本最多某些字符数的一个字符串的子字符串从指定的位置开始
void basic_string_substr(void)
{
	using namespace std;

	string  str1("Heterological paradoxes are persistent.");
	cout << "The original string str1 is: \n " << str1
		<< endl << endl;

	basic_string <char> str2 = str1.substr(6, 7);
	cout << "The substring str1 copied is: " << str2
		<< endl << endl;

	basic_string <char> str3 = str1.substr();
	cout << "The default substring str3 is: \n " << str3
		<< "\n which is the entire original string." << endl;


	return;
	/*
	The original string str1 is:
	Heterological paradoxes are persistent.

	The substring str1 copied is: logical

	The default substring str3 is:
	Heterological paradoxes are persistent.
	which is the entire original string.
	请按任意键继续. . .
	*/
}

//交换两个字符串的内容
void basic_string_swap(void)
{
	using namespace std;

	// Declaring an objects of type basic_string<char>
	string s1("Tweedledee");
	string s2("Tweedledum");
	cout << "Before swapping string s1 and s2:" << endl;
	cout << " The basic_string s1 = " << s1 << "." << endl;
	cout << " The basic_string s2 = " << s2 << "." << endl;

	s1.swap(s2);
	cout << "After swapping string s1 and s2:" << endl;
	cout << " The basic_string s1 = " << s1 << "." << endl;
	cout << " The basic_string s2 = " << s2 << "." << endl;

	return;
	/*
	Before swapping string s1 and s2:
	The basic_string s1 = Tweedledee.
	The basic_string s2 = Tweedledum.
	After swapping string s1 and s2:
	The basic_string s1 = Tweedledum.
	The basic_string s2 = Tweedledee.
	请按任意键继续. . .
	*/
}


你可能感兴趣的:(c++ basic_strings)