STL指示器

// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <windows.h>
using namespace std;
/**********************************************************************
STL指示器
可以反复的对STL容器的内容进行访问,STL共定义了5种类型的指示器
1、输入指示器
2、输出指示器
3、前向指示器
4、双向指示器
5、随机访问指示器
**********************************************************************/

//输入指示器 --- 程序使用输入指示器只能读取容器的内容
int main(int argc, char* argv[])
{
 vector<int> intvector;
 //填充向量
 for(int i=0; i<10; i++)
  intvector.push_back(i*10);
 //显示向量内容
 vector<int>::iterator iter;//iter为指示器
 for(iter=intvector.begin(); iter!=intvector.end();iter++)
  cout<<*iter<<"  ";
 cout<<endl;

 return 0;
}


//输出指示器
int main()
{
 vector<int> intvector(5);
 //填充向量
 vector<int>::iterator iter = intvector.begin();
 *iter++ = 10;
 *iter++ = 15;
 *iter++ = 20;
 *iter++ = 25;
 *iter = 30;
 //显示向量内容
 vector<int>::iterator it = intvector.begin();
 while(it != intvector.end())
 {
  cout<<*it<<endl;
  ++it;
 }
 cout<<endl;
 return 0;
}

//前向指示器---既可读也可写
int main()
{
 vector<int> intvector(5);
 //初始化指示器
 vector<int>::iterator it = intvector.begin();
 vector<int>::iterator saveit = it;
 *it++ = 10;
 *it++ = 15;
 *it++ = 20;
 *it++ = 25;
 *it = 30;
 //显示向量内容
 while(saveit != intvector.end())
 {
  cout<<*saveit<<"  ";
  saveit++;
 }
 cout<<endl;

 return 0;
}


//爽向指示器
int main()
{
 vector<int> intvector(5);
 vector<int>::iterator it = intvector.begin();
 vector<int>::iterator saveit = it;
 //填充向量
 *it++ = 10;
 *it++ = 15;
 *it++ = 20;
 *it++ = 25;
 *it = 30;
 //显示向量内容
 while(saveit != intvector.end())
 {
  cout<<*saveit<<"  ";
  saveit++;
 }
 cout<<endl;
 //反向显示向量内容
 do{
  --saveit;
  cout<<*saveit<<"  ";
 }while(saveit != intvector.begin());

 return 0;
}

//随机指示器
int main()
{
 vector<int> intvector(5);
 vector<int>::iterator it = intvector.begin();
 vector<int>::iterator saveit = it;
 //填充向量
 *it++ = 10;
 *it++ = 15;
 *it++ = 20;
 *it++ = 25;
 *it = 30;
 //更改第三个元素的内容
 it -= 2;
 *it = 100;
 //显示向量内容
 while(saveit != intvector.end())
 {
  cout<<*saveit<<"  ";
  saveit++;
 }
 cout<<endl;
 //反向显示响亮的内容
 saveit -= 1;
 while(saveit >= intvector.begin())
 {
  cout<<*saveit<<"  ";
  saveit--;
 }
 cout<<endl;
 return 0;
}

//特殊用途的指示器
//输入流指示器---istream_iterator<char> (strm)
void show_val(int val)
{
 cout<<val<<"  ";
}

int main()
{
 vector<int> intvector;
 //填充向量
 for(int i=0;i<5;i++)
  intvector.push_back(*
  istream_iterator<int> (cin));
 //显示向量内容
 for_each(intvector.begin(),intvector.end(),show_val);
 cout<<endl;
 return 0;
}


//输出流指示器---ostream_iterator<char> (strm)
void show_val(string val)
{
 cout<<val<<"  ";
}

int main()
{
 vector<string> strvector1;
 vector<string> strvector2;
 //用值填充向量
 strvector1.push_back("Zebra");
 strvector1.push_back("Deer");
 strvector1.push_back("Fish");
 strvector1.push_back("Snake");
 strvector1.push_back("Bat");

 strvector2.push_back("Cat");
 strvector2.push_back("Bird");
 strvector2.push_back("Turtle");
 strvector2.push_back("Horse");
 strvector2.push_back("Cow");

 //显示向量内容
 for_each(strvector1.begin(),strvector1.end(),show_val);
 cout<<endl;
 for_each(strvector2.begin(),strvector2.end(),show_val);

 //向量排序
 merge(strvector1.begin(),strvector1.end(),strvector2.begin(),strvector2.end(),std::ostream_iterator<std::string> (std::cout));
 cout<<endl;
 return 0;
}

//指示器适配器
//反向指示器---rbegin()  rend()
void show_val(string val)
{
 cout<<val<<endl;
}

int main()
{
 vector<string> strvector;
 //填充向量
 strvector.push_back("Zebra");
 strvector.push_back("Deer");
 strvector.push_back("Fish");
 strvector.push_back("Snake");
 strvector.push_back("Bat");

 //显示向量内容
 for_each(strvector.begin(),strvector.end(),show_val);
 cout<<endl;
 //反向显示向量内容
 for_each(strvector.rbegin(),strvector.rend(),show_val);
 cout<<endl;
 return 0;
}

//插入指示器
void show_val(int val)
{
 cout<<val<<endl;
}

int main()
{
 vector<int> intvector(5);
 std::back_insert_iterator<vector<int> > it(intvector);
 *it++ = 23;
 *it++ = 34;
 *it++ = 45;
 *it++ = 56;
 *it = 67;
 //显示向量内容
 for_each(intvector.begin(),intvector.end(),show_val);
 cout<<endl;
 return 0;
}


//类模板
template <class T1,class T2,int num=10>
class mytemp{
private:
 T1 t1;
 T2 t2;
public:
 mytemp(T1 tt1,T2 tt2)
 {
  t1 = tt1 + num;
  t2 = tt2 + num;
 }
 void dispaly()
 {
  cout<<"T1 "<<t1<<"      T2 "<<t2<<endl;
 }
};

int main()
{
 int a = 123;
 int c = 1000;
 double b = 456.789;
 mytemp<int,double> mt1(a,b);
 mytemp<int,double,100> mt2(a,b);

 mt1.dispaly();
 mt2.dispaly();

 return 0;
}


template <class T>
class custom{
public:
 custom();
 void start();
};

template<class T>
custom<T>::custom()
{
}

template <class T>
void custom<T>::start()
{
 T op;
 op.output();
}
/
class b{
public:
 void output()
 {
  cout<<"hello,this is class b"<<endl;
 }
};
//
class a{
public:
 output()
 {
  cout<<"hello,this is class a"<<endl;
 }
};
int main()
{
 custom<b> b1;
 b1.start();
 custom<a> a1;
 a1.start();
 return 0;
}


template<class T>
class temp{
 int data;
public:
 temp(int n){
  data = n;
 }
 void dispaly();
};

template<class T>
void temp<T>::dispaly()
{
 cout<<"data :"<<data<<endl;
}

int main()
{
 temp<int> a(9);
 a.dispaly();
 return 0;
}


int main()
{
 ::MessageBox (NULL,TEXT("hello,this is function of API, MessageBox()"),TEXT("MssageBox"),MB_OK);
 return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值