// StlTest1.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream> using namespace std; template <typename T>
class print {
public:
void operator()(const T& elem) {
cout << elem << ' ';
}
}; void TmpObjectTest() {
int ia[6] = { 0,1,2,3,4,5 };
vector<int> iv(ia, ia+6);
for_each(iv.begin(), iv.end(), print<int>());
std::cout << std::endl;
}
//=================================================
template <typename T>
class testClass {
public:
static const int _datai = 5;
static const long _datal = 3L;
static const char _datac = 'c';
}; void InClassInit() {
std::cout << testClass<int>::_datai << std::endl;
std::cout << testClass<int>::_datal << std::endl;
std::cout << testClass<int>::_datac << std::endl;
}
//==============================================
class INT {
friend ostream& operator<<(ostream& os, const INT& i);
public:
INT(int i) :m_i(i) {};
INT& operator++() {
++(this->m_i);
return *this;
} const INT operator++(int) {
INT temp = *this;
++(*this);
return temp;
} INT& operator--() {
--(this->m_i);
return *this;
} const INT operator--(int) {
INT temp = *this;
--(*this);
return temp;
} int& operator*() const {
return (int&)m_i;
} private:
int m_i;
}; ostream& operator<<(ostream& os, const INT& i) {
os << "[" << i.m_i << "]";
return os;
} void OperatorOverloading() {
INT I(5);
cout << I++;
cout << ++I;
cout << I--;
cout << --I;
cout << *I;
} //==================================================
int main()
{
TmpObjectTest();
InClassInit();
OperatorOverloading();
return 0;
}

  

// StlTest.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
//测试 class template 拥有 static data members。 #include <iostream>
using namespace std; template <typename T>
class testClass {
public:
static int _data;
static T _td;
}; int testClass<int>::_data = 1;
int testClass<int>::_td = 2;
char testClass<char>::_td = 66; void StaticTemplateMemberTest() {
std::cout << testClass<int>::_data << std::endl;
std::cout << testClass<int>::_td << std::endl;
std::cout << testClass<char>::_td << std::endl;
} //==========================================
template <class I,class O>
struct testClass1 {
testClass1() { std::cout << "I,O" << std::endl; }
}; template <class T>
struct testClass1<T*, T*> {
testClass1() { std::cout << "T*,T*" << std::endl; }
}; template <class T>
struct testClass1<const T*, T*> {
testClass1() { std::cout << "const T*,T*" << std::endl; }
}; void ClassPartialSpecTest() {
testClass1<int, char> obj1;
testClass1<int*, int*> obj2;
testClass1<const int*, int*> obj3;
}
//============================================
class alloc1 {}; template <class T,class Alloc=alloc1>
class vector1 {
public:
void swap1(vector1<T, Alloc>&) { cout << "swap()" << endl; }
}; template <class T,class Alloc1>
void swap1(vector1<T, Alloc1>& x, vector1<T, Alloc1>& y) {
x.swap1(y);
} void FuncTmplPartialOrder() {
vector1<int> x, y;
swap1(x, y);
}
//=============================================
class alloc2{}; template <class T,class Alloc2=alloc2>
class vector2 {
public:
typedef T value_type;
typedef value_type* iterator; template <class I>
void insert(iterator position, I first, I last) {
std::cout << "insert()" << std::endl;
}
}; void MemberTemplates() {
int ia[5] = { 0,1,2,3,4 };
vector2<int> x;
vector2<int>::iterator ite = NULL;
x.insert(ite, ia, ia + 5);
}
//=============================================
class alloc3{}; template<class T,class Alloc3 = alloc3,size_t BufSiz = 0>
class Deque3 {
public:
Deque3() { cout << "deque" << endl; }
}; template <class T, class Sequence = Deque3<T>>
class stack3 {
public:
stack3() { cout << "stack" << endl; }
private:
Sequence c;
}; void LimitDefaultTemplate() {
stack3<int> x;
}
//============================================
class alloc4 {}; size_t __deque_buf_size(size_t n, size_t sz) {
return n != 0 ? n : (sz < 512 ? size_t(512 / sz) : size_t(1));
} template <class T,class Ref,class Ptr,size_t BufSiz>
struct __deque_iterator {
typedef __deque_iterator<T, T&, T*, BufSiz> iterator;
typedef __deque_iterator<T, const T&, const T*, BufSiz> const_iterator;
static size_t buffer_size() { return __deque_buf_size(BufSiz, sizeof(T)); }
}; template <class T,class Alloc4 = alloc4,size_t BufSiz = 0>
class deque4 {
public:
typedef __deque_iterator<T, T&, T*, BufSiz> iterator;
}; void NonTypeTmplParamBug() {
cout << deque4<int>::iterator::buffer_size() << endl;
cout << deque4<int, alloc4, 64>::iterator::buffer_size() << endl;
} //=============================================
int main()
{
StaticTemplateMemberTest();
ClassPartialSpecTest();
FuncTmplPartialOrder();
MemberTemplates();
LimitDefaultTemplate();
NonTypeTmplParamBug();
return 0;
}

  

最新文章

  1. js实现图片预加载
  2. 一步一步教你elasticsearch在windows下的安装
  3. keepalived和heartbeat区别
  4. struts2中一些常用的写法 记录
  5. Libfilth(一个滤波器C库)使用
  6. K需要修改的内容
  7. iOS 获取当前城市
  8. PHP学习笔记 - 进阶篇(6)
  9. Linux samba服务器设置简单匿名共享
  10. 许多js框架或js库的min版本是怎么做出来的?
  11. 构建纯TypeScript应用
  12. 执行Python出现LookupError: unknown encoding: cp65001解决办法
  13. BigDecimal 准确的 double , float 计算
  14. oracle权限列表
  15. 关于Git HEAD^与HEAD~的关系
  16. 使用latex撰写博士,硕士学位论文(浙大博士经验分享)
  17. java websocket @ServerEndpoint注解说明
  18. 用logger在控制台打印信息
  19. 阿里巴巴Java开发规约插件使用
  20. ubuntu snmp Error: unknown payload OID

热门文章

  1. Python ---- list和dict遍历
  2. TortoiseSVN 结合使用哪个问题跟踪系统比较好?TRAC?REDMINE?都有什么优缺点?
  3. Java9的新特性
  4. linux下tengine安装
  5. VS2010 C++环境下DLL和LIB文件的生成与调试 备忘
  6. phpstorm破解 IntelliJ IDEA License Server本地搭建教程 http://blog.lanyus.com/archives/174.html/comment-page-6#comments 附件:mac环境
  7. Java前期(静态)绑定和后期(动态)绑定
  8. zoj-3410-Layton&#39;s Escape
  9. python twilio 短信群发 知识留存
  10. uwsgi的使用