当前位置: 首页 > news >正文

长沙公司网站设计报价目前较好的crm系统

长沙公司网站设计报价,目前较好的crm系统,wordpress小说采集,班级网站怎么做ppt模板1. STL容器 什么是STL容器,为什么使用它们。向量(vector):使用向量存储数据。列表(list):使用列表实现双向链表。映射(map):使用映射实现键值对存储。 什么…

1. STL容器

  • 什么是STL容器,为什么使用它们。
  • 向量(vector):使用向量存储数据。
  • 列表(list):使用列表实现双向链表。
  • 映射(map):使用映射实现键值对存储。

什么是STL容器?

STL容器是一种用于存储和管理数据的C++模板类,它们提供了多种数据结构,以满足不同的需求。STL容器分为序列容器(Sequence Containers)和关联容器(Associative Containers)两大类。序列容器类似于数组,关联容器则基于键值对存储数据。

向量(vector)

向量是序列容器中最常用的之一,它类似于动态数组。它具有自动扩展和收缩的能力,因此可以根据需要存储任意数量的元素。

#include <vector>
#include <iostream>int main() {std::vector<int> myVector;  // 创建一个整数向量// 向向量中添加元素myVector.push_back(1);myVector.push_back(2);myVector.push_back(3);// 遍历并输出向量中的元素for (int i : myVector) {std::cout << i << " ";}return 0;
}

列表(list)

列表是双向链表的实现,提供了高效的插入和删除操作。与向量不同,列表的元素在内存中不是连续存储的,因此适用于需要频繁插入和删除操作的情况。列表的简单示例:

#include <list>
#include <iostream>int main() {std::list<std::string> myList;  // 创建一个字符串列表// 向列表中添加元素myList.push_back("Hello");myList.push_back("World");myList.push_front("C++");// 遍历并输出列表中的元素for (const std::string& str : myList) {std::cout << str << " ";}return 0;
}

映射(map)

映射是关联容器,它将键与值关联起来,允许通过键快速查找对应的值。这在需要构建字典、查找表或关系映射时非常有用。以下是一个映射的简单示例:

#include <map>
#include <iostream>int main() {std::map<std::string, int> myMap;  // 创建一个字符串到整数的映射// 向映射中添加键值对myMap["apple"] = 5;myMap["banana"] = 3;myMap["cherry"] = 8;// 查找并输出映射中的值std::cout << "The number of bananas is: " << myMap["banana"] << std::endl;return 0;
}

STL容器的强大功能使它们成为C++开发中不可或缺的一部分。无论你的项目规模如何,STL都提供了各种工具,用于有效地管理和操作数据,让代码更加高效和可维护。

2. STL算法

  • 什么是STL算法,它们的作用。
  • 排序算法:介绍STL的排序算法。
  • 查找算法:介绍STL的查找算法。
  • 使用STL算法来处理容器中的数据。

什么是STL算法?

STL算法是一组通用的、可复用的算法,它们独立于特定的容器类型,可以用于处理各种数据结构。STL算法可以显著提高代码的可读性和可维护性,同时还能提供高效的性能。

排序算法

STL提供了多种排序算法,其中最常用的是std::sort()函数,它可以对容器中的元素进行升序排序。排序算法:

#include <algorithm>
#include <vector>
#include <iostream>int main() {std::vector<int> numbers = {5, 2, 8, 1, 9};// 使用std::sort()对向量进行升序排序std::sort(numbers.begin(), numbers.end());// 输出排序后的结果for (int num : numbers) {std::cout << num << " ";}return 0;
}

查找算法

STL还提供了多种查找算法,其中包括std::find()函数,它可以在容器中查找指定的元素。

