C++之STL

2022/8/31 14:54:40

本文主要是介绍C++之STL,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1 STL概论

STL(标准模板库):
STL的分类:容器,算法和迭代器。
STL提供了6大组件:容器,算法和迭代器,仿函数、适配器(配接器)、空间配置器。

2 三大组件的初识

容器:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include <vector>

// 容器 vector
// 迭代器 遍历的功能
// 普通的指针 也是一种迭代器
void test01() {
	int array[5] = {1,3,5,6,8};
	int *p = array; // 指向array[0]
	for (int i = 0; i < 5;i++) {
		//cout << array[i] << " ";
		cout << *(p++) << " ";
	}
}

void test02() {
   // 声明一个容器
	vector<int> v; // 声明一个容器  这个容器中存放int类型数据  对象名称
	// 向容器中添加数据
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);

	// 遍历容器中的数据
	// 利用迭代器

	// 声明迭代器
	vector<int>::iterator itBegin = v.begin(); // itBegin指向的是v容器中的起始位置
	vector<int>::iterator itEnd = v.end(); // itEnd指向的是v容器中最后一个位置的下一个地址

	while (itBegin != itEnd) {
		cout << *itBegin << " ";

		itBegin++;
	}
}

int main()
{
	test02();
	system("pause");
	return EXIT_SUCCESS;
}
总结: 普通的指针也是一种迭代器 C++容器类 vector ,vector 自带的迭代器vector <T> ::iterator

四种遍历方法:
第1种:直接使用while循环

// 声明迭代器
	vector<int>::iterator itBegin = v.begin(); // itBegin指向的是v容器中的起始位置
	vector<int>::iterator itEnd = v.end(); // itEnd指向的是v容器中最后一个位置的下一个地址

	// 第1种遍历方式
	/*while (itBegin != itEnd) {
		cout << *itBegin << " ";

		itBegin++;
	}*/

第2种:使用for循环

for (vector<int>::iterator start = v.begin(); start != v.end(); start++) {
		cout << *start << " ";
	}

第3种:使用foreach循环

for ( int a: v) {
		cout << a << " ";
	}

第4种: 使用algorithm头文件中的for_each函数

for_each(v.begin(),v.end(),myPrint);
总结: 迭代器可以看作指针。
vector对象的begin()函数返回的值就是指向vector容器第一个元素的指针。

3 string容器

3.1 构造和赋值

// 1. string 构造函数
	string str;
	string str2(str); // 拷贝构造
	string str3 = str2;

	string str4("abc"); // 使用n个字符串初始化
	string str5(10, 'a'); // 用10个字符a来初始化

	cout << str4 << endl;
	cout << str5 << endl;

	// 2. string 基本的赋值
	str = "Hello";
	cout << str << endl;
	str2 = str4; // 拷贝赋值
	str = 'a';
	cout << str << endl;
	 
	str3.assign("fyeuryue",5); //将字符串的前5个数放到str3中
	cout << str3 << endl;
	str3.assign("fyeuryue", 1,5); // 将字符串的第1个字符到第5个字符拿出来赋值给str3
	cout << str3 << endl; 

3.2 存储

从string容器中取出元素的两种方法:
1. 使用方括号 [idnex]
2. 使用at(index)函数

3.3 字符串拼接

直接使用”+”运算符,或者调用append()函数

string s1 = "hello";
string s2;
s2 += " world";  // 字符串拼接

s1.append("lofly"); // 字符串拼接
cout << s1 + s2 << endl;

3.4 字符串查找

调用find()
	int position = s1.find('o', 0); 
	cout << position << endl; // 输出5  找不到返回-1
	position = s1.rfind('o');
	cout << position << endl; // 输出5  找不到返回-1

3.5 字符串替换

// 字符替换
s1.replace(0, 3, "llh"); // 从0开始到3位置结束的字符 替换为llh
cout << s1 << endl; //

3.6 字符串比较

// 字符串比较
	string t1 = "abc";
	string t2 = "abc";

	if (t1.compare(t2) == 0) {
		cout << "字符串相等" << endl;
	}
	else if(t1.compare(t2) == 1){
		cout << "t1大于t2" << endl;
	}
	else {
		cout << "t1小于t2" << endl;
	}

3.7 string子串

	// 子串
	string ss1 = "csadhfu";
	string ss2 = ss1.substr(2,5);
	cout << ss2 << endl;

3.8 string 的插入和删除

// 插入和删除
	string str1 = "hello";
	str1.insert(1,"SB");
	cout << str1 << endl;

	str1.erase(1,2); // 从位置1开始的两个字符会被删除
	cout << str1 << endl;

3.9 string 和 c-style类型转换

//string 和 c-style字符串转换
string s1 = "abc";
	const char *cstr = s1.c_str(); 

	cout << cstr << endl;
	printf("%s \n", cstr);

	string s2(cstr);
	cout << s2 << endl;

3.10 string赋值重新分配内存问题

string s = "abcdefghi";
	char &a = s[1];
	char &b = s[2];
	a = 'm';
	b = 'n';
	cout << s << endl;
	cout << "字符串的地址" << (int*)s.c_str() << endl;

	s = "ppppp";// 原始开辟的内存够用, 不重新分配内存
	s = "pppppppppppppppppppppppppppp"; // 原始开辟的内存不够用,重新分配内存

	cout << s << endl;
	cout << "字符串的地址" << (int*)s.c_str() << endl;

// 将所有的字母转成大写
	string s = "abRdefg";
	for (int i = 0; i < s.size();i++) {
		s[i] = toupper(s[i]);
	}

	cout << s << endl;

转小写使用函数 tolower()



这篇关于C++之STL的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程