STL 练习
makefile
-------------------------------------
%.o : %.cpp
g++ -g -c $< -o $@
all: t t2 rmXX
% : %.o
g++ $< -o $@
rmXX:
echo "rmXX"
rm *.o
--------------------------------
同目录头文件:
hTestSTL.h
---------------------------------
#include<iostream>
using namespace std;
class A
{
private:
int a;
int b;
public:
int aPlusB()
{
cout<<"a:"<<a<<endl;
cout<<"b:"<<b<<endl;
cout<<"b+b:"<<a+b<<endl;
return a+b;
};
void setA (int a)
{
this->a=a;
};
void setB(int b)
{
this->b=b;
};
}; class NodeInfo
{
private:
int beanId;
int value;
public:
NodeInfo()
{
cout<<"init bean"<<endl;
};
void setBeanId(int id)
{
this->beanId=id;
};
void setValue(int value)
{
this->value=value;
};
int getValue()
{
return this->value;
};
int getBeanId()
{
return this->beanId;
}; };
----------------------------
练习1
t1.cpp 队列容器
-----------------------------
#include<iostream>
#include "hTestSTL.h"
#include<queue> using namespace std; queue<A> que;
void showHowUsingQueue()
{
A a;
A *p=&a;
a.setA();
a.setB();
a.aPlusB();
que.push(*p); a.setA();
a.setB();
a.aPlusB();
que.push(*p);
int tmp=;
A *tmp_p=; while(!que.empty())
{
que.pop();
tmp_p=&que.front();
tmp_p->aPlusB();
}
cout<<tmp;
};
int main(int c,char * argv[])
{ showHowUsingQueue();
cout<< "hello"<<endl; A a;
a.setA();
a.setB();
a.aPlusB();
int b ;
cin >> b;
return ;
};
---------------------
练习2 t2.cpp 访函数 迭代 遍历
------------------------
#include<iostream>
#include "hTestSTL.h"
#include<list>
#include<iterator>
#include<algorithm> using namespace std; void print( NodeInfo &elem)
{
cout<<elem.getValue()<<endl;
};
struct Printer
{
template<typename T> void operator()( T& t ) { cout<<"printer:"<<t.getValue()<<endl; }
}; struct Printer2
{
void operator()( NodeInfo & t ) { cout<<"printer2:"<<t.getValue()<<endl; }
};
void showHowUsingList()
{
list<NodeInfo> beanList;
NodeInfo bean;
for(int i=;i<;i++)
{
bean.setValue(i);
beanList.push_back(bean);
}
for_each(beanList.begin(), beanList.end(), print) ;
cout<<"test2"<<endl;
for_each(beanList.begin(), beanList.end(), Printer()) ;
cout<<"test3"<<endl;
for_each(beanList.begin(), beanList.end(), Printer2()) ;
}; int main(int c,char * argv[])
{
showHowUsingList();
return ;
};
STL 练习的更多相关文章
- 详细解说 STL 排序(Sort)
0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...
- STL标准模板库(简介)
标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...
- STL的std::find和std::find_if
std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...
- STL: unordered_map 自定义键值使用
使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...
- C++ STL简述
前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...
- codevs 1285 二叉查找树STL基本用法
C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- STL sort 函数实现详解
作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...
- STL的使用
Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...
- [C/C++] C/C++延伸学习系列之STL及Boost库概述
想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...
随机推荐
- Java环境搭建---(基础)
首先下载eclipse开发工具,下载地址:http://www.eclipse.org/downloads/,界面如下: 选择eclipse juno(4.2)的版本进入界面 点击Downloads, ...
- $.ajaxComplete()
https://www.cnblogs.com/RachelChen/p/5433881.html 全局ajax事件 必须当页面上存在任何ajax请求的时候都将触发这些特定的全局ajax处理函数. ...
- ZOJ 3521 Fairy Wars oj错误题目,计算几何,尺取法,排序二叉树,并查集 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3521 ATTENTION:如果用long long 减小误差,这道题只能用 ...
- hdu4453
题解: splay模板 删除,翻转等等 代码: #include<cstdio> #include<cstring> #include<cmath> #includ ...
- 将window上的项目上传到自己的github
使用git 1.首先在自己的github上面新建仓库 2.记下远程仓库的地址 3.在要上传的项目的目录下使用git命令进行上传 (1)先git init 初始化本地的仓库 (2)git add -A ...
- flowable ProcessEngine和ProcessEngineConfiguration
ProcessEngine是流程引擎,ProcessEngineConfiguration与前面四个引擎配置有些不同. ProcessEngineConfiguration增加了邮件服务和httpCl ...
- Java API访问ZK的权限控制
无权限访问结点 /** * 对于ZK的授权访问 * Created by liuhuichao on 2017/7/27. */ public class AutoSample { private s ...
- Vim技能修炼教程(9) - 缓冲区和标签页
缓冲区 上一节我们学习了窗口相关的命令,其实多窗口的最大功能在于存放多个不同的缓冲区. 文件载入内存之后,我们操作的其实是缓冲区.:write命令就是将缓冲区写回文件的目的. 查看内存中的缓冲区 通过 ...
- 【剑指offer】04A二维数组中的查找,C++实现
1.题目 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数数组和一个整数,判断数组中是否含有该整数. 2.思路 首先选取数 ...
- xgboost系列之应用xgboost的注意事项
问题:win和mac平台运行相同的xgboost代码,效果不同? 解决:xgboost的参数colsample_bytree设置为1.