15 standard library containers and iterators

2023-12-17 23:58

本文主要是介绍15 standard library containers and iterators,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录:

  1. demonstrating input and output with iterators
  2. Standard library vector class template
  3. testing standard library vector class template element-manipulation functions.
  4. standard library list class template
  5. standard library deque class template
  6. Standard Library multiset class template
  7. standard Library set class template
  8. Standard Library multimap class template
  9. Stardard Library class map class template
  10. standard library stack adapter class
  11. standard library queue adapter class template
  12. Standard Library priority_queue adapter class

 

 


 

  1. demonstrating input and output with iterators
#include <iostream>
#include <iterator>
using namespace std;int main() {cout << "Enter two integers:\n" ;istream_iterator<int> inputInt{cin}; // creatint number1{*inputInt};++inputInt;int number2{*inputInt};++inputInt;int number3{*inputInt};ostream_iterator<int> outputInt(cout);cout << "The sum is :";*outputInt = number1 + number2;++outputInt;cout << "\nthe number3 is :";*outputInt = number3;cout << endl;
}


2. Standard library vector class template

#include <iostream>
#include <vector>using namespace std;template<typename T>
void printVector(const vector<T> &integers) {for (auto const &item:integers) {cout << item << " ";}
}template <typename T>
void printVector2(const vector<T>& integers){
//    for(vector<int>::const_iterator constIterator = integers.cbegin(); constIterator != integers.cend(); ++constIterator) {for(vector<int>::const_iterator constIterator = integers.cbegin(); constIterator != integers.cend(); ++constIterator) {cout << *constIterator << " ";}
}int main() {vector<int> integres;cout << "The initial size of integers is :" << integres.size()<< "\nThe initial capacity of integers is :" << integres.capacity();integres.push_back(2);integres.push_back(3);integres.push_back(4);cout << "\n\nThe initial size of integers is :" << integres.size()<< "\nThe initial capacity of integers is :" << integres.capacity();cout << "\n\nprint vector items:";printVector(integres);cout << "\nprint reverse order:";for (auto reverseIterator = integres.crbegin(); reverseIterator != integres.crend(); ++reverseIterator) {cout << *reverseIterator << " ";}cout << "\nprint build_in array:";const size_t SIZE{6};int values[SIZE]{1, 2, 3, 4, 5, 6};for (const int *ptr = cbegin(values); ptr != cend(values); ++ptr) {cout << *ptr << " ";}
}

注意定义 iterators 的时候需要注意!


3. testing standard library vector class template element-manipulation functions.

// testing standard library vector class template element-manipulation functions.#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <stdexcept>using namespace std;int main() {vector<int> values{1, 2, 3, 4, 5, 6};vector<int> integers{values.cbegin(), values.cend()};ostream_iterator<int> output{cout, " "};cout << "Vector integers contain: ";copy(integers.cbegin(), integers.cend(), output);cout << "\nFirst element of integers: " << integers.front()<< "\nLast element of integers: " << integers.back();integers[0] = 7;integers.at(2) = 10;integers.insert(integers.cbegin() + 1, 22);cout << "\n\nContents of vector integers after changes: ";copy(integers.cbegin(), integers.cend(), output);try {integers.at(100) = 777;}catch (out_of_range &outOfRange) {cout << "\n\nException: " << outOfRange.what();}integers.erase(integers.cbegin()); // erase first elementcout << "\n\nVector integers after erasing first element: ";copy(integers.cbegin(), integers.cend(), output);integers.erase(integers.cbegin(), integers.cend());cout << "\nAfter earsing all elements, vector integers " << (integers.empty() ? "is ":"is not " ) << "empyt";integers.insert(integers.cbegin(), values.cbegin(), values.cend());
//    integers.insert(integers.cbegin(), 80);cout << "\n\nContents of vector integers before clear: ";copy(integers.cbegin(), integers.cend(), output);integers.clear();cout << "\nAfter clear, vector integers: " << (integers.empty() ? "is " :"is not " ) << "empty" << endl;
}


4. standard library list class template.


