string类C++笔记

#include<string>头文件

1.定义初始化赋值输入输出对象 

#include <iostream>  
#include <string>    //getline(cin,str1)            用于string类 
using namespace std;
int main()
{
	//定义string对象 
    string str1; 
    //初始化string对象
	string str2="c++"; //复制初始化 
	string str3("c++"); //直接初始化 
	//string类的赋值引用
	str1="pascal"; 
	//输入输出直接cin、cout、getline(cin,str1,'.') 
	cout<<str1<<endl;
//	cin>>str1;
//	cout<<str1; 
    getline(cin,str1);
	cout<<str1<<endl;
}

2.string之间的比较大小

原则:找第一个不同的比较得结果,对大小写敏感,A<a大写变小写'A'+32='a'
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
	vector<string> words={"a","Aee", "b", "app", "appl", "ap", "apply", "apple"};
	sort(words.begin(),words.end());
	for(auto a:words) cout<<a<<" ";
	cout<<char('A'+32);
    return 0;
}
Aee a ap app appl apple apply b a

3.string对象的成员函数

3.1  str.size() str.length()

       str.empty()长度 为空 

include <iostream>  
#include <string>    //getline(cin,str1)            用于string类 
using namespace std;
int main()
{ 
    string str1; 
	string str2="c++";  
	string str3("c++"); 
	//长度 为空 
	cout<<str2.size()<<" "<<str2.length()<<" "<<str2.empty(); 
}        3                   3                  0
str2.empty()返回的是bool值

3.2  str.push_back()结尾char 

       str.append()结尾string  下标

string中的push_back()和append()函数都是向string的结尾插入
push_bach()只能插入单个字符char,而append可以插入string
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1 = "I am a string";
    string s2(s1);
    string s3(s1);
    string append_s = "123456789";
    char append_c = 'B';
    s1.push_back(append_c);
    s2.append(append_s);         尾部加上append_s
	s3.append(append_s,0,5);     尾部加上append_s从下标为0开始的5个字符 注意是string的下标
    cout<<s1<<endl<<s2<<endl<<s3;
}
I am a stringB
I am a string123456789
I am a string12345

3.3  str.erase()删除 下标

#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1, s2, s3;s1 = s2 = s3 = "1234567890";
    s2.erase(5);            注意是下标       从下标5开始把以后的都删除
    s3.erase(5, 3);                         从下标5开始删除3个
    cout<< s1 <<endl<< s2 <<endl<< s3;
}
运行结果:
1234567890
12345
1234590

3.4  str.substr()提取 下标

substr() 函数用于从 string 字符串中提取子字符串,它的原型为:
string substr (size_t pos = 0, size_t len = npos) const;
pos 为要提取的子字符串的起始下标,len 为要提取的子字符串的长度。
#include <iostream>
#include <string>
using namespace std;
int main(){
    string s1 = "first second third",s2;
    s2 = s1.substr(6, 6);      从下标6开始提取6个字符
    cout<< s1 <<s2 <<endl;
}
运行结果:
first second third
second

3.5  str.insert()插入 下标

#include <iostream>
#include <string>
using namespace std;
int main(){
    string str = "meihao";
    string sstr = str.insert(0,2,'a');
    cout<<sstr<<endl;   //aameihao

	string str = "meihao";
	string sstr = str.insert(0,"hello~");
	cout<<sstr<<endl;   //hello~meihao

	string str = "meihao";
	string sstr = str.insert(1,"hello~",3);
	cout<<sstr<<endl;  //mheleihao

	string str1 = "meihao";
	string str2 = "hello~";
	string sstr = str2.insert(6,str1,3,3);
	cout<<sstr<<endl;  //hello~hao
    return 0;
}

3.6  str.fine() 与 str.rfine()  下标

#include <iostream>
#include <string>
using namespace std;
int main(){                                     返回值是下标int
    string str = "mmnkhhmeihiojimeiljmeij";
    int find_res = str.find("ei",2);            从下标2开始向右找第一个ei的e的下标   
    int find_res = str.find("ei",8);            下标为7时还能找到第一个ei  
                                                下标为8时还能找到第二个ei 
    int rfind_res = str.rfind("ei",22);         从下标22开始向左找最右边的ei的e的下标
    cout<<find_res<<endl<<rfind_res;     7     20
}

3.7  str.replace()    有下标也有迭代器

replace() 函数用法详解

#include <iostream>
#include <string>
using namespace std;
int main()
{
	string line = "this@ is@ a test string!";     //从下标4开始的1位用后面代替 
	line = line.replace(4, 1, "123456",0,5);	                        //用下标不加0是默认0 
	line = line.replace(4, 1,3,'Q');    	                            //用n个char替换  
	char* char_P = "123456";
	line = line.replace(line.begin()+4, line.begin()+5,char_P);         //用迭代器画出范围,用char数组换能从头开始n位 
	line = line.replace(line.begin()+4, line.begin()+5,char_P,2);       //用迭代器画出范围,用char数组换 
	line = line.replace(line.begin()+4, line.begin()+5, "123456");      //用迭代器画出范围,用sting换
	line = line.replace(line.begin()+4, line.begin()+5, "123456",2);	//用迭代器画出范围,用sting换只能从头开始n位 
	line = line.replace(line.begin()+4, line.begin()+5, 2,'O');	        //用迭代器画出范围,用n个char替换 
	cout << line << endl;	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值