//fig15_04.cpp
#include
#include //ostream_iterator istream_iterator
using namespace std;
//输入两个数 输出两个数的和
int main()
{
istream_iterator inputInt(cin);//与cin关联上
int num1 = *inputInt;//接受一个整数
++inputInt;//移动到下一个输入值
int num2 = *inputInt;//接受下一个输入
ostream_iterator outputInt(cout);//与cout关联上
cout <<"the sum is ";
*outputInt = num1 + num2;//接受一个输出
cout <
迭代器还可以这么用 ,卧槽!
迭代器的类型
标准容器支持的迭代器
顺序容器
Vector
例子-使用迭代器遍历vector-顺序和逆序
//fig15_10.cpp
#include
#include
using namespace std;
template
void printVector(const vector& integers)
{
for (auto cOnstIterator= integers.cbegin();
constIterator != integers.cend(); ++constIterator)
{
cout <<*constIterator <<" ";
}
cout <
void printReverseVector(const vector& integers)
{
for (auto cOnstReverseIterator= integers.crbegin();
constReverseIterator != integers.crend(); ++constReverseIterator)
{
cout <<*constReverseIterator <<" ";
}
cout < integers;
cout <<"The initial size of integers is" <
例子-操作vector元素
//fig15_11.cpp
#include
#include
#include
#include
#include
#include
using namespace std;
int main()
{
const size_t SIZE = 6;
array values = { 1,2,3,4,5,6 };
vector integers(values.cbegin(),values.cend());//使用array来初始化vector
ostream_iteratoroutput(cout, " ");//通过迭代cout来输出元素,以空格来分隔
//输出
cout <<"Vector integers contains:";
copy(integers.cbegin(), integers.cend(), output);
//首尾元素
//front和back返回的是元素的引用,而begin和end返回的是随机访问迭代器
if (!integers.empty())//检查vector是否为空,不检查的话 如果integers是空,则运行时报错
{
cout <<"\nFirst element is " <
List
//fig15_13.cpp
//List
#include
#include
#include
#include
#include
using namespace std;
template
void printList(const list& listRef)
{
if (listRef.empty())
{
cout <<"list is empty!" < output(cout, " ");
copy(listRef.cbegin(), listRef.cend(), output);
}
cout <ints = { 1,2,3,4 };
list values;
list otherValues;
values.push_front(1);
values.push_front(2);
values.push_back(4);
values.push_back(3);
cout <<"List contains:";
printList(values);
//排序
values.sort();
cout <<"values after sorting contains:";
printList(values);
//使用array来插入otherValues
otherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend());
cout <<"After insert ,otherList contains:";
printList(otherValues);
//splice做了两步:第一步将othevalues里的值插入到values.cend(),第二步将otherValues里的值清空
values.splice(values.cend(), otherValues);
cout <<"After splice values contains:";
printList(values);
cout <<"After splice ,otherValues contains:";
printList(otherValues);
otherValues.insert(otherValues.cbegin(), ints.cbegin(), ints.cend());
cout <<"After insert and sort ,otherList contains:";
otherValues.sort();
printList(otherValues);
values.sort();
//使用merge前,两个容器必须先排序
//merge也是做了两步,第一步将两个容器归并(类似于归并排序),第二步将otherValues清空
values.merge(otherValues);
cout <<"After merge values contains:";
printList(values);
cout <<"After merge otherValues contains:";
printList(otherValues);
values.pop_front();
values.pop_back();
cout <<"After pop_front and pop_back values contains:";
printList(values);
//去重
values.unique();
cout <<"After unique, values contains:";
printList(values);
//交换
values.swap(otherValues);
cout <<"After swap values contains:";
printList(values);
cout <<"After swap otherValues contains:";
printList(otherValues);
//赋值
values.assign(otherValues.cbegin(), otherValues.cend());
cout <<"After assign values contains:";
printList(values);
values.push_front(4);
cout <<"After push_front(4) values contains:";
printList(values);
//移除所有的4
values.remove(4);
cout <<"After remove(4) values contains:";
printList(values);
return 0;
}
deque
//fig15_14.cpp
//deque 双端队列
#include
#include
#include
#include
using namespace std;
int main()
{
dequevalues;
ostream_iterator output(cout, " ");
//双端
values.push_front(2.2);
values.push_front(3.5);
values.push_back(1.1);
//随机访问
cout <<"values contains:";
for (size_t i = 0; i
关联容器
multiset
//fig15_15.cpp
//multi_set
#include
#include
#include
#include
#include
using namespace std;
int main()
{
const size_t SIZE = 6;
array a = { 11,21,13,21,54,66 };
//按照从小到大顺序排序 less
multiset> intMultiset;
ostream_iterator output(cout, " ");
//count 是说该元素在multiset中含有几个
cout <<"There are currently " <
multimap
//fig15_16.cpp
//multimap
#include
map
//fig15_18.cpp
//map
#include
#include
[c++基础]STL