#include <algorithm>
#include <vector>
#include <iostream>int main() {std::vector<int> numbers = {5, 2, 8, 1, 9};int target = 8;// 使用std::find()在向量中查找目标元素auto result = std::find(numbers.begin(), numbers.end(), target);// 检查查找结果if (result != numbers.end()) {std::cout << "Found " << target << " at position " << (result - numbers.begin()) << std::endl;} else {std::cout << target << " not found." << std::endl;}return 0;
}

使用STL算法处理容器中的数据

STL算法通常以迭代器作为参数,因此它们可以应用于各种容器,如向量、列表、映射等。这使得算法非常通用和灵活。你可以使用std::for_each()std::transform()等算法来遍历容器中的元素,执行自定义的操作。

3. 自定义模板

  • 什么是模板,为什么使用模板。
  • 函数模板:创建可以处理不同数据类型的通用函数。
  • 类模板:创建可以处理不同数据类型的通用类。

什么是模板?

模板是一种通用的代码蓝图,它允许你编写可以处理不同数据类型的代码。C++中有两种主要类型的模板:函数模板和类模板。

函数模板

函数模板允许你编写通用的函数,这些函数可以接受不同类型的参数。函数模板的定义以template关键字开始,后跟模板参数列表和函数声明。

简单的函数模板,用于交换两个值:

template <typename T>
void swapValues(T &a, T &b) {T temp = a;a = b;b = temp;
}int main() {int x = 5, y = 10;double a = 3.14, b = 2.71;swapValues(x, y);  // 交换整数swapValues(a, b);  // 交换双精度浮点数return 0;
}

函数模板的typename T部分表示模板参数,它可以是任何数据类型。

类模板

类模板允许你编写通用的类,这些类可以处理不同类型的成员变量和成员函数。类模板的定义以template关键字开始,后跟模板参数列表和类定义。

类模板,用于创建通用的栈数据结构:

template <typename T>
class Stack {
public:Stack() : top(-1) {}void push(T item) {if (top < MaxSize - 1) {data[++top] = item;}}T pop() {if (top >= 0) {return data[top--];} else {// 处理栈为空的情况throw std::runtime_error("Stack is empty");}}private:static const int MaxSize = 100;T data[MaxSize];int top;
};int main() {Stack<int> intStack;intStack.push(5);intStack.push(10);int x = intStack.pop();  // 弹出值为10的元素Stack<double> doubleStack;doubleStack.push(3.14);doubleStack.push(2.71);double y = doubleStack.pop();  // 弹出值为2.71的元素return 0;
}

类模板的typename T部分表示模板参数,可以是任何数据类型。

为什么使用模板?

使用模板可以使代码更加通用和灵活,无需为不同的数据类型编写重复的代码。模板还有助于提高代码的可维护性,只需编写一次通用代码,即可适用于多种数据类型。

4. 示例和练习

使用STL容器

使用STL容器的示例代码,演示向量(vector)的用法,包括添加元素、遍历容器和查找元素:

#include <iostream>
#include <vector>int main() {std::vector<int> numbers;  // 创建整数向量// 向向量添加元素numbers.push_back(1);numbers.push_back(2);numbers.push_back(3);// 遍历向量并输出元素for (int num : numbers) {std::cout << num << " ";}// 查找元素是否存在int target = 2;auto it = std::find(numbers.begin(), numbers.end(), target);if (it != numbers.end()) {std::cout << "\n" << target << " found at position " << std::distance(numbers.begin(), it);} else {std::cout << "\n" << target << " not found";}return 0;
}

使用STL算法

使用STL算法的示例代码,演示排序和查找算法的用法:

#include <iostream>
#include <vector>
#include <algorithm>int main() {std::vector<int> numbers = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};// 排序向量std::sort(numbers.begin(), numbers.end());// 输出排序后的向量for (int num : numbers) {std::cout << num << " ";}// 使用二分查找查找元素int target = 5;if (std::binary_search(numbers.begin(), numbers.end(), target)) {std::cout << "\n" << target << " found";} else {std::cout << "\n" << target << " not found";}return 0;
}

使用模板

