STL_Iterator
2016-06-25 14:19
309 查看
迭代器iterator是一个很神奇的东西。
STL的中心思想在于:将数据容器和算法分开,彼此独立设计,最后再以胶合剂(iterator)将他们撮合在一起。
#pragma once
#include<algorithm>
#include<vector>
#include<list>
#include<string>
void TestVector()
{
vector<int> v;
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(2);
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//STL排序头文件在algorithm
sort(v.begin(), v.end());
for (it=v.begin(); it < v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void TestList()
{
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.push_back(4);
list<int>::iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
it++;
}
cout << endl;
//替换函数
replace(l.begin(), l.end(), 3, 4);
it = l.begin();
while (it != l.end())
{
cout << *it << " ";
it++;
}
}
void Test3()
{
list<string> ls;
ls.push_back("aa");
ls.push_back("bb");
ls.push_back("cc");
ls.push_back("dd");
list<string>::iterator it = ls.begin();
while (it != ls.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//find
it = find(ls.begin(), ls.end(), "aa");
//it = find(ls.begin(), ls.end(), "ee"TL);
if (it != ls.end())//如果查找失败(不存在),it就会返回ls.end()
cout << *it << endl;
else
cout << "find false" << endl;
}
STL的中心思想在于:将数据容器和算法分开,彼此独立设计,最后再以胶合剂(iterator)将他们撮合在一起。
#pragma once
#include<algorithm>
#include<vector>
#include<list>
#include<string>
void TestVector()
{
vector<int> v;
v.push_back(1);
v.push_back(4);
v.push_back(3);
v.push_back(2);
vector<int>::iterator it = v.begin();
while (it != v.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//STL排序头文件在algorithm
sort(v.begin(), v.end());
for (it=v.begin(); it < v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void TestList()
{
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(3);
l.push_back(4);
list<int>::iterator it = l.begin();
while (it != l.end())
{
cout << *it << " ";
it++;
}
cout << endl;
//替换函数
replace(l.begin(), l.end(), 3, 4);
it = l.begin();
while (it != l.end())
{
cout << *it << " ";
it++;
}
}
void Test3()
{
list<string> ls;
ls.push_back("aa");
ls.push_back("bb");
ls.push_back("cc");
ls.push_back("dd");
list<string>::iterator it = ls.begin();
while (it != ls.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//find
it = find(ls.begin(), ls.end(), "aa");
//it = find(ls.begin(), ls.end(), "ee"TL);
if (it != ls.end())//如果查找失败(不存在),it就会返回ls.end()
cout << *it << endl;
else
cout << "find false" << endl;
}
相关文章推荐
- HDU2004 成绩转换
- 一些Android 基础知识
- 欢迎使用CSDN-markdown编辑器
- js如何获取某id的子标签
- AJAX 汽车详细信息练习
- (dp)343. Integer Break
- C++中extern “C”含义深层探索
- 仿HTML5 CSS "标签云"特效-优化效率版
- 易语言使用v8引擎(基于UseDotNet)
- 图片相似度算法,可识别出缩放,拉伸,亮暗后的同一张图片
- 百度api 轨迹
- 异常通知辅助模块 5.3 (try/catch)
- C++高质量编程点滴
- mysql5.7.13免安装版超详细安装教程
- 去除挖宝报毒的问题,修改基础包的java代码,并且打包
- php 面试题(1)
- C++非面向对象的特征
- Adapter类型控件之ListView(列表控件)
- struts2点点
- C++中的结构体与sizeof