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

你可能感兴趣的:(it,C语言,each,Exchange,Components)