-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.cpp
More file actions
66 lines (60 loc) · 1.51 KB
/
variable.cpp
File metadata and controls
66 lines (60 loc) · 1.51 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
#include "variable.h"
#include <string.h>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
string variable::find_key(
string key) { //find key in the map to replace legal string after $
map<string, string>::iterator iter;
iter = variables.find(key);
string res = "";
if (iter != variables.end()) {
res = iter->second;
}
if (res == "") {
cerr << key << " not found!" << endl;
}
return res;
}
void variable::add_element(string key, string value) { //use in case set
map<string, string>::iterator iter;
iter = variables.find(key);
if (iter != variables.end()) {
variables.erase(iter);
variables.insert(pair<string, string>(key, value));
}
else {
variables.insert(pair<string, string>(key, value));
}
}
void variable::rev_element(string key) { //use in case rev
map<string, string>::iterator iter;
iter = variables.find(key);
if (iter != variables.end()) {
reverse(iter->second.begin(), iter->second.end());
}
else {
cerr << "key not find!" << endl;
}
}
string variable::export_element(string key) { //use in case export
map<string, string>::iterator iter;
iter = variables.find(key);
string p = "";
if (iter != variables.end()) {
p = key + "=" + iter->second;
}
else {
cerr << "key not find!" << endl;
}
return p;
}
void variable::delete_all() { //delete private field in case of exit or cin=eof
variables.clear();
}