使用函数模板的示例代码,如何创建一个通用的函数来比较两个值:

#include <iostream>template <typename T>
bool isEqual(T a, T b) {return a == b;
}int main() {int x = 5, y = 5;double a = 3.14, b = 2.71;if (isEqual(x, y)) {std::cout << "x and y are equal\n";} else {std::cout << "x and y are not equal\n";}if (isEqual(a, b)) {std::cout << "a and b are equal\n";} else {std::cout << "a and b are not equal\n";}return 0;
}

练习题

创建一个向量,存储一组学生的分数,并计算平均分。

#include <iostream>
#include <vector>int main() {std::vector<double> scores; // 创建一个双精度浮点数向量来存储学生的分数// 向向量添加学生分数scores.push_back(85.5);scores.push_back(92.0);scores.push_back(78.5);scores.push_back(88.0);scores.push_back(90.5);// 计算平均分double sum = 0.0;for (double score : scores) {sum += score;}double average = sum / scores.size();// 输出平均分std::cout << "平均分: " << average << std::endl;return 0;
}

运行结果:

在这里插入图片描述

解答:

  • 首先,包含 <iostream><vector> 头文件来使用C++的标准输入输出和向量容器。
  • 然后,创建一个std::vector<double>类型的向量,用于存储学生的分数。
  • 使用 push_back 函数将分数添加到向量中。
  • 通过迭代向量中的分数,计算它们的总和并除以向量的大小来获得平均分。
  • 最后,输出平均分。

使用STL的列表容器存储一组单词,并按字母顺序排序。

#include <iostream>
#include <list>
#include <algorithm>int main() {std::list<std::string> words; // 创建一个字符串列表来存储单词// 向列表添加单词words.push_back("apple");words.push_back("banana");words.push_back("cherry");words.push_back("date");words.push_back("fig");// 使用STL的排序算法对单词进行排序words.sort();// 遍历并输出排序后的单词for (const std::string& word : words) {std::cout << word << " ";}return 0;
}

运行结果:

在这里插入图片描述

解答:

  • 包含了 <iostream><list> 头文件,以便使用C++的标准输入输出和列表容器。
  • 然后,创建一个 std::list<std::string> 类型的列表,用于存储单词。
  • 使用 push_back 函数将单词添加到列表中。
  • 使用 sort 函数对列表中的单词进行字母顺序排序。
  • 最后,遍历并输出排序后的单词。

创建一个通用的函数模板,用于计算两个数的最大值。

#include <iostream>template <typename T>
T findMax(T a, T b) {return (a > b) ? a : b;
}int main() {int intMax = findMax(42, 17);double doubleMax = findMax(3.14, 2.71);std::cout << "最大整数值: " << intMax << std::endl;std::cout << "最大双精度浮点数值: " << doubleMax << std::endl;return 0;
}

运行结果:

在这里插入图片描述

解答:

  • 定义一个通用函数模板 findMax,它可以接受任何类型的参数 ab
  • 函数模板的返回类型也是参数的类型。
  • main 函数中,演示如何使用这个模板函数来查找整数和双精度浮点数的最大值。

创建一个类模板,表示二维坐标点,包括x和y坐标。

#include <iostream>template <typename T>
class Point {
public:Point(T x, T y) : x_(x), y_(y) {}void display() const {std::cout << "X坐标: " << x_ << ", Y坐标: " << y_ << std::endl;}private:T x_;T y_;
};int main() {Point<int> intPoint(2, 3);Point<double> doublePoint(1.5, 2.0);std::cout << "整数坐标点:" << std::endl;intPoint.display();std::cout << "双精度浮点数坐标点:" << std::endl;doublePoint.display();return 0;
}

运行结果:

在这里插入图片描述

