STL(二)容器(Vector、String上)

Vector(代码运行环境:VS2017)

数据结构上看来很像一个数组,也被称为“单端数组”,但是和数组有一点不同之处。
它的空间是动态分布的,像是一个动态数组。
它动态扩展的方式不是在原有位置上增加而是再重新找一个更大的空间然后将原来元素复制到此处之后释放原有空间。
vector迭代器支持随机访问。
实际使用案例

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    
    
	vector<int>c;
	c.push_back(10);//容器中插入数据,尾插法
	c.push_back(20);
	c.push_back(30);
	for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
	{
    
    
		cout << *it << endl;
	}
	return 0;
}

在这里插入图片描述
方法二:
使用for_each遍历函数:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
void pringtf(int i)
{
    
    
	cout << i << endl;
}
int main()
{
    
    
	vector<int>c;
	c.push_back(10);//容器中插入数据,尾插法
	c.push_back(20);
	c.push_back(30);
	/*
	for (vector<int>::iterator it = c.begin(); it != c.end(); it++)
	{
		cout << *it << endl;
	}
	*/
	for_each(c.begin(), c.end(), pringtf);
	return 0;
}

for_each()底层实现如下图所示:

在这里插入图片描述
当vector里的数据是另一种类型数据:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
class Person
{
    
    
public:
	string name;
	int age;
	Person(string name, int age)
	{
    
    
		this->name = name;
		this->age = age;
	}
};
int main()
{
    
    
	vector<Person>v;
	Person p1("aaa",20);

	Person p2("bbb", 21);
	Person p3("ccc", 22);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		cout << "姓名:" << (*it).name << " ";
		cout << "年龄:" << (*it).age<< "  ";
	}
	return 0;
}

运行结果:
在这里插入图片描述
vector容器嵌套容器
类似于一个二维数组的操作。
见代码实现:

#include<iostream>
#include<vector>
#include<string>
using namespace std;
void test01()
{
    
    
	//创建小容器
	vector<vector<int>>v;
	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	//小容器添加数据
	for (int i = 0; i < 3; i++)
	{
    
    
		v1.push_back(i + 1);
		v2.push_back(i + 1);
		v3.push_back(i + 1);
	}
	//放入大容器
	v.push_back(v1);
	v.push_back(v2);
	v.push_back(v3);
	for (vector<vector<int>>::iterator it = v.begin(); it != v.end(); it++)
	{
    
    
		//注意看it,此时的it指的是<vector<int>>,类似的情况下it指的都是<>内的内容
		for (vector<int>::iterator it1 = (*it).begin(); it1 != (*it).end(); it1++)
		{
    
    
			cout << *it1<< "	";
		}
		cout << endl;
	}
}
int main()
{
    
    
	test01();
	return 0;
}

运行结果:
在这里插入图片描述

String(代码运行环境:VS2017)

本质:
string是c++风格的字符串,而string的本质上是一个类。
*string和char 的区别
1.char *是一个指针
2.string是一个类,它的内部封装了char *,管理这个字符串,是一个char *的容器。
特点:
string内部封装了很多成员方法比如查找、插入、替换等。
string管理char *所分配的内存,不必担心复制越界或取值越界等,由类内部进行负责。
string的构造函数
实际案例:

#include<iostream>
#include<string>
using namespace std;
void test02()
{
    
    
	string s1;
	const char* str = "hellow word";
	string s2(str);
	string s3(s2);
	string s4(5, 'b');
	cout << "s1=" << s1 << endl;
	cout << "s2=" << s2 << endl;
	cout << "s3=" << s3 << endl;
	cout << "s4=" << s4 << endl;
}
int main(void)
{
    
    
	test02();
}

运行结果:
在这里插入图片描述
string的赋值操作
在这里插入图片描述
实际案例:

#include<iostream>
#include<string>
using namespace std;
void test03()
{
    
    
	string str1;
	str1 = "hellow word";
	string str2,str3,str4,str5,str6,str7;
	str2 = str1;
	str3 = 'a';
	str4.assign("hellow c++");
	str5.assign("hellow c++", 5);
	str6.assign(str1);
	str7.assign(5,'c');
	cout << "str1=" << str1 << endl;
	cout << "str2=" << str2 << endl;
	cout << "str3=" << str3 << endl;
	cout << "str4=" << str4 << endl;
	cout << "str5=" << str5 << endl;
	cout << "str6=" << str6 << endl;
	cout << "str7=" << str7 << endl;

}
int main(void)
{
    
    
	test03();
}


运行结果:
在这里插入图片描述
string字符串的拼接
在这里插入图片描述
实际案例:

#include<iostream>
#include<string>
using namespace std;
void test04()
{
    
    
	string str3 = "I";
	string str2;
	str3.append("love");
	str3.append("game abcde", 4);
	str2.append(str3);
	str3.append(str2, 2, 5);
	cout << "str2=" << str2 << endl;
	cout << "str3=" << str3 << endl;
}
int main(void)
{
    
    
	test04();
}

运行结果:
在这里插入图片描述
string的查找和替换
在这里插入图片描述
字符串比较
在这里插入图片描述
实际案例:

#include<iostream>
#include<string>
using namespace std;
void test04()
{
    
    
	string str3 = "I";
	string str2;
	str3.append("love");
	str3.append("game abcde", 4);
	str2.append(str3);
	str3.append(str2, 2, 5);
	//cout << "str2=" << str2 << endl;
	//cout << "str3=" << str3 << endl;
	int m;
	m = str2.compare(str3);
	cout << m << endl;

}
int main(void)
{
    
    
	test04();
}

运行结果:
在这里插入图片描述
string字符串插入和删除
在这里插入图片描述获取想要的字符串
在这里插入图片描述

扫描二维码关注公众号,回复: 15100843 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_45866980/article/details/126209771