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

绵阳市网站建立线上营销推广方案有哪些

绵阳市网站建立,线上营销推广方案有哪些,河南网站seo优化,怎么查网站做站点地图#include<array> std::array 是C11引入的一个模板类&#xff0c;用于表示固定大小的数组。它结合了原生数组的高效性和标准库容器的便利性。以下是一些使用 std::array 的示例&#xff0c;展示如何声明、初始化和操作 std::array。 1. 声明和初始化 声明一个空的 std::…

#include<array>
std::array 是C++11引入的一个模板类,用于表示固定大小的数组。它结合了原生数组的高效性和标准库容器的便利性。以下是一些使用 std::array 的示例,展示如何声明、初始化和操作 std::array

1. 声明和初始化

声明一个空的 std::array
#include <array>
#include <iostream>int main() {// 声明一个大小为5的int类型的std::arraystd::array<int, 5> arr;// 使用默认值初始化(所有元素初始化为0)for (const auto& elem : arr) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

0 0 0 0 0
使用列表初始化
#include <array>
#include <iostream>int main() {// 使用列表初始化std::array<int, 5> arr = {1, 2, 3, 4, 5};// 遍历并打印数组元素for (const auto& elem : arr) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 3 4 5
部分初始化

如果提供的初始化值少于数组大小,剩余的元素将被默认初始化为0。

#include <array>
#include <iostream>int main() {// 部分初始化std::array<int, 5> arr = {1, 2};// 遍历并打印数组元素for (const auto& elem : arr) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 0 0 0

2. 访问数组元素

使用下标操作符 []
#include <array>
#include <iostream>int main() {std::array<int, 5> arr = {1, 2, 3, 4, 5};// 访问并修改数组元素arr[2] = 10;// 打印修改后的数组for (const auto& elem : arr) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 10 4 5
使用 at() 方法

at() 方法提供了范围检查,如果索引超出范围,会抛出 std::out_of_range 异常。

#include <array>
#include <iostream>
#include <stdexcept>int main() {std::array<int, 5> arr = {1, 2, 3, 4, 5};try {// 访问数组元素std::cout << arr.at(2) << std::endl;// 尝试访问超出范围的元素std::cout << arr.at(10) << std::endl;} catch (const std::out_of_range& e) {std::cerr << "Error: " << e.what() << std::endl;}return 0;
}

输出:

3
Error: std::out_of_range

3. 获取数组大小

std::array 提供了 size() 方法来获取数组的大小。

#include <array>
#include <iostream>int main() {std::array<int, 5> arr = {1, 2, 3, 4, 5};// 获取数组大小std::cout << "Array size: " << arr.size() << std::endl;return 0;
}

输出:

Array size: 5

4. 使用迭代器

std::array 支持迭代器操作,可以使用标准库算法。

#include <array>
#include <iostream>
#include <algorithm> // std::sortint main() {std::array<int, 5> arr = {5, 2, 9, 1, 5};// 使用标准库算法排序std::sort(arr.begin(), arr.end());// 打印排序后的数组for (const auto& elem : arr) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 5 5 9

5. 使用 std::array 作为函数参数

传递 std::array 到函数
#include <array>
#include <iostream>void printArray(const std::array<int, 5>& arr) {for (const auto& elem : arr) {std::cout << elem << " ";}std::cout << std::endl;
}int main() {std::array<int, 5> arr = {1, 2, 3, 4, 5};// 将数组传递给函数printArray(arr);return 0;
}

输出:

1 2 3 4 5

6. 使用 std::array 的优势

  • 固定大小std::array 的大小在编译时确定,因此比 std::vector 更高效。
  • 安全性:提供了范围检查(通过 at() 方法)。
  • 兼容性:支持标准库算法和迭代器操作。

#include <list>
std::list 是C++标准模板库(STL)中的一个双向链表容器。它提供了高效的插入和删除操作,并且可以动态调整大小。以下是一些使用 std::list 的示例,展示如何声明、初始化、操作和遍历 std::liststd::list是一个非常灵活的容器,适用于需要频繁插入和删除元素的场景。

1. 声明和初始化

