STL-------string的API

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/XUCHEN1230/article/details/86502724
#include<iostream>
#include<string>
#include<stdexcept>

using namespace std;



//string 容器;
//包含#include<string>头文件;


//string 构造函数的基本操作
void test01()
{
	/*
	string();//创建一个空的字符串 例如: string str;
	string(const string& str);//使用一个string对象初始化另一个string对象
	string(const char* s);//使用字符串s初始化
	string(int n, char c);//使用n个字符c初始化
	*/

	string str;			//默认构造函数
	string str1(str);	//拷贝构造函数
	string str2 = str;	//拷贝构造函数
	string str3 = "sadsf";//有参构造函数
	string str4(10, 'c');//有构造函数
	cout << str3 << endl;
	cout << str4 << endl;
}
//显示结果和预期一样;

void test02()
{
	/*
	string基本赋值操作
	string& operator=(const char* s);//char*类型字符串 赋值给当前的字符串
	string& operator=(const string &s);//把字符串s赋给当前的字符串
	string& operator=(char c);//字符赋值给当前的字符串
	string& assign(const char *s);//把字符串s赋给当前的字符串
	string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
	string& assign(const string &s);//把字符串s赋给当前字符串
	string& assign(int n, char c);//用n个字符c赋给当前字符串
	string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串
	*/
	string str;
	str = "hello";
	string str2;
	str2 = str;
	cout << str << endl << str2 << endl;

	//assign的重载版本:
	//string& assign(const char *s, int n);//把字符串s的前n个字符赋给当前的字符串
	string str3;
	str3.assign("asddaf", 3);
	cout << str3 << endl;
	//输出asd;

	//string& assign(const string &s, int start, int n);//将s从start开始n个字符赋值给字符串
	string str4;
	str4.assign(str, 1, 3);
	cout << str4 << endl;//从0开始索引;
	//输出的是“ell";
}


//string 存取字符串:
/*
char& operator[](int n);//通过[]方式取字符
char& at(int n);//通过at方法获取字符
*/
void test03()
{
	string a = "hello world";
	//方法一:
	for (auto i = 0; i < a.size(); i++) {
		cout << a[i] << endl;
	}
	//方法二:
	for (auto i = 0; i < a.size(); i++) {
		cout << a.at(i) << endl;
	}
	//[]和at的区别:
	//[]越界后会直接挂掉,at越界后会抛出异常;
	/*
	try {
		cout << a[100] << endl;
	}
	catch(...){
		cout << "越界异常" << endl;
	}
	*/
	//[]的越界异常不会被捕捉,屏幕无输出;
	try {
		cout << a.at(100) << endl;
	}
	catch (out_of_range & e) {
		cout<<e.what()<<endl;
	}
	//默认的异常;
	catch (...) {
		cout << "越界异常" << endl;
	}
	//at()的异常可以被捕捉,屏幕有输出;
}

/*
string拼接操作
string& operator+=(const string& str);//重载+=操作符
string& operator+=(const char* str);//重载+=操作符
string& operator+=(const char c);//重载+=操作符
string& append(const char *s);//把字符串s连接到当前字符串结尾
string& append(const char *s, int n);//把字符串s的前n个字符连接到当前字符串结尾
string& append(const string &s);//同operator+=()
string& append(const string &s, int pos, int n);//把字符串s中从pos开始的n个字符连接到当前字符串结尾
string& append(int n, char c);//在当前字符串结尾添加n个字符c
*/
/*
string查找和替换
int find(const string& str, int pos = 0) const; //查找str第一次出现位置,从pos开始查找
int find(const char* s, int pos = 0) const;  //查找s第一次出现位置,从pos开始查找
int find(const char* s, int pos, int n) const;  //从pos位置查找s的前n个字符第一次位置
int find(const char c, int pos = 0) const;  //查找字符c第一次出现位置
int rfind(const string& str, int pos = npos) const;//查找str最后一次位置,从pos开始查找
int rfind(const char* s, int pos = npos) const;//查找s最后一次出现位置,从pos开始查找
int rfind(const char* s, int pos, int n) const;//从pos查找s的前n个字符最后一次位置
int rfind(const char c, int pos = 0) const; //查找字符c最后一次出现位置
string& replace(int pos, int n, const string& str); //替换从pos开始n个字符为字符串str
string& replace(int pos, int n, const char* s); //替换从pos开始的n个字符为字符串s
*/
void test04()
{
	//+重载;
	string s1 = "我";
	string s2 = "爱北京";
	s1 += s2;
	cout << s1 << endl;
	
	//append重载:
	s1.append("天安门");
	cout << s1 << endl;

	//find操作:
	string s3 = "abcdefg";
	int pos = s3.find("bc");

	cout << "pos = " << pos << endl;
	//pos=1;是"bc"从0开始出现的第一个位置;
	//按b出现的第一个位置查找;

	pos = s3.find("sad");
	cout << "pos=" << pos << endl;
	//找不到这样的字符串,返回-1;

	pos = s3.rfind("bc");
	cout << "pos=" << pos << endl;
	//也是显示的1;
	//内部的查找机制不一样,方向不一样,但是结果一样;


	//替换操作:
	string s4 = "hello";
	s4.replace(1, 3, "111");
	cout << s4 << endl;//h1110;

	s4.replace(1, 3, "111111");
	cout << s4 << endl;//h1111110;
}


