Chapter 1-03

Please indicate the source: http://blog.csdn.net/gaoxiangnumber1

Welcome to my github: https://github.com/gaoxiangnumber1

Configuration_add_1

Code

Review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@function: test __STL_NULL_TMPL_ARGS(bound friend template friend)

*/

 

#include<iostream>

using namespace std;

 

class alloc

{

};

 

template<class T, class Alloc = alloc, size_t BufSiz = 0>

class deque

{

public:

       deque()

       {

              cout << "deque\n";

       }

};

 

// class declaration; otherwise "error: ‘stack’ does not name a type"

template<class T, class Sequence>

class stack;

// function declaration, otherwise "error: template-id ‘operator==<int>’ for

// ‘bool operator==(...)’ does not match any template declaration"

template<class T, class Sequence>

bool operator == (const stack<T, Sequence> &x, const stack<T, Sequence> &y);

 

template<class T, class Sequence = deque<T>>

class stack

{

       // the following 3 friend function declaration are ok; the fourth is wrong.

       friend bool operator== <T> (const stack<T>&, const stack<T>&);

       friend bool operator== <T> (const stack&, const stack&);

       friend bool operator== <> (const stack&, const stack&);

       //friend bool operator== (const stack&, const stack&);

       // if add the 4th:

       // warning: friend declaration ‘bool operator==(...)’ declares a non-template function

       // error: undefined reference to `operator==(...> const&)

public:

       stack()

       {

              cout << "stack\n";

       }

 

private:

       Sequence c;

};

 

template<class T, class Sequence>

bool operator== (const stack<T, Sequence> &x, const stack<T, Sequence> &y)

{

       return cout << "operator==\t";

}

 

int main()

{

       stack<int> x;

       stack<int> y;

       cout << (x == y) << endl;

 

       // stack<char> y1;

       // cout << (x == y1) << endl;

       // if add:

       // error: no match for ‘operator==’ (operand types are ‘stack<int>’ and ‘stack<char>’)

 

       return 0;

}

 

/*

Output:

deque

stack

deque

stack

operator==   1

*/

Configuration_add_2

Code

Review

#include<iostream>

using namespace std;

 

#define _STL_TEMPLATE_NULL template<>

// "#define _STL_TEMPLATE_NULL" is wrong:

// error: an explicit specialization must be preceded by ‘template <>’

 

template<class T>

struct Hash // don't use "hash" bacause it is defined in system codes

{

       void operator() ()

       {

              cout << "Hash<T>\n";

       }

};

 

_STL_TEMPLATE_NULL

struct Hash<char>

{

       void operator()()

       {

              cout << "Hash<char>\n";

       }

};

 

_STL_TEMPLATE_NULL

struct Hash<unsigned char>

{

       void operator()()

       {

              cout << "Hash<unsigned char>\n";

       }

};

 

int main()

{

       Hash<long> obj1;

       Hash<char> obj2;

       Hash<unsigned char> obj3;

 

       obj1();

       obj2();

       obj3();

 

       return 0;

}

/*

output:

Hash<T>

Hash<char>

Hash<unsigned char>

*/

1.9.2

temporary_object.cc

code

review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@function: test temporary object

*/

 

#include<vector>

#include<algorithm>

#include<iostream>

using namespace std;

 

template<class T>

class print

{

public:

       void operator() (const T &elem)

       {

              cout << elem << " ";

       }

};

 

int main()

{

       int num[6] = {1, 2, 3, 4, 5, 6};

       vector<int> iv(num, num + 6);

       // print<int> is a temporary object

       for_each(iv.begin(), iv.end(), print<int>());  // output: 1 2 3 4 5 6.

 

       return 0;

}

1.9.3

inclass_init.cc

code

review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@function: test in-class initialization of static const integral members(char, short, int ...)

*/

 

#include<iostream>

using namespace std;

 

template<class T>

class TestClass

{

public:

       static const int int_num = 5;

       static const long long_num = 3L;

       static const char char_num = 'c';

};

 

int main()

{

       cout << TestClass<int>::int_num << endl;

       cout << TestClass<int>::long_num << endl;

       cout << TestClass<int>::char_num << endl;

 

       return 0;

}

 

/*

Output:

5

3

c

*/

1.9.4

operator_overloading.cc

code

review

/*

Welcome to the GX_STL!

@author GaoXiang

@blog http://blog.csdn.net/gaoxiangnumber1

@github https://github.com/gaoxiangnumber1

@function: operator overloading

*/

 

#include<iostream>

using namespace std;

 

class Number

{

       friend ostream& operator<< (ostream &os, const Number &obj);

 

public:

       Number(int data): data_(data) {}

 

       // prefix : increment and then fetch

       Number& operator++()

       {

              ++(this->data_);

              return *this;

       }

       // postfix: fetch and then increment

       const Number operator++(int)

       {

              Number temp = *this;

              ++(*this);

              return temp;

       }

 

       // prefix : decrement and then fetch

       Number& operator--()

       {

              --(this->data_);

              return *this;

       }

       // postfix: fetch and then decrement

       const Number operator--(int)

       {

              Number temp = *this;

              --(*this);

              return temp;

       }

 

       // dereference

       int& operator*() const

       {

              return (int&)data_;     // convert "const int" to "non-const lvalue" explicitly

       }

 

private:

       int data_;

};

 

ostream& operator<<(ostream& os, const Number &obj)

{

       os << "[" << obj.data_ << "]\n";

       return os;

}

 

int main()

{

       Number number(5);

 

       cout << number++;

       cout << ++number;

       cout << number--;

       cout << --number;

       cout << *number;

 

       return 0;

}

 

/*

Output:

[5]

[7]

[7]

[5]

5

*/

1.9.5 前闭后开区间表示法 [ )

l  任何一个STL算法,都需要获得由一对迭代器(泛型指标)所标示的区间,用以表示操作范围。这一对迭代器标示的是前闭后开区间,[first, last)表示。整个实际范围从first开始,直到last-1。迭代器last所指的是[最后一个元素的下一位置]

Please indicate the source: http://blog.csdn.net/gaoxiangnumber1

Welcome to my github: https://github.com/gaoxiangnumber1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值