Please indicate the source: http://blog.csdn.net/gaoxiangnumber1
Welcome to my github: https://github.com/gaoxiangnumber1
/*
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==
// ‘bool operator==(...)’ does not match any template declaration"
template
bool operator == (const stack
template
class stack
{
// the following 3 friend function declaration are ok; the fourth is wrong.
friend bool operator==
friend bool operator==
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
{
return cout << "operator==\t";
}
int main()
{
stack
stack
cout << (x == y) << endl;
// stack
// cout << (x == y1) << endl;
// if add:
// error: no match for ‘operator==’ (operand types are ‘stack
return 0;
}
/*
Output:
deque
stack
deque
stack
operator== 1
*/
#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
}
};
_STL_TEMPLATE_NULL
struct Hash
{
void operator()()
{
cout << "Hash
}
};
_STL_TEMPLATE_NULL
struct Hash
{
void operator()()
{
cout << "Hash
}
};
int main()
{
Hash
Hash
Hash
obj1();
obj2();
obj3();
return 0;
}
/*
output:
Hash
Hash
Hash
*/
/*
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
// print
for_each(iv.begin(), iv.end(), print
return 0;
}
/*
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
cout << TestClass
cout << TestClass
return 0;
}
/*
Output:
5
3
c
*/
/*
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