Chapter 1-02
来源:互联网 发布:python 高级用法 编辑:程序博客网 时间:2024/05/16 09:34
Please indicate the source:http://blog.csdn.net/gaoxiangnumber1
Welcome to my github:https://github.com/gaoxiangnumber1.
1.9.1
configuration_3.cc
Code Review
/*
Welcome to the GX_STL!
@author GaoXiang
@blog http://blog.csdn.net/gaoxiangnumber1
@github https://github.com/gaoxiangnumber1
@e-mail gaoxiangnumber1@126.com
@function: test __STL_STATIC_TEMPLATE_MEMBER_BUG, defined in <stl_config.h>
*/
#include<iostream>
using namespace std;
template<class T>
class TestClass
{
public: // to test conveniently
static int data_;
};
// allocate memory for static data members and assign initial value
template<>
// if we omit "template<>":error: specializing member ‘TestClass<int>::data_’ requires ‘template<>’ syntax
int TestClass<int>::data_ = 1;
template<>
int TestClass<char>::data_ = 2;
int main()
{
cout << TestClass<int>::data_ << endl; // should be 1
cout << TestClass<char>::data_ << endl; // should be 2
TestClass<int> obj_int1, obj_int2;
TestClass<char> obj_char1, obj_char2;
cout << obj_int1.data_ << endl; // should be 1
cout << obj_int2.data_ << endl; // should be 1
cout << obj_char1.data_ << endl; // should be 2
cout << obj_char2.data_ << endl; // should be 2
obj_int1.data_ = 3;
obj_char2.data_ = 4;
cout << obj_int1.data_ << endl; // should be 3
cout << obj_int2.data_ << endl; // should be 3
cout << obj_char1.data_ << endl; // should be 4
cout << obj_char2.data_ << endl; // should be 4
return 0;
}
/*
The output:
1
2
1
1
2
2
3
3
4
4
*/
Configuration_5.cc
Code Review
/*
Welcome to the GX_STL!
@author GaoXiang
@blog http://blog.csdn.net/gaoxiangnumber1
@github https://github.com/gaoxiangnumber1
@e-mail gaoxiangnumber1@126.com
@function: test class template partial specialization
( <C++ Primer> 876: except the general design for class
template, specialize some design for some template arguments). That is, test
__STL_CLASS_PARTIAL_SPECIALIZATION in <stl_config.h>
*/
#include<iostream>
using namespace std;
// general design
template<class I, class O>
struct TestClass
{
TestClass()
{
cout << "I, O\n";
}
};
// class template partial specializations
template<class T>
struct TestClass<T*, T*>
{
TestClass()
{
cout << "T*, T*\n";
}
};
// class template partial specializations
template<class T>
struct TestClass<const T*, T*>
{
TestClass()
{
cout << "const T*, T*\n";
}
};
int main()
{
TestClass<int, char> object1; // output "I, O"
TestClass<int*, int*> object2; // output "T*, T*"
TestClass<const int*, int*> object3; // output "const T*, T*"
return 0;
}
/*
Output:
I, O
T*, T*
const T*, T*
*/
Configuration_8.cc
Code Review
/*
Welcome to the GX_STL!
@author GaoXiang
@blog http://blog.csdn.net/gaoxiangnumber1
@github https://github.com/gaoxiangnumber1
@e-mail gaoxiangnumber1@126.com
@function: test __STL_MEMBER_TEMPLATES in <stl_config.h>
*/
#include<iostream>
using namespace std;
class alloc
{
};
template<class T, class Alloc = alloc>
class vector
{
public:
typedef T value_type;
typedef value_type* iterator;
template<class I>
void insert(iterator position, I first, I last)
{
cout << "insert()\n";
}
};
int main()
{
int test[] = {0, 1, 2, 3, 4};
vector<int> x;
vector<int>::iterator ite;
x.insert(ite, test, test + 5); // output "insert()"
return 0;
}
Configuration_10.cc
code review
/*
Welcome to the GX_STL!
@author GaoXiang
@blog http://blog.csdn.net/gaoxiangnumber1
@github https://github.com/gaoxiangnumber1
@e-mail gaoxiangnumber1@126.com
@function: test __STL_LIMITED_DEFAULT_TEMPLATES in <stl_config.h>
*/
#include<iostream>
using namespace std;
class alloc
{
};
template<class T, class Alloc = alloc, size_t BufSiz = 0>
class deque
{
public:
deque()
{
cout << "deque\n";
}
};
// according previous parameter value "T", set next parameter Sequence's
// default value as "deque<T>"
template<class T, class Sequence = deque<T> >
class stack
{
public:
stack()
{
cout << "stack\n";
}
private:
Sequence c;
};
int main()
{
stack<int> s; // output "deque\nstack"
return 0;
}
Configuration_11.cc
Code
Review
/*
Welcome to the GX_STL!
@author GaoXiang
@blog http://blog.csdn.net/gaoxiangnumber1
@github https://github.com/gaoxiangnumber1
@function: test __STL_NON_TYPE_TMPL_PARAM_BUG in <stl_config.h>
*/
#include<iostream>
using namespace std;
class alloc
{
};
inline 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() // we can call static member function without object!
{
return deque_buf_size(BufSiz, sizeof(T));
}
};
template<class T, class Alloc = alloc, size_t Buf_Siz = 0>
class deque
{
public:
typedef deque_iterator<T, T&, T*, Buf_Siz> iterator;
};
int main()
{
// we CAN call static member function without object!!!
cout << deque<int>::iterator::buffer_size() << endl; // 128
cout << deque<int, alloc, 64>::iterator::buffer_size() << endl; // 64
return 0;
}
Welcome to my github:https://github.com/gaoxiangnumber1.
Please indicate the source:http://blog.csdn.net/gaoxiangnumber1.
- Chapter 1-02
- chapter 1
- Chapter 1 >.<
- chapter 1
- chapter 1
- chapter 1
- Chapter 1
- CHAPTER 1
- Chapter 1
- Chapter 1
- C和指针(Chapter 1 & Chapter 2)
- CHAPTER 10:EX 02
- Chapter 2-02
- Chapter 3-02
- Chapter 4-02
- Chapter 5-02
- Chapter 6-02
- Chapter 7-02
- poj2112 Optimal Milking
- linux下安装php7+mysql5.7+nginx
- 蓝桥基础练习学习记录
- Chapter 1-01
- java web学习(进阶篇)九 Spring MVC
- Chapter 1-02
- Java基础 数列排序
- CodeForces 119A Epic Game
- 书
- cocos2d-x中级知识(一)——数据容器类
- UVA 1218 带状态判断的树形Dp
- poj 1163 The Triangle
- Codeforces Round #342 (Div. 2)-A. Guest From the Past(数学)
- mvc多个文件上传和单个文件上传