/*
 string比较操作
compare函数在>时返回 1,<时返回 -1,==时返回 0。
比较区分大小写,比较时参考字典顺序,排越前面的越小。
大写的A比小写的a小。
int compare(const string &s) const;//与字符串s比较
int compare(const char *s) const;//与字符串s比较
*/
void test05()
{
	string s1 = "dsfsf";
	string s2 = "dsfsfd";

	//s1和s2比较:
	if (s1.compare(s2) == 0) {
		cout << "s1和s2相等" << endl;
	}
	else if (s1.compare(s2) == 1) {
		cout << "s1大于s2" << endl;
	}
	else {
		cout << "s1小于s2" << endl;
	}
}

/*
string子串
string substr(int pos = 0, int n = npos) const;//返回由pos开始的n个字符组成的字符串
*/
void test06()
{
	string s1 = "dfsf";
	string s2 = s1.substr(1, 3);
	cout << s2 << endl;
}

//需求:有一个邮箱,得到其用户名:
//例如:[email protected]
void test07()
{
	string s1="[email protected]";
	int pos = s1.find('@');
	string s2 = s1.substr(0, pos);
	cout << s2 << endl;
}

/*
string插入和删除操作
string& insert(int pos, const char* s); //插入字符串
string& insert(int pos, const string& str); //插入字符串
string& insert(int pos, int n, char c);//在指定位置插入n个字符c
string& erase(int pos, int n = npos);//删除从Pos开始的n个字符
*/
void test08()
{
	//插入:
	string s1 = "hello";
	s1.insert(1, "1234");
	cout << s1 << endl;

	//删除:
	s1.erase(1, 4);
	cout << s1 << endl;
}

/*
string和c-style字符串转换
//string 转 char*
string str = "itcast";
const char* cstr = str.c_str();
//char* 转 string
char* s = "itcast";
string str(s);
*/

void func(string s)
{
	cout << s << endl;
}
void func2(char* p)
{
	cout << p << endl;
}
void test09()
{
	string s = "abc";
	//string->const char*;
	func(s);//char*和string可以隐式类型转换;
	//func2(s);//string不能隐式类型转换为char*;

	const char *p = s.c_str();
	//还可以通过const cast去掉const;
	func(p);//char*和string可以隐式类型转换;

	//const char*->string;
	string s2(p);
	//实用性比较高;
}

/*
为了修改string字符串的内容,下标操作符[]和at都会返回字符的引用。但当字符串的内存被重新分配之后,可能发生错误.
*/
void test10()
{

	string s = "abcdefg";
	char& a = s[2];
	char& b = s[3];

	a = '1';
	b = '2';

	cout << s << endl;
	cout << (int*)s.c_str() << endl;

	s = "pppppppppppppppppppppppp";

	//a = '1';
	//b = '2';

	cout << s << endl;
	cout << (int*)s.c_str() << endl;
}

int main()
{

	test01();
	test02();
	test03();
	test04();
	test05();
	test06();
	test07();
	test08();
	test09();
	test10();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/XUCHEN1230/article/details/86502724
今日推荐