目录:
- demonstrating input and output with iterators
- Standard library vector class template
- testing standard library vector class template element-manipulation functions.
- standard library list class template
- standard library deque class template
- Standard Library multiset class template
- standard Library set class template
- Standard Library multimap class template
- Stardard Library class map class template
- standard library stack adapter class
- standard library queue adapter class template
- Standard Library priority_queue adapter class
- 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}; // creat
int 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 element
cout << "\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);
// sort
values.sort(); // sort values
cout << "\nvalues after sorting contains: ";
printList(values);
// insert element of ints into otherValues
vector<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 valuse
values.splice(values.cend(), otherValues);
cout << "\nAfter splice, values contains: ";
printList(values);
cout << "\nAfter splice, otherValues contains: ";
printList(otherValues);
// sort
values.sort();
cout << "\nAfter splice, sort, values contains: ";
printList(values);
// insert element into otherValues
otherValues.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 elements
values.unique();
cout << "\nAfter unique, values contains: ";
printList(values);
cout << "\nnow otherValues contains: ";
printList(otherValues);
// swap element of values and otherValues
values.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 order
values.merge(otherValues);
cout << "\n\nAfter merge, values contains: ";
printList(values);
cout << "\nAfter merge, othervalues contains: ";
printList(otherValues);
values.remove(4); // remove all 4
cout << "\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 values
values.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 values
cout << "\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 ignored
pairs.insert(make_pair(20, 9.345));
pairs.insert(make_pair(15, 99.3)); // dup ignored
cout << "\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;
}