量化交易之C++篇 - STL三大组件

#include <iostream>
#include <string>

using namespace std;

#include <vector> // 容器 vector

#include <algorithm> // 使用系统算法的头文件


// 迭代器 遍历功能 用指针理解;
// 普通指针也算一种迭代器;
void test01() {
	int array[5] = { 1, 3, 5, 6, 8 };

	int* p = array; // 指针指向数组的首地址;

	for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
		cout << array[i] << endl;
	}

	for (int i = 0; i < sizeof(array) / sizeof(array[0]); i++) {
		cout << *(p++) << endl;
	}
}

void myPrint(int value) {
	cout << value << endl;
}

void test02() {

	vector<int> v; // 声明一个容器, 这个容器中存放int 类型数据
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);

	// 遍历容器中的数据;
	// 第一种遍历方法: 利用迭代器;
	vector<int>::iterator itBegin = v.begin(); // itBegin 指向容器的起始位置;
	vector<int>::iterator itEnd = v.end(); // itEnd 指向容器的最后一个位置的下一个地址;
	while (itBegin != itEnd)
	{
		cout << *itBegin << endl;
		itBegin++;
	}

	// 第二种遍历方法
	for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
		cout << *it << endl;
	}

	// 第三种方式 利用算法
	for_each(v.begin(), v.end(), myPrint);
}

class Person {

public:
	Person(string name, int age) {
		this->name = name;
		this->age = age;
	}

	void showInfo() {
		cout << "name: " << this->name << "   age: " << this->age << endl;
	}

private:
	string name;
	int age;
};

// 利用迭代器遍历
void test03() {
	vector<Person> v;
	Person person1("roger", 19);
	Person person2("rei ri", 20);
	Person person3("o denn", 25);

	v.push_back(person1);
	v.push_back(person2);
	v.push_back(person3);

	for (vector<Person>::iterator it = v.begin(); it != v.end(); it++) {
		(*it).showInfo();
	}
}

// 存放自定义类型的指针
void test04() {
	vector<Person*> v;
	Person person1("roger", 19);
	Person person2("rei ri", 20);
	Person person3("o denn", 25);

	v.push_back(&person1);
	v.push_back(&person2);
	v.push_back(&person3);

	for (vector<Person*>::iterator it = v.begin(); it != v.end(); it++) {
		(*it)->showInfo();
	}
}


// 容器嵌套容器
void test05() {
	vector<vector<int>> v;

	vector<int>v1;
	vector<int>v2;
	vector<int>v3;
	
	// 填装小容器
	for (int i = 0; i < 5; 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++) {
		for (vector<int>::iterator vit = (*it).begin(); vit != (*it).end(); vit++) {
			cout << *vit << "  ";
		}
		cout << endl;
	}
}


int main() {

	//test01();

	//test02();

	//test03();

	//test04();

	test05();

	return EXIT_SUCCESS;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL是指标准模板库(Standard Template Library),它是C++语言的一部分,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的目标是提供高效、可重用和可扩展的组件,以便开发人员能够更轻松地编写高质量的代码。STL包含了许多常见的数据结构,如vector、list、set、map等,以及各种算法,比如排序、查找、遍历等。通过使用STL,开发人员可以更加高效地处理各种数据结构和算法的问题,提高代码的开发效率和质量。 在STL中,我们可以使用各种容器来存储和管理数据。例如,我们可以使用std::map来创建一个键值对的映射,其中每个键都有一个与之相关联的值。下面是一个示例代码,展示了如何创建和使用一个std::map对象: std::map<std::string, int> disMap() { std::map<std::string, int> tempMap{ {"C语言教程",10},{"STL教程",20} }; return tempMap; } std::map<std::string, int> newMap(disMap()); 在这个示例中,disMap()函数创建了一个临时的std::map对象,并初始化了其中的一些键值对。然后,使用移动构造函数将这个临时对象移动到了一个新的std::map对象newMap中。最终,我们可以通过newMap对象来访问和操作这些键值对。 综上所述,STLC++中的标准模板库,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的使用可以提高代码的开发效率和质量,并且通过各种容器和算法,可以方便地处理各种数据结构和算法的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL详解超全总结(快速入门STL)](https://blog.csdn.net/qq_50285142/article/details/114026148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C++实验】阅读STL源码并分析](https://blog.csdn.net/qq_35760825/article/details/125311509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值