-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelement.cpp
More file actions
91 lines (84 loc) · 2.7 KB
/
element.cpp
File metadata and controls
91 lines (84 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <sstream>
#include <iostream>
#include "element.h"
//=========================================================================
// base constructor
// Parameters: none
// Returns: new Element object
// Creates an empty Element object
//=========================================================================
template <class D, class T>
Element<D,T>::Element() : data(D()), key(T()) {
// Default constructor of T is used to initialize item
// For int, it initializes to 0 (using std::numeric_limits), for std::string, it initializes to empty string
}
// Pre condition: None
// Post condition: An empty element is created
/*
{
if (constexpr (std::is_same<T, int>)) {
item = 0;
} else if (constexpr (std::is_same<T, std::string>)) {
item = "";
} else {
// Handle other types here if necessary
}
key = -1;
}
*/
//=========================================================================
// parameter constructor
// Parameters:
// d - data value for the element
// k - key for the element
// Returns: none
//=========================================================================
template <class D, class T>
Element<D,T>::Element( D d, T k )
// Pre condition: d and k are valid entries for the element type
// Post condition: an element is created with d as the data and k as the key
{
data = d;
key = k;
}
//=========================================================================
// get_key
// Parameters: none
// Returns:
// key - int value of the element's key
//=========================================================================
template <class D, class T>
T Element<D,T>::get_key( void ) const
// Pre condition: element exists
// Post condition: correct key vlaue of the element is returned
{
return key;
}
//=========================================================================
// get_data
// Parameters: none
// Returns:
// item - data value of the element
//=========================================================================
template <class D, class T>
D Element<D,T>::get_data( void ) const
// Pre condition: element exists
// Post condition: correct data value of the element is returned
{
return data;
}
//=========================================================================
// to_string
// Parameters: none
// Returns:
// string - the string containing the data and key of an element
//=========================================================================
template <class D, class T>
string Element<D,T>::to_string() const
// Pre condition: element exists
// Post condition: string contains correct data and key value of the element
{
ostringstream out;
out << "(" << data << "," << key << ")";
return out.str();
}