网站制作致谢词,河南省网站,市场营销策略是什么,公司招聘网站续费申请#includearray std::array 是C11引入的一个模板类#xff0c;用于表示固定大小的数组。它结合了原生数组的高效性和标准库容器的便利性。以下是一些使用 std::array 的示例#xff0c;展示如何声明、初始化和操作 std::array。
1. 声明和初始化
声明一个空的 std::…
#includearray std::array 是C11引入的一个模板类用于表示固定大小的数组。它结合了原生数组的高效性和标准库容器的便利性。以下是一些使用 std::array 的示例展示如何声明、初始化和操作 std::array。
1. 声明和初始化
声明一个空的 std::array
#include array
#include iostreamint main() {// 声明一个大小为5的int类型的std::arraystd::arrayint, 5 arr;// 使用默认值初始化所有元素初始化为0for (const auto elem : arr) {std::cout elem ;}std::cout std::endl;return 0;
}输出
0 0 0 0 0使用列表初始化
#include array
#include iostreamint main() {// 使用列表初始化std::arrayint, 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 iostreamint main() {// 部分初始化std::arrayint, 5 arr {1, 2};// 遍历并打印数组元素for (const auto elem : arr) {std::cout elem ;}std::cout std::endl;return 0;
}输出
1 2 0 0 02. 访问数组元素
使用下标操作符 []
#include array
#include iostreamint main() {std::arrayint, 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 stdexceptint main() {std::arrayint, 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_range3. 获取数组大小
std::array 提供了 size() 方法来获取数组的大小。
#include array
#include iostreamint main() {std::arrayint, 5 arr {1, 2, 3, 4, 5};// 获取数组大小std::cout Array size: arr.size() std::endl;return 0;
}输出
Array size: 54. 使用迭代器
std::array 支持迭代器操作可以使用标准库算法。
#include array
#include iostream
#include algorithm // std::sortint main() {std::arrayint, 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 95. 使用 std::array 作为函数参数
传递 std::array 到函数
#include array
#include iostreamvoid printArray(const std::arrayint, 5 arr) {for (const auto elem : arr) {std::cout elem ;}std::cout std::endl;
}int main() {std::arrayint, 5 arr {1, 2, 3, 4, 5};// 将数组传递给函数printArray(arr);return 0;
}输出
1 2 3 4 56. 使用 std::array 的优势
固定大小std::array 的大小在编译时确定因此比 std::vector 更高效。安全性提供了范围检查通过 at() 方法。兼容性支持标准库算法和迭代器操作。 #include list std::list 是C标准模板库STL中的一个双向链表容器。它提供了高效的插入和删除操作并且可以动态调整大小。以下是一些使用 std::list 的示例展示如何声明、初始化、操作和遍历 std::list。std::list是一个非常灵活的容器适用于需要频繁插入和删除元素的场景。
1. 声明和初始化
声明一个空的 std::list
#include list
#include iostreamint main() {std::listint 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 iostreamint main() {// 使用初始化列表初始化std::listint myList {1, 2, 3, 4, 5};// 打印列表中的元素for (const auto elem : myList) {std::cout elem ;}std::cout std::endl;return 0;
}输出
1 2 3 4 52. 插入和删除操作
插入元素
std::list 提供了多种插入方法如 push_front、push_back 和 insert。
#include list
#include iostreamint main() {std::listint 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_front、pop_back 和 erase 方法来删除元素。
#include list
#include iostreamint main() {std::listint 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 53. 遍历列表
使用范围基于的 for 循环
#include list
#include iostreamint main() {std::listint 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 iostreamint main() {std::listint 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 54. 其他常用操作
获取列表大小
#include list
#include iostreamint main() {std::listint myList {1, 2, 3, 4, 5};// 获取列表大小std::cout List size: myList.size() std::endl;return 0;
}输出
List size: 5清空列表
#include list
#include iostreamint main() {std::listint 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::listint 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::listint 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 95. 使用 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 iostreamint main() {std::mapint, 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 iostreamint main() {// 使用初始化列表初始化std::mapint, 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: three2. 插入和删除操作
插入键值对
std::map 提供了多种插入方法如 operator[] 和 insert。
#include map
#include iostreamint main() {std::mapint, 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 iostreamint main() {std::mapint, 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: three3. 查找和访问键值对
查找键值对
std::map 提供了 find 方法来查找键值对。
#include map
#include iostreamint main() {std::mapint, 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 iostreamint main() {std::mapint, 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: three4. 遍历 std::map
使用范围基于的 for 循环
#include map
#include iostreamint main() {std::mapint, 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 iostreamint main() {std::mapint, 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: three5. 其他常用操作
获取 std::map 的大小
#include map
#include iostreamint main() {std::mapint, 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 iostreamint main() {std::mapint, 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)。自动排序键值对会根据键自动排序。支持迭代器操作可以使用标准库算法进行操作。