4 c++编程-提高篇-STL简介

科技 网编 2023-09-24 13:03 120 0

重新系统学习c++语言,并将学习过程中的知识在这里抄录、总结、沉淀。同时希望对刷到的朋友有所帮助,一起加油哦!

生命就像一朵花,要拼尽全力绽放!死磕自个儿,身心愉悦!

写在前面,本篇章主要简单介绍STL概念和引入,后续将分章节详细介绍。

1.1 STL的产生
  • 合理有效提高代码的复用性,是软件开发人员的追求。
  • c++的面向对象和泛型编程思想,目的就是提高代码的复用性。
  • 为了建立数据结构和算法的一套标准,产生了STL。
1.2 STL基本概念
  • STL(Standard Template Library 标准模版库)
  • STL从广义上看包含:容器(container)算法(algorithm)迭代器(iterator)
  • 容器和算法之间通过迭代器连接
  • STL几乎所有的代码都采用了模版类或者模版函数。
1.3 STL六大组件

4 c++编程-提高篇-STL简介

​编辑

1.4 容器算法迭代器介绍

了解概念后,先来利用代码感受下STL的魅力。

STL中最常用的容器为vector,可以理解为数组,下面先看下如何向vector中插入数据、遍历数据。

我们来实现一下vector存放内置数据类型,并遍历输出

容器: vector

算法:for_each

迭代器:vector<int>::iterator

4 c++编程-提高篇-STL简介

编辑

示例:


#include<iostream>
#include<string>
#include<vector>
#include<algorithm> // 使用算法 for_each时需引入 using namespace std; void myPrint(int val) {
cout << val << endl;
} void test() {
// 创建vector容器 数组
vector<int> v; // 插入数据
v.push_back(1);
v.push_back(2);
v.push_back(3); // 通过迭代器访问容器中的数据 //vector<int>::iterator it = v.begin(); // 起始迭代器,指向容器中的第一个元素
//vector<int>::iterator end = v.end(); // 结束迭代器,指向容器中最后一个元素的下一位
// 遍历1
//while (it != end) {
// cout << *it << endl; // it 看成指针,通过*it解引用,输出元素值
// it++;
//} // 遍历2 更简单的for循环输出
//for (vector<int>::iterator it = v.begin(); it < v.end(); it++) {
// cout << *it << endl;
//} // 遍历3 使用算法
// 使用STL提供标准遍历算法 头文件 algorithm
//for_each(v.begin(),v.end(), myPrint); // 遍历4 更简单
// c++11/14新特性 新的for循环——for(x:range)
// for (auto item: v) 可以用auto自动类型推导.若给定数据类型下面写法
for (int item: v) {
cout << item << endl;
}
} int main() {
test(); system("pause");
return 0;
}

1.4.2vector存放自定义数据类型

实现一下vector存放自定义数据类型,并遍历输出

vetor内可存放自定义数据类型对象或对象指针。

示例:


#include <iostream>
#include <string>
#include<vector> using namespace std;
class Person {
public:
Person(string name, int age) {
this->m_name = name;
this->m_age = age;
} string m_name;
int m_age;
}; // vector存放对象
void test() {
vector<Person> v;
Person p1("a1", 10);
Person p2("a2", 20);
Person p3("a3", 30);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3); for (vector<Person>::iterator it = v.begin(); it < v.end(); it++) {
// 通过*it解引用拿到Person变量
//cout << "name:" << (*it).m_name << ",age:" << (*it).m_age << endl;
// it是指针,通过->来指定元素拿到变量
cout << "name:" << it->m_name << ",age:" << it->m_age << endl;
}
} // vector存放对象指针
void test2() {
vector<Person*> v;
Person p1("a1", 10);
Person p2("a2", 20);
Person p3("a3", 30);
v.push_back(&p1);
v.push_back(&p2);
v.push_back(&p3); for (vector<Person*>::iterator it = v.begin(); it < v.end(); it++) {
// 通过*it解引用拿到Person*,是对象指针
cout << "::name:" << (*it)->m_name << ",age:" << (*it)->m_age << endl; // 还可以通过*it解引用拿到Person*,然后**t解引用,来拿到Person对象
//cout << "name:" << (**it).m_name << ",age:" << (**it).m_age << endl;
}
}
int main() {
//test();
test2();
system("pause");
return 0;
}

1.4.3 vector容器嵌套容器

示例:


#include <iostream>
#include <string>
#include<vector> using namespace std; void test() {
// 定义一个容器嵌套容器
vector<vector<int>> v;
vector<int> v1;
vector<int> v2;
vector<int> v3; for (int i = 1; i < 4; i++) {
v1.push_back(i);
v2.push_back(i+1);
v3.push_back(i+2);
} // 将容器元素插入到容器
v.push_back(v1);
v.push_back(v2);
v.push_back(v3); // 遍历1
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;
} // 遍历2
/*for (vector<int> item : v) {
for (int o : item) {
cout << o << " ";
}
cout << endl;
}*/
} int main() {
test(); system("pause");
return 0;
}

总结

以上是真正的电脑专家为你收集整理的4 c++编程-提高篇-STL简介的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得真正的电脑专家网站内容还不错,欢迎将真正的电脑专家推荐给好友。

评论区