【string类常见接口】

发布于:2025-08-02 ⋅ 阅读:(10) ⋅ 点赞:(0)

1、constructor

alt

 构造函数有7个,常用有3个分别为:
string();这个是没有参数的默认构造,也就是自动会调用这个构造函数。
string(const string& str);拷贝构造
string(const char* s);常用构造函数,写字符串即可。

 string(const string& str,size_t pos,size_t len = npos);这个拷贝构造函数,pos为下标的位置,len为长度。
string(const char* s,size_t n);构造字符串几个字符。
string(size_t n,char c);
string(InputIterator first,InputIterator last )

void string_test1()
{
	//construct test
	//string()  调用默认构造(无参,缺省参数,编译器自动形成的构造)
	string s1;
	//<<这个不能输出自定义类型
	//这儿是操作符重载了,并且要写成全局
	//然后定义成类的friend,就可以访问私有
	cout << s1 << endl;
	//string(const char*s)
	string s2("hello world");
	cout << s2 << endl;
	//string(const string& str)(拷贝构造)
	string s3 = s2;
	cout << s3 << endl;
	//string(const string& str,size_t pos,size_t len = npos)
	string s4(s2, 1);
	cout << s4 << endl;
	//string(const char*s ,size_t n)
	string s5("hello bit",8);
	cout << s5 << endl;
	//string(size_t n,char c)
	string s6(4, 'x');
	cout << s6<< endl;
	//string(InputIterator first,InputIterator last)
	string s7(s2.begin() + 1, s2.end() - 1);
	cout << s7 << endl;
}
//const string& s
//引用传参,是参数的别名
//理解为s和参数里的数据是同一块空间
void push_back(const string& s)
{

}
void string_test2()
{
	//隐式类型转换+拷贝构造
	string s1 = "hello world";
	const string& s2 = "hello world";
	//
	push_back(s2);
	push_back("hello world");
	push_back(s1);
}

2、[ ]

void string_test3()
{
	//用[]访问string
	string s1("hello world");
	//1.char& operator[](size_t pos)
	//2.const char& operator(size_t pos) const
	s1[0] = 'x';
	//size_t size() const
	// return the length of the string,in terms of bytes
	//被const修饰 size_t size(string* const this) const
	for (size_t i = 0; i < s1.size();i++)
	{
		cout << ++s1[i] << " ";
	}
	cout << endl;
	const string s2("hello world");
	//s2[0] = 'y';
}

 []运算符重载提供了两个接口,一个可读可写,一个只可读。
char& operator[](size_t pos) //可读可写
const char& operator(size_t pos) const;只能读,*this被const修饰,内容不可变,接受的返回值必须是常量char&,也不可变。

3、迭代器遍历

void string_test4()
{
	//<<符号重载
	//ostream& operator<<(ostream& os,const string& str)
	//os->cout
	string s1("hello world");
	//iterator begin()
	//const_iterator begin()const
	//Return iterator to beginning
	//Returns an iterator pointing to the first character of the string
	string::iterator it1 = s1.begin();
	//iterator end()
	//const_iterator end() const
	while (it1 != s1.end())
	{
		cout << *it1 << " ";
		it1++;
	}
	cout << endl;
	//reverse 颠倒
	//reverse_iterator rbegin()
	//const_reverse_iterator rbegin()  const
	//reverse_iterator rend()
	//const_reverse_iterator rend() const
	string::reverse_iterator  rit1 = s1.rbegin();
	while (rit1 != s1.rend())
	{
		cout << *rit1 << " ";
		rit1++;
	}
	cout << endl;
}

 迭代器有两个,一个正向迭代器,一个反向迭代器

4、范围for遍历

void string_test5()
{
	string s1("hello world");
	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;
	for (auto& e : s1)
	{
		e += 1;
	}
	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;
}

5、sort排序

alt
 string类中按字符ASCII码排序,传迭代器

void string_test6()
{
	//按字典序排序
	string s1("hello world");
	cout << s1 << endl;
	sort(s1.begin(), s1.end());
	cout << s1 << endl;
}

6、push_back 、 append、 +=

void string_test7()
{
	string s1("hello world");
	cout << s1 << endl;
	//push_back尾插
	//void push_back(char c)
	//append 附加
	//Appends character c to the end of the string
	//increasing its length by one
	s1.push_back('x');
	cout << s1 << endl;
	s1.append("yyyyyyyyyyyy");
	cout << s1 << endl;
	//符号重载+=
	//1.string& operator+=(const string& str)
	//2.string& operator+=(const char* s)
	//3.string& operator+=(char c)
	//"+="接口把能想到的全部设计进去了
	string s2("MMMNNN");
	s1 += 'y';
	s1 += "PPPPQQQQ";
	s1 += s2;
	cout << s1 << endl;
}

alt

 pash_back尾插一个字符,append尾插一个字符串,功能很多。+=是运算符重载,接口很多,能想到的都设计进去了,比如+=string,+=字符,+=字符串

