-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompare.html
More file actions
197 lines (144 loc) · 5.46 KB
/
compare.html
File metadata and controls
197 lines (144 loc) · 5.46 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<!-- Mirrored from cppreference.com/cppstring/compare.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:24:52 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
<head>
<meta name="generator" content=
"HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">
<title>compare</title>
<link href="../cppreference.css" rel="stylesheet" type="text/css">
<link href="../prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../prettify.js"></script>
</head>
<body onload="prettyPrint()">
<table>
<tr>
<td>
<div class="body-content">
<div class="header-box">
<a href="../index-2.html">cppreference.com</a> > <a href=
"index.html">C++ Strings</a> > <a href=
"compare.html">compare</a>
</div>
<div class="name-format">
compare
</div>
<div class="syntax-name-format">
Syntax:
</div>
<pre class="syntax-box">
#include <string>
int compare( const string& str );
int compare( const char* str );
int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string& str );
int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const string& str, <strong>size_type</strong> index2,
<strong>size_type</strong> length2 );
int compare( <strong>size_type</strong> index, <strong>size_type</strong> length, const char* str, <strong>size_type</strong> length2 );
</pre>
<p>The compare() function either compares <em>str</em> to the current
string in a variety of ways, returning</p>
<table class="code-table">
<tr>
<th class="code-table-th">Return Value</th>
<th class="code-table-th">Case</th>
</tr>
<tr>
<td class="code-table-td">less than zero</td>
<td class="code-table-td">this < str</td>
</tr>
<tr>
<td class="code-table-td">zero</td>
<td class="code-table-td">this == str</td>
</tr>
<tr>
<td class="code-table-td">greater than zero</td>
<td class="code-table-td">this > str</td>
</tr>
</table>
<p>The various functions either:</p>
<ul>
<li>compare <em>str</em> to the current string,</li>
<li>compare <em>str</em> to a substring of the current string,
starting at <em>index</em> for <em>length</em> characters,</li>
<li>compare a substring of <em>str</em> to a substring of the
current string, where <em>index2</em> and <em>length2</em> refer to
<em>str</em> and <em>index</em> and <em>length</em> refer to the
current string,</li>
<li>or compare a substring of <em>str</em> to a substring of the
current string, where the substring of <em>str</em> begins at zero
and is <em>length2</em> characters long, and the substring of the
current string begins at <em>index</em> and is <em>length</em>
characters long.</li>
</ul>
<p>For example, the following code uses compare() to compare four
strings with eachother:</p>
<pre class="prettyprint">
string names[] = {"Homer", "Marge", "3-eyed fish", "inanimate carbon rod"};
for( int i = 0; i < 4; i++ ) {
for( int j = 0; j < 4; j++ ) {
cout << names[i].compare( names[j] ) << " ";
}
cout << endl;
}
</pre>
<p>Data from the above code was used to generate this table, which
shows how the various strings compare to eachother:</p>
<table class="code-table">
<tr>
<th class="code-table-th"></th>
<th class="code-table-th">Homer</th>
<th class="code-table-th">Marge</th>
<th class="code-table-th">3-eyed fish</th>
<th class="code-table-th">inanimate carbon rod</th>
</tr>
<tr>
<td class="code-table-td">"Homer".compare( x )</td>
<td class="code-table-td">0</td>
<td class="code-table-td">-1</td>
<td class="code-table-td">1</td>
<td class="code-table-td">-1</td>
</tr>
<tr>
<td class="code-table-td">"Marge".compare( x )</td>
<td class="code-table-td">1</td>
<td class="code-table-td">0</td>
<td class="code-table-td">1</td>
<td class="code-table-td">-1</td>
</tr>
<tr>
<td class="code-table-td">"3-eyed fish".compare( x
)</td>
<td class="code-table-td">-1</td>
<td class="code-table-td">-1</td>
<td class="code-table-td">0</td>
<td class="code-table-td">-1</td>
</tr>
<tr>
<td class="code-table-td">"inanimate carbon
rod".compare( x )</td>
<td class="code-table-td">1</td>
<td class="code-table-td">1</td>
<td class="code-table-td">1</td>
<td class="code-table-td">0</td>
</tr>
</table>
<div class="related-name-format">
Related topics:
</div>
<div class="related-content">
<a href="string_operators.html">String operators</a>
</div>
</div>
</td>
<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
</tr>
</table>
</body>
<!-- Mirrored from cppreference.com/cppstring/compare.html by HTTrack Website Copier/3.x [XR&CO'2004], Tue, 22 Jan 2008 06:24:52 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=iso-8859-1"><!-- /Added by HTTrack -->
</html>