forked from ashishlal/c--
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13_7.cpp
More file actions
175 lines (168 loc) · 4.73 KB
/
13_7.cpp
File metadata and controls
175 lines (168 loc) · 4.73 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <iostream>
#include <sstream>
#include <vector>
#include <memory>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::istringstream;
template<typename A, typename B>
struct Pair {
A key;
B val;
Pair(A &k, B &u): key(k), val(u) {}
Pair(const A &k, const B &u): key(k), val(u) {}
bool operator != (Pair X) { bool equal = ((X.key == key) && (X.val == val)); if(equal) return false; return true; }
};
template <typename T, typename U>
class Assoc {
#if 0
template<typename A, typename B>
struct Pair {
A key;
B val;
Pair(A &k, B &u): key(k), val(u) {}
Pair(const A &k, const B &u): key(k), val(u) {}
};
#endif
vector<Pair<T, U> > vec;
Assoc(const Assoc &) {} ; // copy constructor
Assoc & operator=(const Assoc &) {}; // assignment operator
public:
Assoc() {}
const U& operator[] (const T &t)
{
for(typename vector<Pair<T, U> >::iterator p=vec.begin(); p != vec.end(); p++)
{
if(t == p->key) return p->val;
return vec.back().val;
}
}
void add(const T &t, const U & val)
{
for(typename vector<Pair<T, U> >::iterator p=vec.begin(); p != vec.end(); p++)
{
if(const_cast<T &>(t) == p->key) return;
}
vec.push_back(Pair<T, U> (t, val));
}
void erase(const T &t)
{
for(typename vector<Pair<T, U> >::iterator p=vec.begin(); p != vec.end(); p++)
{
if(const_cast<T &>(t) == p->key) {
vec.erase(p);
return;
}
}
}
U & find(const T & key)
{
for(typename vector<Pair<T, U> >::iterator p=vec.begin(); p != vec.end(); p++)
{
if(const_cast<T &>(key) == p->key) {
return p->val;
}
}
return vec.back().val;
}
void topologicalsort()
{
vector<Pair<T, U> > vec1 ;
vec1.clear();
for(typename vector<Pair<T, U> >::iterator p=vec.begin();
p != vec.end(); p++) {
auto pred = p;
for(typename vector<Pair<T, U> >::iterator p1=vec.begin();
p1 != vec.end(); p1++) {
if((!(p1->key.c)) && (pred->key.compare(p1->key) > 0)) {
pred = p1;
}
}
pred->key.c = true;
vec1.push_back(*pred);
}
vec = std::move(vec1);
}
void print_all()
{
cout << "=============================== " << endl;
for(typename vector<Pair<T, U> >::iterator p=vec.begin();
p != vec.end(); p++)
{
cout << p->key.s << ": " << p->val.val << endl;
}
cout << "=============================== " << endl;
}
};
template<typename X, typename Y>
class Map {
Assoc<X, Y> vec;
public:
Map() {};
Map(const Assoc<X, Y> &vec1):vec(vec1) {};
void insert(const X &t, const Y &u)
{
vec.add(t, u);
}
void erase(const X & key)
{
vec.erase(key);
}
Y & find(const X & t)
{
return vec.find(t);
}
void topologicalsort()
{
return vec.topologicalsort();
}
void print_all()
{
return vec.print_all();
}
};
class A {
public:
string s;
bool c;
A() {c=false;};
A(string &str): s(str), c(false) {};
A(const char *str): s(string(str)) {};
operator string() { return s; }
bool operator== (A &a) { if(a.s == s) return true; return false;}
int compare(string &s1) { return s.compare(s1); }
int compare(A &a) { return s.compare(a.s); }
};
class B {
public:
double val;
B() {};
B(double &v): val(v) {};
B(double &&v): val(std::forward<double> (v)) {};
// B(double const & v): val(v) {};
operator double() { return val; };
bool operator== (B &b) { if(b.val == val) return true; return false;}
int compare(double v) { return (int)(val-v); }
};
int main()
{
Map<A, B> m;
string str;
cout << "Enter a (key, value) pair seperated by a comma" << endl;
cout << "Enter a string for key and a number for value" << endl;
getline(cin, str);
istringstream ss(str);
string word;
vector<string> words;
while( getline(ss, word, ',') ) {
cout << word << "\n";
words.push_back(word);
}
double val = atof(words[1].c_str());
m.insert(words[0], val);
m.insert("aa", 42);
m.print_all();
}