// standard library list class template.#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>using namespace std;template<typename T>
void printList(const list<T> &listRef);template<typename T>
void printList(const list<T> &listRef) {if (listRef.empty()) {cout << "List is empyt";} else {ostream_iterator<T> output{cout, " "};copy(listRef.cbegin(), listRef.cend(), output);}
}int main() {list<int> values;list<int> otherValues;values.push_front(1);values.push_front(2);values.push_back(4);values.push_back(3);cout << "values contains: ";printList(values);// sortvalues.sort(); // sort valuescout << "\nvalues after sorting contains: ";printList(values);// insert element of ints into otherValuesvector<int> ints{2, 6, 4, 8};otherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend());cout << "\nAfter insert, otherValues contains: ";printList(otherValues);// remove otherValues elements and insert at end of valusevalues.splice(values.cend(), otherValues);cout << "\nAfter splice, values contains: ";printList(values);cout << "\nAfter splice, otherValues contains: ";printList(otherValues);// sortvalues.sort();cout << "\nAfter splice, sort, values contains: ";printList(values);// insert element into otherValuesotherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend());otherValues.sort();cout << "\nAfter insert and sort, otherValues contains: ";printList(otherValues);// remove otherValues elements and insert into values in sorted order// merge 会抹去 otherValues 里面的内容values.merge(otherValues);cout << "\n\nAfter merge:\n values contains: ";printList(values);cout << "\n otherValues contains: ";printList(otherValues);values.pop_front();values.pop_back();cout << "\n\nAfter pop_front and pop_back:\n values contains: ";printList(values);// remove duplicate elementsvalues.unique();cout << "\nAfter unique, values contains: ";printList(values);cout << "\nnow otherValues contains: ";printList(otherValues);// swap element of values and otherValuesvalues.swap(otherValues);cout << "\n\nAfter swap:\n values contains: ";printList(values);cout << "\n otherValues contains: ";printList(otherValues);// replace contents of values with elements of otherValues, assign会保留自己values.assign(otherValues.cbegin(), otherValues.cend());cout << "\n\nAfter assign, values contains: ";printList(values);cout << "\nAfter assign, otherValues contains: ";printList(otherValues);// remove otherVales elements and insert into values in sorted ordervalues.merge(otherValues);cout << "\n\nAfter merge, values contains: ";printList(values);cout << "\nAfter merge, othervalues contains: ";printList(otherValues);values.remove(4); // remove all 4cout << "\n\nAfter remove(4), values contains: ";printList(values);cout << endl;}


5. standard library deque class template


// standard library deque class template#include <iostream>
#include <deque>
#include <algorithm>
#include <iterator>using namespace std;int main() {deque<double> values;ostream_iterator<double> output{cout, " "};// insert element in valuesvalues.push_front(2.2);values.push_front(3.5);values.push_back(1.1);cout << "values contains: ";copy(values.cbegin(), values.cend(), output);values.pop_front(); // remove first element// using  subscript operator to obtain element of valuescout << "\nAfter pop_front(), values contains: ";for (size_t i{0}; i < values.size(); ++i) {cout << values[i] << " ";}values[1] = 5.4;cout << "\nAfter values[1]=5.4, values contains: ";copy(values.cbegin(), values.cend(), output);cout << endl;}


6. Standard Library multiset class template

// Standard Library multiset class template#include <array>
#include <iostream>
#include <set>
#include <algorithm>
#include <iterator>
#include <vector>using namespace std;int main() {multiset<int, less<int>> intMultiset;cout << "There are currently " << intMultiset.count(15)<< " values of 15 in the multiset\n";intMultiset.insert(15);intMultiset.insert(15);cout << "After inserts, there are " << intMultiset.count(15)<< " values of 15 in themultiset\n\n";auto result{intMultiset.find(15)};if (result != intMultiset.end()) {cout << "Found value 15\n";}result = intMultiset.find(20);if (result == intMultiset.end()) {cout << "Did not find value 20\n";}vector<int> a{7, 22, 9, 1, 18, 30, 100, 22, 85, 13};intMultiset.insert(a.cbegin(), a.cend());cout << "\nAfter insert, intMultiset contains:\n";ostream_iterator<int> output{cout, " "};copy(intMultiset.begin(), intMultiset.end(), output);cout << "\n\nLower bound of 22: " << *(intMultiset.lower_bound(22));cout << "\nUpper bound of 22: " << *(intMultiset.upper_bound(22));auto p{intMultiset.equal_range(22)};cout << "\n\nequal_range of 22: " << "\n Lower bound: " << *(p.first)<< "\n Upper bound: " << *(p.second);cout << endl;
}