声明一个空的 std::list
#include <list>
#include <iostream>int main() {std::list<int> myList;// 使用 push_back 添加元素myList.push_back(10);myList.push_back(20);myList.push_back(30);// 打印列表中的元素for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

10 20 30
使用初始化列表初始化
#include <list>
#include <iostream>int main() {// 使用初始化列表初始化std::list<int> myList = {1, 2, 3, 4, 5};// 打印列表中的元素for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 3 4 5

2. 插入和删除操作

插入元素

std::list 提供了多种插入方法,如 push_frontpush_backinsert

#include <list>
#include <iostream>int main() {std::list<int> myList = {1, 2, 3, 4, 5};// 在列表开头插入元素myList.push_front(0);// 在列表末尾插入元素myList.push_back(6);// 在指定位置插入元素auto it = myList.begin();std::advance(it, 3); // 移动到第3个位置myList.insert(it, 99);// 打印列表中的元素for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

0 1 2 99 3 4 5 6
删除元素

std::list 提供了 pop_frontpop_backerase 方法来删除元素。

#include <list>
#include <iostream>int main() {std::list<int> myList = {0, 1, 2, 99, 3, 4, 5, 6};// 删除列表开头的元素myList.pop_front();// 删除列表末尾的元素myList.pop_back();// 删除指定位置的元素auto it = myList.begin();std::advance(it, 3); // 移动到第3个位置myList.erase(it);// 打印列表中的元素for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 3 4 5

3. 遍历列表

使用范围基于的 for 循环
#include <list>
#include <iostream>int main() {std::list<int> myList = {1, 2, 3, 4, 5};// 使用范围基于的 for 循环遍历列表for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 3 4 5
使用迭代器
#include <list>
#include <iostream>int main() {std::list<int> myList = {1, 2, 3, 4, 5};// 使用迭代器遍历列表for (auto it = myList.begin(); it != myList.end(); ++it) {std::cout << *it << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 3 4 5

4. 其他常用操作

获取列表大小
#include <list>
#include <iostream>int main() {std::list<int> myList = {1, 2, 3, 4, 5};// 获取列表大小std::cout << "List size: " << myList.size() << std::endl;return 0;
}

输出:

List size: 5
清空列表
#include <list>
#include <iostream>int main() {std::list<int> myList = {1, 2, 3, 4, 5};// 清空列表myList.clear();// 检查列表是否为空if (myList.empty()) {std::cout << "List is empty." << std::endl;}return 0;
}

输出:

List is empty.
反转列表
#include <list>
#include <iostream>
#include <algorithm> // std::reverseint main() {std::list<int> myList = {1, 2, 3, 4, 5};// 反转列表myList.reverse();// 打印反转后的列表for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

5 4 3 2 1
排序列表
#include <list>
#include <iostream>
#include <algorithm> // std::sortint main() {std::list<int> myList = {5, 2, 9, 1, 5};// 排序列表myList.sort();// 打印排序后的列表for (const auto& elem : myList) {std::cout << elem << " ";}std::cout << std::endl;return 0;
}

输出:

1 2 5 5 9

5. 使用 std::list 的优势

  • 高效的插入和删除操作std::list 是双向链表,插入和删除操作的时间复杂度为 O(1)。
  • 动态调整大小:可以动态添加和删除元素,不需要预先分配固定大小。
  • 支持迭代器操作:可以使用标准库算法进行操作。

#include <map>

std::map 是C++标准模板库(STL)中的一个关联容器,用于存储键值对(key-value pairs)。它基于红黑树实现,能够高效地进行插入、删除和查找操作。以下是一些使用 std::map 的示例,展示如何声明、初始化、操作和遍历 std::map
std::map 是一个非常强大的关联容器,适用于需要高效查找、插入和删除操作的场景。

1. 声明和初始化

声明一个空的 std::map
#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap;// 插入键值对myMap[1] = "one";myMap[2] = "two";myMap[3] = "three";// 打印键值对for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

输出:

1: one
2: two
3: three
使用初始化列表初始化
#include <map>
#include <iostream>int main() {// 使用初始化列表初始化std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 打印键值对for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

输出:

1: one
2: two
3: three

2. 插入和删除操作

插入键值对

std::map 提供了多种插入方法,如 operator[]insert

