:
#include <cassert>
template< typename T >
const T * MyFind (const T * _pArray, int _nElements, const T & _value)
{
for (int i = 0; i < _nElements; i++)
if (_pArray[ i ] == _value)
return _pArray + i;
return nullptr;
}
int main ()
{
int data[ 5 ] = { 1, 2, 3, 4, 5 };
const int * pResult = MyFind(data, 5, 3);
assert(pResult && ((pResult - data) == 2));
}
. , , . , , .
, . . , , .
template< typename T >
class List
{
public:
struct Node
{
T m_value;
Node * m_pNextNode;
Node (const T& _value)
: m_value(_value),
m_pNextNode(nullptr)
{
}
};
private:
Node * m_pFirstNode, * m_pLastNode;
List (const List< T >&);
List< T > & operator = (const List< T > &);
public:
List ()
{
m_pFirstNode = m_pLastNode = nullptr;
}
~List ()
{
Node * pCurrent = m_pFirstNode;
while (pCurrent)
{
Node * pTemp = pCurrent->m_pNextNode;
delete pCurrent;
pCurrent = pTemp;
}
}
void push_back (const T & _value)
{
Node * pNewNode = new Node(_value);
if (m_pLastNode)
{
m_pLastNode->m_pNextNode = pNewNode;
m_pLastNode = pNewNode;
}
else
m_pFirstNode = m_pLastNode = pNewNode;
}
const Node * first_node () const
{
return m_pFirstNode;
}
};
template< typename T >
const typename List< T >::Node *
MyFind (const List< T > & _list, const T & _value)
{
const List< T >::Node* pCurrentNode = _list.first_node();
while (pCurrentNode)
{
if (pCurrentNode->m_value == _value)
return pCurrentNode;
pCurrentNode = pCurrentNode->m_pNextNode;
}
return nullptr;
}
int main ()
{
List < int > myList;
myList.push_back(1);
myList.push_back(2);
myList.push_back(3);
myList.push_back(4);
myList.push_back(5);
|
|
const List< int >::Node * pNode = MyFind(myList, 3);
assert(pNode && pNode->m_value == 3);
}
, , . .
, . -, , , . , , . , , , .
template< typename T >
const T * MyFind (const T * _pArrayFirst,
const T * _pArrayLast,
const T & _value)
{
const T * pCurrent = _pArrayFirst;
while (pCurrent!= _pArrayLast)
{
if (* pCurrent == _value)
return pCurrent;
}
return _pArrayLast;
}
:
int main ()
{
int data[ 5 ] = { 1, 2, 3, 4, 5 };
const int * pResult = MyFind(data, data + 5, 3);
assert(pResult && ((pResult - data) == 2));
}
, .
, . , , .
, . - , . (==,!=), (=), (++X), , (* x), . - .
template< typename It, typename T >
It MyFind (It _first, It _last, const T & _value)
{
It current = _first;
while (current!= _last)
{
if (* current == _value)
return current;
++ current;
}
return _last;
}
. , , .
, , . (generic concept) - , - . , , , . , , ( , , ).
|
|
. , .
, , , . :
template< typename T >
class List
{
public:
//...
struct Iterator
{
Node * m_pCurrentNode;
Iterator (Node * _pCurrentNode)
: m_pCurrentNode(_pCurrentNode)
{
}
bool operator == (const Iterator & _it) const
{
return m_pCurrentNode == _it.m_pCurrentNode;
}
bool operator!= (const Iterator & _it) const
{
return!(* this == _it);
}
Iterator & operator ++ ()
{
assert(m_pCurrentNode);
m_pCurrentNode = m_pCurrentNode->m_pNextNode;
return * this;
}
const T& operator * () const
{
assert(m_pCurrentNode);
return m_pCurrentNode->m_value;
}
};
Iterator begin ()
{
return Iterator(m_pFirstNode);
}
Iterator end ()
{
return Iterator(nullptr);
}
//...
};
, :
const List< int >::Iterator it = MyFind(myList.begin(), myList.end(), 3);
assert(* it == 3);
, , . , , , .
-
. ,
,
(
, , , ).
, ,
(
C++11,
- ).
, ,
.
,
.
, , ,
,
. :
, ;
|
|
,
,
vptr;
,
( ,
).
, ,
:
- ,
/ ;
.
,
,
.
- 5-
100- .
- ( AcademicGroupMarks) -
, .
- , , .
.
MarkSystem .
- SovietMarkSystem
BolognaMarkSystem.
AcademicGroupMarks,
.
.
.
:
●
MarkSystem, ;
●
BolognaMarkSystem
SovietMarkSystem , ;
●
;
●
.
,
.
.
.
, , ,
.