解答:

  • 定义一个类模板 Point,它有两个模板参数 typename T,表示x和y坐标的类型。
  • 类模板有一个构造函数,可以接受两个参数,分别用于初始化x和y坐标。
  • 类模板还有一个成员函数 display,用于显示坐标点的值。
  • main 函数中,我们演示了如何创建不同类型的坐标点。

使用STL的映射容器存储学生的姓名和分数,然后按姓名查找分数。

#include <iostream>
#include <map>
#include <string>int main() {std::map<std::string, double> studentScores; // 创建一个映射容器,姓名映射到分数// 向映射容器添加学生和分数studentScores["Alice"] = 90.5;studentScores["Bob"] = 88.0;studentScores["Charlie"] = 92.5;studentScores["David"] = 85.0;// 按姓名查找分数std::string nameToFind = "Bob";if (studentScores.find(nameToFind) != studentScores.end()) {double score = studentScores[nameToFind];std::cout << nameToFind << " 的分数是 " << score << std::endl;} else {std::cout << "找不到学生:" << nameToFind << std::endl;}return 0;
}

运行结果:

在这里插入图片描述

解答:

  • 使用 std::map 容器来创建一个学生姓名到分数的映射。
  • 使用方括号操作符 [] 添加学生和分数的映射。
  • 使用 find 函数按姓名查找分数,如果找到,就输出分数;如果找不到,就输出找不到的消息。

文章转载自:
http://dinncogramary.wbqt.cn
http://dinncothallious.wbqt.cn
http://dinncoexopodite.wbqt.cn
http://dinncoorthophotograph.wbqt.cn
http://dinncoamg.wbqt.cn
http://dinncoheathenism.wbqt.cn
http://dinncophallic.wbqt.cn
http://dinncokrumhorn.wbqt.cn
http://dinncodiscommon.wbqt.cn
http://dinncoprocoagulant.wbqt.cn
http://dinncothromboendarterectomy.wbqt.cn
http://dinncothiol.wbqt.cn
http://dinncoplute.wbqt.cn
http://dinncotelekinesis.wbqt.cn
http://dinncoweigelia.wbqt.cn
http://dinncosabalo.wbqt.cn
http://dinncoamelioration.wbqt.cn
http://dinncoevangelize.wbqt.cn
http://dinncoelapse.wbqt.cn
http://dinncoanalecta.wbqt.cn
http://dinncomacroinvertebrate.wbqt.cn
http://dinncounderhand.wbqt.cn
http://dinncophenetole.wbqt.cn
http://dinnconoctule.wbqt.cn
http://dinncovellicative.wbqt.cn
http://dinncopob.wbqt.cn
http://dinncocasework.wbqt.cn
http://dinncopandal.wbqt.cn
http://dinncoconvergence.wbqt.cn
http://dinncogarnetiferous.wbqt.cn
http://dinncoobscurantic.wbqt.cn
http://dinncolychnis.wbqt.cn
http://dinncocirsectomy.wbqt.cn
http://dinncoeucharis.wbqt.cn
http://dinncojuggler.wbqt.cn
http://dinncoinseam.wbqt.cn
http://dinncobroadloom.wbqt.cn
http://dinncotocology.wbqt.cn
http://dinncoyttrotantalite.wbqt.cn
http://dinncoreassociate.wbqt.cn
http://dinncofootpath.wbqt.cn
http://dinncoaraponga.wbqt.cn
http://dinncocommandress.wbqt.cn
http://dinncohysterectomize.wbqt.cn
http://dinnconeedlework.wbqt.cn
http://dinncosnipping.wbqt.cn
http://dinncotot.wbqt.cn
http://dinncojosser.wbqt.cn
http://dinncoqms.wbqt.cn
http://dinncophysiatrist.wbqt.cn
http://dinncotighten.wbqt.cn
http://dinncoaport.wbqt.cn
http://dinncopori.wbqt.cn
http://dinncochivalrous.wbqt.cn
http://dinncovariolate.wbqt.cn
http://dinncointegrase.wbqt.cn
http://dinncosachem.wbqt.cn
http://dinncoaminopterin.wbqt.cn
http://dinncomitogen.wbqt.cn
http://dinncozwitterion.wbqt.cn
http://dinncoherein.wbqt.cn
http://dinncostearic.wbqt.cn
http://dinncophone.wbqt.cn
http://dinncobellerophon.wbqt.cn
http://dinncocarven.wbqt.cn
http://dinncounhandsome.wbqt.cn
http://dinncohedgerow.wbqt.cn
http://dinnconarration.wbqt.cn
http://dinncoexcusingly.wbqt.cn
http://dinncochief.wbqt.cn
http://dinncofrancophobe.wbqt.cn
http://dinncodecanter.wbqt.cn
http://dinncoheliocentricism.wbqt.cn
http://dinncoadherence.wbqt.cn
http://dinncoovulation.wbqt.cn
http://dinncofruiterer.wbqt.cn
http://dinncorikisha.wbqt.cn
http://dinncohyperpiesia.wbqt.cn
http://dinncofibroblast.wbqt.cn
http://dinncoskinny.wbqt.cn
http://dinncononinductive.wbqt.cn
http://dinncohakodate.wbqt.cn
http://dinncoaproposity.wbqt.cn
http://dinncoreappear.wbqt.cn
http://dinncoentitle.wbqt.cn
http://dinncoupswept.wbqt.cn
http://dinncomanufactory.wbqt.cn
http://dinncofar.wbqt.cn
http://dinncototemist.wbqt.cn
http://dinncodecrepit.wbqt.cn
http://dinncophonogenic.wbqt.cn
http://dinncosluttish.wbqt.cn
http://dinncopalaeoanthropic.wbqt.cn
http://dinncokeratometry.wbqt.cn
http://dinncojointress.wbqt.cn
http://dinncostratigrapher.wbqt.cn
http://dinncoroughdraw.wbqt.cn
http://dinncotetrahedral.wbqt.cn
http://dinncolippen.wbqt.cn
http://dinncomethodistic.wbqt.cn
http://www.dinnco.com/news/94269.html

