stl源码分析de练习
// 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;
}
作 者: itdef
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
B站算法视频题解
https://space.bilibili.com/18508846
qq 151435887
gitee https://gitee.com/def/
欢迎c c++ 算法爱好者 windows驱动爱好者 服务器程序员沟通交流
如果觉得不错,欢迎点赞,你的鼓励就是我的动力
浙公网安备 33010602011771号