7. standard Library set class template

// standard Library set class template.
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <iterator>
using namespace std;int main() {vector<double> a {2.1, 4.2, 9.5, 2.1, 3.7};set<double, less<double>> doubleSet{a.begin(), a.end()};cout << "double contains: ";ostream_iterator<double> output{cout, " "};copy(doubleSet.begin(), doubleSet.end(), output);auto p{doubleSet.insert(13.8)};cout << "\n\n" << *(p.first) << (p.second ? " was " : " was not ") << "insertd";cout << "\ndoubleSet contains: ";copy(doubleSet.begin(), doubleSet.end(), output);auto p2{doubleSet.insert(9.5)};cout << "\n\n" << *(p2.first) << (p2.second ? " was " : " was not ") << "insertd";cout << "\ndoubleSet contains: ";copy(doubleSet.begin(), doubleSet.end(), output);}


8.Standard Library multimap class template

// Standard Library multimap class template#include <iostream>
#include <map>
using namespace std;int main() {multimap<int, double, less<int>> pairs;cout << "There are currently " << pairs.count(15)<< " pairs with key 15 in the multimap\n";//    pairs.insert(make_pair(15, 99.3));pairs.insert({15, 99.3}); // 另一种写法pairs.insert(make_pair(15, 2.7));cout << "There are currently " << pairs.count(15)<< " pairs with key 15 in the multimap\n";pairs.insert(make_pair(30, 111.11));pairs.insert(make_pair(10, 22.22));pairs.insert(make_pair(25, 33.333));pairs.insert(make_pair(20, 9.345));pairs.insert(make_pair(5, 77.54));cout << "\nMultimap pairs contains:\nKey\tValue\n";for (auto mapItem:pairs) {cout << mapItem.first << "\t" << mapItem.second << "\n";}cout << endl;
}


9.Stardard Library class map class template

// Stardard Library class map class template#include <iostream>
#include <map>
using namespace  std;int main() {map<int, double, less<int>> pairs;pairs.insert({15, 2.7});pairs.insert(make_pair(30, 111.11));pairs.insert(make_pair(5, 1010.1));pairs.insert(make_pair(10, 22.22));pairs.insert(make_pair(25, 33.333));pairs.insert(make_pair(5, 77.54)); // dup ignoredpairs.insert(make_pair(20, 9.345));pairs.insert(make_pair(15, 99.3)); // dup ignoredcout << "\n\npairs contains:\nKey\tValue\n";for (auto mapItem:pairs) {cout << mapItem.first << "\t" << mapItem.second << "\n";}pairs[25] = 999.99;pairs[40] = 8765.43;cout << "\n\npairs contains:\nKey\tValue\n";for (auto mapItem:pairs) {cout << mapItem.first << "\t" << mapItem.second << "\n";}cout << endl;
}


10.standard library stack adapter class

