前端开发入门到精通的在线学习网站

网站首页 > 资源文章 正文

string 基本用法

qiguaw 2024-11-18 15:57:01 资源文章 17 ℃ 0 评论

stl基本知识

#include<iostream>
using namespace std;
#include"vector"
#include"algorithm"
class Teacher
{
public:
	int  age;
	char name[64];
public:
	void printT()
	{
		cout << "age:" << age << endl;
	}


};
void main11()

{
	Teacher t1, t2, t3;
	t1.age = 31;
	t2.age = 32;
	t3.age = 33;
	Teacher *p1, *p2, *p3;
	p1 = &t1;
	p2 = &t2;
	p3 = &t3;
	vector<Teacher*> v1;//把你的元素拷贝到容器中
	v1.push_back(p1);
	v1.push_back(p2);//容器实现了 数据类型和算法的有效分离
	v1.push_back(p3);//添加的时候已经复制到容器里面了

	//迭代器相当于一个指针
	for (vector<Teacher*>::iterator it = v1.begin(); it != v1.end(); it++)//迭代器
	{ 
		cout << (*it)->age <<endl;
	}

		int num1=	count(v1.begin(), v1.end(), 3);
		cout << "num1:" << num1 << endl;
}

void main()
{
	main11();
	cout << "hello" << endl;
	system("pause");
	return;
}

string 和char*比较

1)string是一个类,char*是一个指向字符的指针

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

2)string不用考虑内存释放和越界

string管理char*所分配的内存,每一次string的复制,取值都由string 类复制维护,所以不用担心复制越界和取值越界

3)string提供了一系列的字符串操作函数

void main11()
{
	string s1 = "aaaa";
	string s2("bbbb");
	string s3 = s2;//通过拷贝构造函数 来初始化对象
	string s4(10, 'a');
	cout << "s1:" << s1 << endl;
	cout << "s2:" << s2 << endl;
	cout << "s3:" << s3 << endl;
	cout << "s4:" << s4 << endl;
}
//字符串的遍历
void main22()
{
	
	string s1 = "asdgfgh";
	//重载了数组操作符 可以用数组方式遍历
	for (int i = 0; i < s1.length(); i++)
	{
		cout << s1[i] << " ";
	}
	//迭代器
	for (string::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//
	for (int i = 0; i < s1.length; i++)
	{
		cout << s1.at(i) << " ";//抛出异常
	}
}
//字符指针和string 的转换
void main23()
{
	string s1 = "aaabbbbbb";
	printf("s1:%s\n", s1.c_str());//s1=>char *

	char buf1[128];
	s1.copy(buf1, 3, 0);//s1=>buf
	cout << "buf1:" << buf1 << endl;
}
//字符串的连接
void main24()
{
	string s1 = "aaaa";
	string s2 = "bbbb";
	s1 = s1 + s2;
	cout << s1 << endl;
	string s3 = "333";
	string s4 = "444";
	s3.append(s4);
	cout << "s3:" << s3 << endl;
}
void main25()//查找和替换
{
	string s1 = "scd hello scd 222 111 scd";
	// 第一次出现scd的下标
	int index = s1.find("scd", 0);
	cout << "index:" << index << endl;

	//求scd出现的次数 第一次出现的数组下标
	 int  offindex=s1.find("scd", 0);
	 while (offindex != string::npos)
	 {
		 cout << "offindex:" << offindex << endl;
		 offindex = offindex + 1;
		 offindex= s1.find("scd", offindex);
	 }
	 string s3 = "aaa  bbb ccc";
	 s3.replace(0, 3, "AAA"); 
	 cout << "s3" << s3 << endl;
	  offindex = s1.find("scd", 0);
	 while (offindex != string::npos)
	 {
		 cout << "offindex:" << offindex << endl;
		 offindex = offindex + 1;
		 offindex = s1.find("scd", offindex);
	 }
	 cout << "s3" << s3 << endl; 
}
void main26()//区间删除和插入
{
	string s1 = "hello1 hello2 hello1";
	string::iterator it = find(s1.begin(), s1.end(), '1');
	if (it != s1.end())
	{
		s1.erase(it);
	}
	cout << "s1删除后的结果" <<s1<< endl;
	s1.erase(s1.begin(), s1.end());
	cout << "s1全部删除" << s1 << endl;
	cout << "s1长度" << s1.length() << endl;

	string s2 = "BBB";
	s2.insert(0, "AAA");//头插
  s2.insert(s2.length(),"CCC");//尾插
	cout << s2 << endl;
}
void main27()//算法基本用法
{
	string s1 = "AAAbbb";
	//函数入口地址 函数对象 预定义的函数对象
	transform(s1.begin(), s1.end(), s1.begin(),toupper);
	cout << s1 << endl; 
	string s2 = "AAAbbb";
	transform(s1.begin(), s1.end(), s1.begin(), tolower);
	cout << "s2:" << s2 << endl;
}

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表