7、Insert,erase

void string_test8()
{
	string s1("hello world,");
	cout << s1 << endl;
	//assign :分派
	//assigns a new value to the string,replacing its
	//current contents
	s1.assign("xxxxxxxyyyyyyyyyy");
	cout << s1 << endl;
	//insert
	// 要挪动位置,慎用->O(N)
	//string& insert(size_t pos,const string& str)
	string s2(" hello bit!");
	s1.insert(s1.size(), s2);
	cout << s1 << endl;
	//string& insert(size_t pos,const char* s)
	s1.insert(0, "kkkkk ");
	cout << s1 << endl;
	//string& insert(size_t pos,size_t n,char)
	s1.insert(0, 1, 'F');
	cout << s1 << endl;
}

void string_test9()
{
	string s1("hello world");
	cout << s1 << endl;
	//erase
	//string& erase(size_t pos = 0,size_t len = npos)
	//iterator erase(iterator p)
	//iterator erase(iterator first,iterator last)
	s1.erase(s1.size()-1);
	cout << s1 << endl;
	//replace 替代
	//string& replace(size_t pos,size_t len,const string& str)
	//string& replace(size_t pos,size_t len,const char* s)
	//string& replace(size_t pos,size_t len,const char c)
	string s2("MMMMMNNNNN");
	s1.replace(0, 0, s2);
	cout << s1 << endl;
}

 insert设计的很灵活,第一个参数size_t pos,从哪个位置插入,第二个参数插入什么,字符串,string,字符。erase,第一个参数是从哪个位置删除,第二个参数,删除多少个字符。

8、reserve、resize、c_str

 reserve设置容量,resize设置大小,resize既影响容量,有影响大小。

void test_string17()
{
	string s1("11111");
	string s2("11111111111111111111111111111111111111");
	cout << s1.capacity() << endl;  //15
	//reserve保留
	//reverse 逆置,反转
	s1.reserve(100);  //设置容量
	//知道需要多少空间,提前开-->reverse
	//s1[100] = 'x'; 这样写不可以,因为[]会调用 char& operator[](size_t pos)
	cout << s1.capacity() << endl;  //111
}

void test_string18()
{
	string s1;
	//既设置size,又扩容了,string机制决定
	//resize设置存储字符的大小
	s1.resize(5, '0');
	s1[4] = '3';
	s1[3] = '4';
	s1[2] = '5';
	s1[1] = '6';
	s1[0] = '7';
	cout << s1 << endl;
	string s2("hello world");
	//尾插
	s2.resize(20, 'x');
	cout << s2 << endl;
	//删除
	//把字符删除了,不能缩容
	s2.resize(5);
	string file("Test.cpp");
	//file.c_str()获取file字符串的地址
	FILE* fout = fopen(file.c_str(), "r");
	char ch = fgetc(fout);
	while (ch != EOF)
	{
		cout << ch;
		ch = fgetc(fout);
	}
}

9、find 、substr、string比较大小

void test_string19()
{
	//find找位置,substr找子串
	//size_t find(const string& str,size_t pos = 0)const
	//size_t find(char c,size_t pos = 0)const
	//return 
	//The position of the first character of the first match.
	//If no matches were found,the function returns string::npos(-1).
	string file("string.cpp.zip");
	size_t pos = file.rfind('.');
	//"Helloworld"  10个字符
	//substr获取子串
	//string substr(size_t pos = 0,size_t len = npos)const
	//return 
	//A string object with a substring of this object
	string suffix = file.substr(pos, file.size() - pos);
	cout << suffix << endl;
	cout << "************************************************************************" << endl<<endl;
	string url("https://gitee.com/ailiangshilove/cpp-class/blob/master/%E8%AF%BE%E4%BB%B6%E4%BB%A3%E7%A0%81/C++%E8%AF%BE%E4%BB%B6V6/string%E7%9A%84%E6%8E%A5%E5%8F%A3%E6%B5%8B%E8%AF%95%E5%8F%8A%E4%BD%BF%E7%94%A8/TestString.cpp");
	size_t pos1 = url.find(':'); //find找到:位置后,返回:的下标
	string url1 = url.substr(0, pos1 - 0);
	cout << url1 << endl;
	size_t pos2 = url.find('/', pos1 + 3);
	string url2 = url.substr(pos1+3, pos2-pos1-3); //右开-左闭
	cout << url2 << endl;
	string url3 = url.substr(pos2 + 1);
	cout << url3 << endl;
}

void string_test20()
{
	string s1 = "hello";
	string s2 = "world";

	string ret1 = s1 + s2;
	cout << ret1 << endl;
	string ret2 = s1 + "xxxxxxxx";
	cout << ret2 << endl;

	string ret3 = "xxxxxxxx" + s1;
	cout << ret3 << endl;
	//字典序比较
	cout << (s1 < s2) << endl;
}

alt
alt