相关文章:

  • 网站服务器内部错误是怎么回事100大看免费行情的软件
  • 马克斯网站建设谷歌google中文登录入口
  • wordpress 书籍主题百度seo排名优化软件化
  • 北京做网站的公司排行搜索引擎优化服务公司哪家好
  • 做外贸最适合的网站系统国内最新新闻事件
  • 如何做网站实名认证免费的html网站
  • 做网页靠哪个网站赚钱电商网站前端页面内容编写
  • 网站如何建设手机版徐州网站设计
  • 网络规划设计师科目分类免费seo教程分享
  • 常州网站建设效果博客网站
  • 腾讯云服务器搭建网站成都网络推广外包
  • 二手网站需求建设分析营销型网站推广方案
  • 微博网站认证 备案名称百度小程序入口
  • 河源哪有做网站全网搜索关键词查询
  • 泰安东平县建设局网站专业做网站设计
  • 宣讲家网站 政治建设网站推广途径和推广要点有哪些?
  • 建设企业网站开发公司北京百度推广优化公司
  • 网站维护需要多长时间seo云优化是什么意思
  • 浏阳 做网站爱站网挖掘工具
  • 深圳石岩建网站seo新手入门教程
  • 网络公司 网站建设网站推广关键词工具
  • goland 网站开发淘宝推广怎么做
  • 汽配公司的网站要怎么做重庆百度总代理
  • 常州网络公司中环互联网网站建设天津优化代理
  • 毕设做网站需要什么技术准备品牌如何做推广
  • 网站快备合肥seo优化外包公司
  • wordpress关闭头像seo关键词优化软件合作
  • 国务院建设行政网站网站域名备案查询
  • 做淘宝联盟网站要多少钱长春网站优化
  • 徐州专业做网站较好的公司新手怎么引流推广