<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
compare [C++ Reference]
</title>
<meta name="generator" content="DokuWiki Release 2009-12-25c "Lemming"" />
<meta name="robots" content="index,follow" />
<meta name="date" content="2009-10-10T20:05:13-0700" />
<meta name="keywords" content="string,compare" />
<link rel="search" type="application/opensearchdescription+xml" href="/wiki/lib/exe/opensearch.php" title="C++ Reference" />
<link rel="start" href="/wiki/" />
<link rel="contents" href="/wiki/string/compare?do=index" title="Index" />
<link rel="alternate" type="application/rss+xml" title="Recent Changes" href="/wiki/feed.php" />
<link rel="alternate" type="application/rss+xml" title="Current Namespace" href="/wiki/feed.php?mode=list&ns=string" />
<link rel="edit" title="Edit this page" href="/wiki/string/compare?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/string/compare" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/string/compare" />
<link rel="canonical" href="http://www.cppreference.com/wiki/string/compare" />
<link rel="stylesheet" media="all" type="text/css" href="/wiki/lib/exe/css.php?s=all&t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="screen" type="text/css" href="/wiki/lib/exe/css.php?t=custom1&tseed=1272971091" />
<link rel="stylesheet" media="print" type="text/css" href="/wiki/lib/exe/css.php?s=print&t=custom1&tseed=1272971091" />
<script type="text/javascript" charset="utf-8" ><!--//--><![CDATA[//><!--
var NS='string';var JSINFO = {"id":"string:compare","namespace":"string"};
//--><!]]></script>
<script type="text/javascript" charset="utf-8" src="/wiki/lib/exe/js.php?tseed=1272971091" ></script>
<link rel="shortcut icon" href="/wiki/lib/tpl/custom1/images/favicon.png" />
</head>
<body>
<div class="dokuwiki">
<div class="stylehead">
<div class="breadcrumbs">
<span class="bchead">You are here: </span><a href="../start.html" title="start">C++ Reference</a> » <a href="../string/start.html" title="string:start">C++ Strings</a> » <a href="../string/compare.html" title="string:compare">compare</a> </div>
</div>
<div class="page">
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
<!-- wikipage start -->
<h2><a name="compare" id="compare">compare</a></h2>
<div class="level2">
<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include <string></span>
<span class="kw4">int</span> compare<span class="br0">(</span> <span class="kw4">const</span> string<span class="sy0">&</span> str <span class="br0">)</span> <span class="kw4">const</span><span class="sy0">;</span>
<span class="kw4">int</span> compare<span class="br0">(</span> <span class="kw4">const</span> Char<span class="sy0">*</span> str <span class="br0">)</span> <span class="kw4">const</span><span class="sy0">;</span>
<span class="kw4">int</span> compare<span class="br0">(</span> size_type index<span class="sy0">,</span> size_type length<span class="sy0">,</span> <span class="kw4">const</span> string<span class="sy0">&</span> str <span class="br0">)</span> <span class="kw4">const</span><span class="sy0">;</span>
<span class="kw4">int</span> compare<span class="br0">(</span> size_type index<span class="sy0">,</span> size_type length<span class="sy0">,</span> <span class="kw4">const</span> string<span class="sy0">&</span> str<span class="sy0">,</span> size_type index2<span class="sy0">,</span> size_type length2 <span class="br0">)</span> <span class="kw4">const</span><span class="sy0">;</span>
<span class="kw4">int</span> compare<span class="br0">(</span> size_type index<span class="sy0">,</span> size_type length<span class="sy0">,</span> <span class="kw4">const</span> Char<span class="sy0">*</span> str<span class="sy0">,</span> size_type length2 <span class="sy0">=</span> npos <span class="br0">)</span> <span class="kw4">const</span><span class="sy0">;</span></pre>
<p>
The compare() function either compares str to the current string in a variety
of ways, returning
</p>
<table class="inline">
<tr class="row0">
<th class="col0 leftalign">Return Value </th><th class="col1">Case</th>
</tr>
<tr class="row1">
<td class="col0 leftalign">less than zero </td><td class="col1">this < str</td>
</tr>
<tr class="row2">
<td class="col0 leftalign">zero </td><td class="col1">this == str</td>
</tr>
<tr class="row3">
<td class="col0">greater than zero </td><td class="col1">this > str</td>
</tr>
</table>
<p>
The various functions either:
</p>
<ul>
<li class="level1"><div class="li"> compare str to the current string,</div>
</li>
<li class="level1"><div class="li"> compare str to a substring of the current string, starting at index for length characters,</div>
</li>
<li class="level1"><div class="li"> compare a substring of str to a substring of the current string, where index2 and length2 refer to str and index and length refer to the current string,</div>
</li>
<li class="level1"><div class="li"> or compare a substring of str to a substring of the current string, where the substring of str begins at zero and is length2 characters long, and the substring of the current string begins at index and is length characters long.</div>
</li>
</ul>
<p>
For example, the following code uses compare() to compare four strings with each other:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="kw4">string</span> names<span class="br0">[</span><span class="br0">]</span> <span class="sy0">=</span> <span class="br0">{</span><span class="st0">"Homer"</span><span class="sy0">,</span> <span class="st0">"Marge"</span><span class="sy0">,</span> <span class="st0">"3-eyed fish"</span><span class="sy0">,</span> <span class="st0">"inanimate carbon rod"</span><span class="br0">}</span><span class="sy0">;</span>
<span class="kw1">for</span><span class="br0">(</span> <span class="kw4">int</span> i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0"><</span> <span class="nu0">4</span><span class="sy0">;</span> i<span class="sy0">++</span> <span class="br0">)</span> <span class="br0">{</span>
<span class="kw1">for</span><span class="br0">(</span> <span class="kw4">int</span> j <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> j <span class="sy0"><</span> <span class="nu0">4</span><span class="sy0">;</span> j<span class="sy0">++</span> <span class="br0">)</span> <span class="br0">{</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> names<span class="br0">[</span>i<span class="br0">]</span>.<span class="me1">compare</span><span class="br0">(</span> names<span class="br0">[</span>j<span class="br0">]</span> <span class="br0">)</span> <span class="sy0"><<</span> <span class="st0">" "</span><span class="sy0">;</span>
<span class="br0">}</span>
<a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a> <span class="sy0"><<</span> endl<span class="sy0">;</span>
<span class="br0">}</span></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="inline">
<tr class="row0">
<th class="col0"> </th><th class="col1"> Homer </th><th class="col2"> Marge </th><th class="col3"> 3-eyed fish </th><th class="col4"> inanimate carbon rod </th>
</tr>
<tr class="row1">
<td class="col0">“Homer”.compare( x )</td><td class="col1">0</td><td class="col2">-1</td><td class="col3">1</td><td class="col4">-1</td>
</tr>
<tr class="row2">
<td class="col0">“Marge”.compare( x )</td><td class="col1">1</td><td class="col2">0</td><td class="col3">1</td><td class="col4">-1</td>
</tr>
<tr class="row3">
<td class="col0">“3-eyed fish”.compare( x )</td><td class="col1">-1</td><td class="col2">-1</td><td class="col3">0</td><td class="col4">-1</td>
</tr>
<tr class="row4">
<td class="col0">“inanimate carbon rod”.compare( x )</td><td class="col1">1</td><td class="col2">1</td><td class="col3">1</td><td class="col4">0</td>
</tr>
</table>
<p>
Related Topics: <a href="../string/operator.html" class="wikilink1" title="string:string_operators">String operators</a>
</p>
</div>
<!-- wikipage stop -->
</div>
<div class="clearer"> </div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
string/compare.txt · Last modified: 10/10/2009 20:05 by 124.255.102.233 </div>
-->
</div>
</div></div></body>
</html>