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. MD5加密(java和c#)

    java代码 public static String md5(String str) { try { MessageDigest md = MessageDigest.getInstance(&qu ...

  2. UI-基本控件的简单使用

    1.IBAction:   //====================== 1> 能保证方法可以连线 2> 相当于void 2.IBOutlet: 1> 能保证属性可以连线 3.常 ...

  3. git checkout 报错 refname 'origin/branch-name' is ambiguous

    When this happened, it created the file .git/refs/heads/origin/branch-name. So, I just deleted the f ...

  4. c# 系统校时工具类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HuaT ...

  5. Android程序员学WEB前端(8)-CSS(3)-盒子内联块级定位浮动-Sublime

    转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76618473 觉得博文有用,请点赞,请评论,请关注,谢谢!~ 盒子模型: <!D ...

  6. Java调用本地方法又是怎么一回事

    JNI JNI即Java Native Interface,它能在Java层实现对本地方法的调用,一般本地的实现语言主要是C/C++,其实从虚拟机层面来看JNI挺好理解,JVM主要使用C/C++ 和少 ...

  7. Android深入理解JNI(二)类型转换、方法签名和JNIEnv

    相关文章 Android深入理解JNI系列 前言 上一篇文章介绍了JNI的基本原理和注册,这一篇接着带领大家来学习JNI的数据类型转换.方法签名和JNIEnv. 1.数据类型的转换 首先给出上一篇文章 ...

  8. 如何制作dll库的API文档,自动生成微软风格的chm文件 Sandcastle Help File Builder 使用方法

    当你开发了一个库的时候,就需要给库开发一个api文档,微软提供了一个C#库的自动生成工具.我在使用的过程中记录了相关的信息,以供大家学习和查阅,如有不正之处,欢迎指出. 首先先下载一个软件,下载地址在 ...

  9. U-Boot 不能识别FAT32 SD分区

    /********************************************************************* * U-Boot 不能识别FAT32 SD分区 * 说明: ...

  10. Xcode 8 修改项目名

    很麻烦,不想修改了 有个教程,贴一下. From: 大发写字的地方 Xcode8 修改包名(含cocopods)