天天看点

初识STL之string容器

文章目录

    • 1.string基本概念
    • 2.string构造函数
    • 3.string赋值操作
    • 5.string查找跟替换
    • 6.string字符串比较
    • 7.string字符存取
    • 8.string字符的插入和删除
    • 9.string子串
    • 10.String字符串转换成整数函数

1.string基本概念

本质:

string是c++风格的字符串,而string本质上是一个类

string和char*区别:

char是一个指针

string是一个类,类内封装了char,管理这个字符串,是一个char*型的容器.

2.string构造函数

构造函数原型

string();              //创建一个空的字符串
string(const char*s);  //使用字符串s初始化

string(const string& str); //使用一个string对象初始化另一个string对象
string(int n,char c);  //使用n个字符c初始化
           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string s1; //默认构造
	
	const char* str = "hello world"; // string str = "hello world";
	
	string s2(str);  
	cout<<"s2= "<<s2<<endl;
	
	string s3(s2);
	cout<<"s3= "<<s3<<endl;
	
	string s4(10,'a');
	cout<<"s4= "<<s4<<endl;
}

int main()
{
	test1();
}
           

3.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赋给当前字符串 
           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1;
	str1 = "hello world";
	cout<<"str1 = "<<str1<<endl; 
	
	string str2;
	str2 = str1;
	cout<<"str2 = "<<str2<<endl;
	
	string str3;
	str3 = "a";
	cout<<"str3 = "<<str3<<endl;  
	
	string str4;
	str4.assign("hello c++");
	cout<<"str4 = "<<str4<<endl; 
	
	string str5;
	str5.assign("hello c++",5);
	//hello
	cout<<"str5 = "<<str5<<endl; 
	
	string str6;
	str6.assign(str4);
	cout<<"str6 = "<<str6<<endl; 
	
	string str7;
	str7.assign(10,'a');
	cout<<"str7 = "<<str7<<endl; 
	
}

int main()
{
	test1();
}
           

4.string字符串的拼接

拼接的函数原型:

string& operator+=(const char* str);
	string& operator+=(const char c);
	string& operator+=(const string &str);
	string& append(const char* s);  //把字符串s连结到当前字符串结尾 
	string& append(const char* s,int n);//把字符串s的前n个字符连结到当前字符串结尾 
	string& append(const string &s);
	//把字符串s从pos开始的n个字符连结到当前字符串结尾 
	string& append(const string &s,int pos,int n); 
	
           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "I";
	str1.append(" love ");
	cout<<"str1 = "<<str1<<endl;
	

	str1.append("game asdafa",4);
	cout<<"str1 = "<<str1<<endl;
	
	string str2 = " LOL DNF";
	str1.append(str2);
	cout<<"str2 = "<<str1<<endl;
	
	str1.append(str2,0,4);
	cout<<"str3 = "<<str1<<endl;
	
}

int main()
{
	test1();
}
           

5.string查找跟替换

查找跟替换的函数原型

//查找str第一次出现的位置,从pos开始查找 
	int find(const string& str,int pos = 0)const;  
	//查找s第一次出现的位置,从pos开始查找 
	int find(const char* s,int pos = 0)const;
	//从pos位置查找s的前n个字符第一次出现的位置 
	int find(const char* s,int pos = 0,int n)const;
	//从pos位置查找c的前n个字符第一次出现的位置
	int find(const char c,int pos = 0)const;
	//rfind和find用法一样,不同的是rfind是从后往前查找 
	int rfind(const string& str,int pos = 0)const;	
	int rfind(const char* s,int pos = 0)const;	
	int rfind(const char* s,int pos = 0,int n)const;
	int rfind(const char c,int pos = 0)const;
	
	//替换pos开始n个字符的字符串str 
	string& replace(int pos,int n,const string& str);
	//替换从pos开始的n个字符为字符串s 
	string& replace(int pos,int n,const char* s);
           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!" ;
	int pos = str1.find("l");
	int r_pos = str1.rfind("l"); 
	cout<<"pos = "<<pos<<" r_pos= "<<r_pos<<endl;
	
	str1.replace(2,2,"222");
	//he222o world!
	cout<<"str1 = "<<str1<<endl;

	
}

int main()
{
	test1();
}
           

6.string字符串比较

比较方式:

字符串比较是按字符ASCII码经行对比

=返回 0

>返回 1

<返回 -1

函数原型:

int compare(const string& s)const;//与字符串s比较 
	int compare(const char* s)const;
           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!";
	string str2 = "hello world!";
	
	if(str1.compare(str2)==0)  cout<<"str1=str2"<<endl;
	
	string str3 = "hello world!";
	string str4 = "xello world!";
	
	if(str3.compare(str4)<0)  cout<<"str1<str2"<<endl;
	
	string str5 = "hello world!";
	string str6 = "aello world!";
	
	if(str5.compare(str6)>0)  cout<<"str1>str2"<<endl;
	
}

int main()
{
	test1();
}
           

7.string字符存取

string单个字符存取有两个方式

char& operator[](int n);
	char& at(int n);
           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!";
	for(int i=0;i<str1.size();i++)
		cout<<str1[i]<<" ";
	cout<<endl;
	for(int i=0;i<str1.size();i++)
		cout<<str1.at(i)<<" ";

	
}

int main()
{
	test1();
}
           

8.string字符的插入和删除

函数原型

string& insert(int pos,const char* s);
	string& insert(int pos,const string& str);	
	string& insert(int pos,int n,char c); //从指定位置插入字符串 

	string& erase(int pos,int n = npos);//删除从pos开始的n个字符 

           
#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "hello world!";
	str1.insert(2,"222");
	cout<<"str1 = "<<str1<<endl;
	
	string str2 = "good";
	str1.insert(2,str2);
	cout<<"str1 = "<<str1<<endl;
	
	str1.insert(2,4,'a');
	cout<<"str1 = "<<str1<<endl;
	
	str1.erase(2,11);
	cout<<"str1 = "<<str1<<endl;
}

int main()
{
	test1();
}
           

9.string子串

函数原型

#include<iostream>
#include<cstring>

using namespace std;

void test1()
{
	string str1 = "abcdefg";
	string str2 = str1.substr(0,3);
	cout<<str2<<endl;
}

int main()
{
	test1();
}
           

10.String字符串转换成整数函数

string s1= "1234567";
	int a = stoi(s1);
	cout<<a<<endl;

           

继续阅读