#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap;// 使用 operator[] 插入键值对myMap[1] = "one";myMap[2] = "two";// 使用 insert 方法插入键值对myMap.insert(std::make_pair(3, "three"));// 打印键值对for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

输出:

1: one
2: two
3: three
删除键值对

std::map 提供了 erase 方法来删除键值对。

#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 删除键为2的键值对myMap.erase(2);// 打印键值对for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

输出:

1: one
3: three

3. 查找和访问键值对

查找键值对

std::map 提供了 find 方法来查找键值对。

#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 查找键为2的键值对auto it = myMap.find(2);if (it != myMap.end()) {std::cout << "Found: " << it->first << " -> " << it->second << std::endl;} else {std::cout << "Key not found." << std::endl;}return 0;
}

输出:

Found: 2 -> two
访问键值对

可以直接使用 operator[] 或迭代器来访问键值对。

#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 使用 operator[] 访问键值对std::cout << "Key 1: " << myMap[1] << std::endl;// 使用迭代器访问键值对for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

输出:

Key 1: one
1: one
2: two
3: three

4. 遍历 std::map

使用范围基于的 for 循环
#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 使用范围基于的 for 循环遍历for (const auto& pair : myMap) {std::cout << pair.first << ": " << pair.second << std::endl;}return 0;
}

输出:

1: one
2: two
3: three
使用迭代器
#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 使用迭代器遍历for (auto it = myMap.begin(); it != myMap.end(); ++it) {std::cout << it->first << ": " << it->second << std::endl;}return 0;
}

输出:

1: one
2: two
3: three

5. 其他常用操作

获取 std::map 的大小
#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 获取大小std::cout << "Map size: " << myMap.size() << std::endl;return 0;
}

输出:

Map size: 3
清空 std::map
#include <map>
#include <iostream>int main() {std::map<int, std::string> myMap = {{1, "one"},{2, "two"},{3, "three"}};// 清空 mapmyMap.clear();// 检查是否为空if (myMap.empty()) {std::cout << "Map is empty." << std::endl;}return 0;
}

输出:

Map is empty.

6. 使用 std::map 的优势

  • 高效的查找、插入和删除操作:基于红黑树实现,操作的时间复杂度为 O(log n)。
  • 自动排序:键值对会根据键自动排序。
  • 支持迭代器操作:可以使用标准库算法进行操作。
http://www.mmbaike.com/news/91317.html

相关文章:

  • 免费课程网站有哪些微信seo排名优化软件
  • 穷游网站 做行程 封面网络运营培训
  • 网站建设宝安全案网络推广公司
  • 福建省人民政府驻上海办事处企业seo整站优化方案
  • 网站风格发展趋势网络营销的特点有哪些特点
  • 建设银行网站地址郑州优化网站关键词
  • 常德市网站建设免费网站大全
  • 网站友情链接对方网站没有加入本站链接对本站有没有影响?自己的网站怎么推广
  • 网站建设流程与步骤东莞网站推广营销网站设计
  • 做科技汽车的视频网站有哪些推广seo优化公司
  • 视频网站备案怎么做湖北seo推广
  • 深圳网站建设ucreatorseo基础入门
  • 网页小游戏点击即玩网络营销seo优化
  • 投资网站建设十八大禁用黄app入口
  • 邓州网站设计免费的编程自学网站
  • 做网站是什么专业什么工作seo实战密码第三版pdf
  • 移动端网站案例怎样看网页的友情链接
  • WordPress css隐藏熊掌号seo外链代发
  • discuz 做网站可以吗做网络推广的团队
  • 网站建设方案 文库如何做营销活动
  • 龙岗网站建设公司哪家好seo怎么做
  • 网站建设有模板吗国内免费顶级域名注册
  • 成都设计公司上市东莞网站推广行者seo08
  • 代做课程设计网站竹子建站官网
  • 网站中 点击出现登录框怎么做软文推广是什么意思?
  • 立方米网站数字营销平台有哪些
  • 品牌网站设计制作哪家好百度如何购买关键词
  • java门户网站开发教程seo引擎搜索入口
  • 推广自己的网站需要怎么做网络营销手段有哪些方式
  • 网站做点击收费广告投放平台公司