forked from aaronbloomfield/pdr
-
Notifications
You must be signed in to change notification settings - Fork 228
/
svutil.cpp.html
63 lines (55 loc) · 7.52 KB
/
svutil.cpp.html
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
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="GENERATOR" content="GNU source-highlight
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite">
<title>svutil.cpp</title>
</head>
<body style="background-color:white">
<pre><i><span style="color:#9A1900">// utility functions for vectors of strings</span></i>
<b><span style="color:#000080">#include</span></b> <span style="color:#FF0000">"svutil.h"</span>
<i><span style="color:#9A1900">// prints the strings in the vector</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">svOutput</span></b> <span style="color:#990000">(</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">vector<string></span> <span style="color:#990000">&</span>vect <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> i<span style="color:#990000">=</span><span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> vect<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">();</span> <span style="color:#990000">++</span>i<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
cout <span style="color:#990000"><<</span> vect<span style="color:#990000">.</span><b><span style="color:#000000">at</span></b><span style="color:#990000">(</span>i<span style="color:#990000">)</span> <span style="color:#990000"><<</span> <span style="color:#FF0000">" "</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
cout <span style="color:#990000"><<</span> endl<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// finds a string in a vector and returns its index</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">svFind</span></b> <span style="color:#990000">(</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">vector<string></span> <span style="color:#990000">&</span>vect<span style="color:#990000">,</span> <span style="color:#008080">string</span> s<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">for</span></b> <span style="color:#990000">(</span><span style="color:#009900">int</span> i<span style="color:#990000">=</span><span style="color:#993399">0</span><span style="color:#990000">;</span> i <span style="color:#990000"><</span> vect<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">();</span> <span style="color:#990000">++</span>i<span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> vect<span style="color:#990000">.</span><b><span style="color:#000000">at</span></b><span style="color:#990000">(</span>i<span style="color:#990000">)</span> <span style="color:#990000">==</span> s <span style="color:#990000">)</span> <b><span style="color:#0000FF">return</span></b> i<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// removes an element from the vector at index idx</span></i>
<i><span style="color:#9A1900">// returns true if remove successful, otherwise false</span></i>
<span style="color:#009900">bool</span> <b><span style="color:#000000">svDeleteAt</span></b> <span style="color:#990000">(</span> <span style="color:#008080">vector<string></span> <span style="color:#990000">&</span>vect<span style="color:#990000">,</span> <span style="color:#009900">int</span> idx <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<span style="color:#009900">int</span> lastIndex <span style="color:#990000">=</span> vect<span style="color:#990000">.</span><b><span style="color:#000000">size</span></b><span style="color:#990000">()-</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> idx <span style="color:#990000"><</span> <span style="color:#993399">0</span> <span style="color:#990000">||</span> idx <span style="color:#990000">></span> lastIndex <span style="color:#990000">)</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">false</span></b><span style="color:#990000">;</span>
<b><span style="color:#0000FF">if</span></b> <span style="color:#990000">(</span> idx <span style="color:#990000">==</span> lastIndex <span style="color:#990000">)</span>
vect<span style="color:#990000">.</span><b><span style="color:#000000">pop_back</span></b><span style="color:#990000">();</span>
<b><span style="color:#0000FF">else</span></b> <span style="color:#FF0000">{</span>
<span style="color:#008080">string</span> temp <span style="color:#990000">=</span> vect<span style="color:#990000">.</span><b><span style="color:#000000">at</span></b><span style="color:#990000">(</span>lastIndex<span style="color:#990000">);</span>
vect<span style="color:#990000">.</span><b><span style="color:#000000">pop_back</span></b><span style="color:#990000">();</span>
vect<span style="color:#990000">.</span><b><span style="color:#000000">at</span></b><span style="color:#990000">(</span>idx<span style="color:#990000">)</span> <span style="color:#990000">=</span> temp<span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<b><span style="color:#0000FF">return</span></b> <b><span style="color:#0000FF">true</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// sorts a vector of string in ascending order</span></i>
<span style="color:#009900">void</span> <b><span style="color:#000000">svSort</span></b> <span style="color:#990000">(</span> <span style="color:#008080">vector<string></span> <span style="color:#990000">&</span>vect <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// not implemented yet</span></i>
<b><span style="color:#0000FF">return</span></b><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
<i><span style="color:#9A1900">// return the index of the largest string in range index start to end</span></i>
<span style="color:#009900">int</span> <b><span style="color:#000000">svFindMax</span></b><span style="color:#990000">(</span> <b><span style="color:#0000FF">const</span></b> <span style="color:#008080">vector<string></span> <span style="color:#990000">&</span>vect<span style="color:#990000">,</span> <span style="color:#009900">int</span> start<span style="color:#990000">,</span> <span style="color:#009900">int</span> end <span style="color:#990000">)</span> <span style="color:#FF0000">{</span>
<i><span style="color:#9A1900">// not implemented yet</span></i>
<b><span style="color:#0000FF">return</span></b> <span style="color:#990000">-</span><span style="color:#993399">1</span><span style="color:#990000">;</span>
<span style="color:#FF0000">}</span>
</pre>
</body>
</html>