简述C++ string的用法

7个部分
初始化、访问、拼接、查找、截短、反转、大小写转换

#include<string>
#include<iostream>
using namespace std;

int main(){
	const char* a = "Hello World!";// 常量字符串 
	cout << a << endl;
	
	// 一 
	//sting的初始化 
	//复制 
	string b (a); // string b = a;
	cout << b << endl;
	
	//复制 前五个 
	string c ( a, 5);
	cout << c << endl;
	
	//赋值为 10个 'H' 
	string d ( 10, 'H');
	cout << d << endl;
	
	//赋值
	string e ("Hello World!");
	cout << e << endl; 
	
	// 二 
	//string的访问方法
	cout << "二 string的访问方法" << endl;
	
	//1 下标法 
	cout << "下标法" << endl;
	for(int i = 0; i < e.length(); i++){
		cout << e[i];
	} 
	cout << endl;
	
	//2 迭代器
	cout << "it 迭代器" << endl;
	for(auto it = e.begin(); it != e.end(); it++){
		cout << *it;
	}
	cout << endl;
	
	//3 c_str()
	cout << "c_str()" << endl;
	cout << e.c_str() << endl;
	
	// 三 
	//拼接字符串
	cout << "三 拼接字符串" << endl;
	
	// 1 +=
	cout << "1 +=" << endl;
	string m ("Hello ");
	string n ("World!");
	m += n;
	cout << m << endl;
	
	// 2 append()
	cout << "2 append()" << endl;
	string o ("Hello ");
	o.append( n);
	cout << o << endl;
	
	string s("I am an apple.");
}

在这里插入图片描述

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	//四 
	
	//在string 中查找字符或 字符串 
	// string 类的 成员函数 find 
	string a ("I am an apple.");
	cout << a << endl;
	
	//1 查找 字符串 (找不到 将返回 -1 string::npos) 
	cout << "查找 \"an\" " << endl;
	int pos = a.find( "an", 0);
	cout << "位置是" << pos << endl;
	
	cout << endl;
	
	//2 查找 字符 'a' 并 输出其全部位置 
	cout << "查找 字符'a'" << endl;
	int i = a.find( 'a', 0);
	while( i != string::npos){
		cout << " 'a' 的位置有 " << i << endl;
		i = a.find( 'a', i + 1); 
	}
	cout << endl;
	
	//五 
	//string 的 截短
	//erase 函数
	//1 clear() 清空字符串 
	cout << "clear() 清空字符串" << endl;
	a.clear();
	cout << a << endl; 
	
	//2 erase( , ) 删除指定范围内的内容 
	a = "I am an apple.";
	cout << a << endl;
	
	cout << "erase( 5, 14) 删除指定范围内的内容" << endl;
	a.erase( 5, 14);
	cout << a << endl;
	
	//3 erase() 删除迭代器所指的字符 
	a = "I am an apple.";
	
	//用迭代器找出想删除的字符 
	auto it = find( a.begin(), a.end(), 'a');
	a.erase( it);
	cout << "erase()删除迭代器所指的字符'a'" << endl;
	cout << a;
}

在这里插入图片描述

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	//六
	//字符串的反转 
	string a("Hello World!");
	cout << a << endl;
	cout << endl;
	
	// reverse( 迭代器1, 迭代器2) 反转两个迭代器之间的内容 
	reverse( a.begin(), a.end());
	cout << "reverse( a.begin(), a.end())反转两个迭代器之间的内容" << endl;
	cout << a << endl;
}

在这里插入图片描述

#include<string>
#include<iostream>
#include<algorithm>
using namespace std;

int main(){
	//七
	//字符串的大小写转换 
	string a ("Welcome to China!");
	cout << a << endl << endl;
	
	//transform( 迭代器1, 迭代器2, 迭代器3, 函数) 
	//迭代器 1 2 指定范围  
	//迭代器 3 来存放结果 
	// 大写
	transform( a.begin(), a.end(), a.begin(), ::toupper);
	cout << "transform() 大写" << endl; 
	cout << a << endl << endl;
	
	// 小写 
	transform( a.begin(), a.end(), a.begin(), ::tolower);
	cout << "transform() 小写" << endl; 
	cout << a << endl << endl;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值