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

using namespace std;

 

class alloc

{

};

 

template

class deque

{

public:

       deque()

       {

              cout << "deque\n";

       }

};

 

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

template

class stack;

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

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

template

bool operator == (const stack &x, const stack &y);

 

template>

class stack

{

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

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

       friend bool operator== (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

bool operator== (const stack &x, const stack &y)

{

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

}

 

int main()

{

       stack x;

       stack y;

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

 

       // stack y1;

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

       // if add:

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

 

       return 0;

}

 

/*

Output:

deque

stack

deque

stack

operator==   1

*/

Configuration_add_2

Code

Review

#include

using namespace std;

 

#define _STL_TEMPLATE_NULL template<>

// "#define _STL_TEMPLATE_NULL" is wrong:

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

 

template

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

{

       void operator() ()

       {

              cout << "Hash\n";

       }

};

 

_STL_TEMPLATE_NULL

struct Hash

{

       void operator()()

       {

              cout << "Hash\n";

       }

};

 

_STL_TEMPLATE_NULL

struct Hash

{

       void operator()()

       {

              cout << "Hash\n";

       }

};

 

int main()

{

       Hash obj1;

       Hash obj2;

       Hash obj3;

 

       obj1();

       obj2();

       obj3();

 

       return 0;

}

/*

output:

Hash

Hash

Hash

*/

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

#include

#include

using namespace std;

 

template

class print

{

public:

       void operator() (const T &elem)

       {

              cout << elem << " ";

       }

};

 

int main()

{

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

       vector iv(num, num + 6);

       // print is a temporary object

       for_each(iv.begin(), iv.end(), print());  // 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

using namespace std;

 

template

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_num << endl;

       cout << TestClass::long_num << endl;

       cout << TestClass::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

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

你可能感兴趣的:(STL)