// standard library stack adapter class
#include <iostream>
#include <stack>
#include <vector>
#include <list>using namespace std;template<typename T>
void pushElement(T &stackRef);template<typename T>
void popElement(T &stackRef);int main() {stack<int> intDequeStack;stack<int, vector<int>> intVectorStack;stack<int, list<int>> intListStack;cout << "Pushing onto intDequeStack: ";pushElement(intDequeStack);cout << "\nPushing onto intVectorStack: ";pushElement(intVectorStack);cout << "\nPushing onto intListStack: ";pushElement(intListStack);cout << "\n\nPopping from intDequeStack: ";popElement(intDequeStack);cout << "\nPushing onto intVectorStack: ";popElement(intVectorStack);cout << "\nPushing onto intListStack: ";popElement(intListStack);
}template<typename T>
void pushElement(T &stackRef) {for (int i{0}; i < 10; ++i) {stackRef.push(i);cout << stackRef.top() << " "; // view and display top element}
}template<typename T>
void popElement(T &stackRef) {while (!stackRef.empty()) {cout << stackRef.top() << " "; // top 不会删除元素stackRef.pop();}
}


11. standard library queue adapter class template

// standard library queue adapter class template#include <iostream>
#include <queue>
using namespace std;int main() {queue<double>  values;values.push(3.2);values.push(9.8);values.push(5.4);cout << "Popping from values: ";while (!values.empty()) {cout << values.front() << " ";values.pop();}cout << endl;
}


12. Standard Library priority_queue adapter class

// Standard Library priority_queue adapter class
// heap 的意思,小顶堆、大顶堆 , by default less<T>
#include <iostream>
#include <queue>using namespace std;int main() {priority_queue<double> priorities;priorities.push(3.2);priorities.push(9.8);priorities.push(5.4);cout << "Popping from priorities: ";while (!priorities.empty()) {cout << priorities.top() << " ";priorities.pop();}cout << endl;
}


 

 

 

这篇关于15 standard library containers and iterators的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/506341

相关文章

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Adblock Plus官方规则Easylist China说明与反馈贴(2015.12.15)

-------------------------------特别说明--------------------------------------- 视频广告问题:因Adblock Plus的局限,存在以下现象,优酷、搜狐、17173黑屏并倒数;乐视、爱奇艺播放广告。因为这些视频网站的Flash播放器被植入了检测代码,而Adblock Plus无法修改播放器。 如需同时使用ads

15 组件的切换和对组件的data的使用

划重点 a 标签的使用事件修饰符组件的定义组件的切换:登录 / 注册 泡椒鱼头 :微辣 <!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-

PIL Python Imaging Library (PIL)

介绍         把Python的基础知识学习后,尝试一下如何安装、加载、使用非标准库,选择了图像处理模块PIL。         Python Imaging Library (PIL)是PythonWare公司提供的免费的图像处理工具包,是python下的图像处理模块,支持多种格式,并提供强大的图形与图像处理功能。虽然在这个软件包上要实现类似MATLAB中的复杂的图像处理算法并不

java基础总结15-面向对象11(抽象类)

下面通过一下的小程序深入理解抽象类 因此在类Animal里面只需要定义这个enjoy()方法就可以了,使用abstract关键字把enjoy()方法定义成一个抽象方法,定义如下:public abstract void enjoy();   从某种意义上来说,抽象方法就是被用来重写的,所以在父类声明的抽象方法一定要在子类里面重写。如果真的不想在子类里面重写这个方法,那么可以再在子类里

15年亚洲区长春站赛后总结

刷题打比赛的日子才叫青春   今年和ljy、lsj组队去长春站。这支队伍是我很放心的一支队伍,ljy可以做数学题和复杂思维题,lsj思维缜密可以和ljy对思路,我负责手速狗+模板暴力流。 有了去年两场亚洲区的经验,心态有了很大变化,也深知赛场上风云莫测,不至最后一分钟,仍未分胜负。开场的F题卡了很久,WA了很多发,这种复杂思维题丢给ljy和lsj搞了。我去开L题,给LJY说完题意后,他给

Android studio jar包多层嵌套,Add library '__local_aars__:...@jar' to classpath问题

在添加jar包,早app下的build.gradle中的 implementation files('libs/jar包的名字.jar') 修改为 api files('libs/jar包的名字.jar') implementation 单层引用,只引用当前jar包层, api 多层引用,应用当前jar包层,已经jar包引用的jar包层

How can I provide a RGBA png file to OpenAI PHP library

题意:将RGBA PNG文件提供给OpenAI的PHP库 问题背景: I import Orhanerday\OpenAi library to my DALL-E Examples project but when I provide images, I got Invalid input image - format must be in ['RGBA'], got RGB. er

找不同-第15届蓝桥省赛Scratch初级组真题第4题

[导读]:超平老师的《Scratch蓝桥杯真题解析100讲》已经全部完成,后续会不定期解读蓝桥杯真题,这是Scratch蓝桥杯真题解析第183讲。 如果想持续关注Scratch蓝桥真题解读,可以点击《Scratch蓝桥杯历年真题》并订阅合集,查阅教程更方便。 第15届蓝桥杯省赛已于2024年8月24日落下帷幕,编程题一共有5题,分别如下: 猪八戒落地 游乐场 画西瓜 找不同 消