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 练习的更多相关文章

  1. 详细解说 STL 排序(Sort)

    0 前言: STL,为什么你必须掌握 对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算 ...

  2. STL标准模板库(简介)

    标准模板库(STL,Standard Template Library)是C++标准库的重要组成部分,包含了诸多在计算机科学领域里所常见的基本数据结构和基本算法,为广大C++程序员提供了一个可扩展的应 ...

  3. STL的std::find和std::find_if

    std::find是用来查找容器元素算法,但是它只能查找容器元素为基本数据类型,如果想要查找类类型,应该使用find_if. 小例子: #include "stdafx.h" #i ...

  4. STL: unordered_map 自定义键值使用

    使用Windows下 RECT 类型做unordered_map 键值 1. Hash 函数 计算自定义类型的hash值. struct hash_RECT { size_t operator()(c ...

  5. C++ STL简述

    前言 最近要找工作,免不得要有一番笔试,今年好像突然就都流行在线笔试了,真是搞的我一塌糊涂.有的公司呢,不支持Python,Java我也不会,C有些数据结构又有些复杂,所以是时候把STL再看一遍了-不 ...

  6. codevs 1285 二叉查找树STL基本用法

    C++STL库的set就是一个二叉查找树,并且支持结构体. 在写结构体式的二叉查找树时,需要在结构体里面定义操作符 < ,因为需要比较. set经常会用到迭代器,这里说明一下迭代器:可以类似的把 ...

  7. STL bind1st bind2nd详解

    STL bind1st bind2nd详解   先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...

  8. STL sort 函数实现详解

    作者:fengcc 原创作品 转载请注明出处 前几天阿里电话一面,被问到STL中sort函数的实现.以前没有仔细探究过,听人说是快速排序,于是回答说用快速排序实现的,但听电话另一端面试官的声音,感觉不 ...

  9. STL的使用

    Vector:不定长数组 Vector是C++里的不定长数组,相比传统数组vector主要更灵活,便于节省空间,邻接表的实现等.而且它在STL中时间效率也很高效:几乎与数组不相上下. #include ...

  10. [C/C++] C/C++延伸学习系列之STL及Boost库概述

    想要彻底搞懂C++是很难的,或许是不太现实的.但是不积硅步,无以至千里,所以抽时间来坚持学习一点,总结一点,多多锻炼几次,相信总有一天我们会变得"了解"C++. 1. C++标准库 ...

随机推荐

  1. 【转】Fork/Join框架测试

    Fork/Join框架介绍 下面使用该框架计算0-50000000000的和,并比较普通计算方法.Fork/Join框架.Java8新特性三种计算方式的计算时间: import java.time.D ...

  2. CF 1A

    题解: n/a*m/a 都要向上取整 代码: #include<cstdio> #include<cmath> #include<cstring> #include ...

  3. Shell脚本编写规范

    shell脚本需要有较高的实用性.可维护.可阅读.方便他人阅读,因而需要建立一定的规范来操作 dream361@master:~$ cat test2.sh #!/bin/bash 所使用的bash程 ...

  4. Yii2学习笔记:汉化yii,设置表单的描述(属性标签attributeLabels)

    一:汉化框架 框架汉化在这里设置,如果不生效,前台后台的配置文件都设置下应该就可以了 二:汉化表单 汉化表单,直接在模型层设置,例如: 原来的联系我们表单 汉化后: ] 这种汉化在哪里修改呢?其实是设 ...

  5. iOS开发探索-高斯模糊&毛玻璃效果

    iOS开发中有的时候需要将图片设置模糊,来实现特定的效果获取更好的用户体验, iOS7之后半透明模糊效果得到大范围使用的比较大,现在也可以看到很多应用局部用到了图片模糊效果,可以通过高斯模糊和毛玻璃效 ...

  6. jmeter测试MySQL数据库

    前提:安装好MySQL数据库,并且下载了jmeter安装包 1.打开jmeter 2.下载mysql-connector-java.jar,将jar包默认复制到jmeter的bin目录下面,因为默认打 ...

  7. L146 Space Station Hole Cause Will Be Determined

    The head of the U.S. space agency said Tuesday he's sure that investigators will determine the cause ...

  8. Talk to customer about the trouble with wireless failure connection。

    It’s upset for  me... 1 ATemel Studio61 and  MKII program failure. Notes: The Flash file : A 90 is w ...

  9. 浅析C#中ASP.NET页面的生存周期

    一般来说,页要经历下表概述的各个阶段.除了页生命周期阶段以外,在请求前后还存在应用程序阶段,但是这些阶段并不特定于页. 阶段 说明 页请求 页请求发生在页生命周期开始之前.用户请求页时,ASP.NET ...

  10. d3.js(v5.7)的node与数据匹配(自动匹配扩展函数)

    在d3操作时,当然少不了对已有节点绑定数据,那么问题就来了,节点个数和数据长度不一样的,怎么办. d3在节点少于数据长度的时候,有enter().appen()方法实现node的增加: 在节点大于数据 ...