<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
npos [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-11T12:25:02-0700" />
<meta name="keywords" content="string,npos" />
<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/npos?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/npos?do=edit" />
<link rel="alternate" type="text/html" title="Plain HTML" href="/wiki/_export/xhtml/string/npos" />
<link rel="alternate" type="text/plain" title="Wiki Markup" href="/wiki/_export/raw/string/npos" />
<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:npos","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/npos.html" title="string:npos">npos</a> </div>
</div>
<div class="page">
</script>
<script type="text/javascript">
_uacct = "UA-2828341-1";
urchinTracker();
</script>
<!-- wikipage start -->
<h2><a name="npos" id="npos">npos</a></h2>
<div class="level2">
<p>
Syntax:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="kw4">static</span> <span class="kw4">const</span> size_type npos <span class="sy0">=</span> <span class="sy0">-</span><span class="nu0">1</span><span class="sy0">;</span></pre>
<p>
<code>string::npos</code> is a special value that indicates one of the following:
</p>
<ul>
<li class="level1"><div class="li"> “not found”, as a return value of <code>find()</code>, <code>find_first_of()</code>, etc.</div>
</li>
<li class="level1"><div class="li"> “all remaining characters”, as length of substrings.</div>
</li>
</ul>
<p>
Example code:
</p>
<pre class="c code c++" style="font-family:monospace;"> <span class="co2">#include <set></span>
<span class="co2">#include <iostream></span>
<span class="co2">#include <iterator></span>
<span class="co2">#include <algorithm></span>
<span class="co2">#include <string></span>
<span class="kw4">int</span> main<span class="br0">(</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw4">const</span> std<span class="sy0">::</span><span class="kw4">string</span> text<span class="br0">(</span><span class="st0">"Vectors contain contiguous elements stored as an array.<span class="es1">\n</span><span class="es1">\n</span>"</span>
<span class="st0">"Accessing members of a vector can be done in constant time, "</span>
<span class="st0">"appending elements to a vector can be done in amortized constant time, "</span>
<span class="st0">"whereas locating a specific value "</span>
<span class="st0">"or inserting elements into the vector takes linear time."</span><span class="br0">)</span><span class="sy0">;</span>
<span class="kw4">const</span> std<span class="sy0">::</span><span class="kw4">string</span> delims<span class="br0">(</span><span class="st0">" <span class="es1">\t</span><span class="es1">\n</span>.,?!;:<span class="es1">\"</span>"</span><span class="br0">)</span><span class="sy0">;</span>
std<span class="sy0">::</span><span class="me2">set</span><span class="sy0"><</span>std<span class="sy0">::</span><span class="me2">string</span><span class="sy0">></span> words<span class="sy0">;</span>
std<span class="sy0">::</span><span class="kw4">string</span><span class="sy0">::</span><span class="me2">size_type</span> end_pos<span class="sy0">;</span>
<span class="co1">// find beginning of the first word</span>
std<span class="sy0">::</span><span class="kw4">string</span><span class="sy0">::</span><span class="me2">size_type</span> beg_pos <span class="sy0">=</span> text.<span class="me1">find_first_not_of</span><span class="br0">(</span>delims<span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// while beg_pos is not "not found" (i.e. beginning of a word is found)</span>
<span class="kw1">while</span> <span class="br0">(</span>beg_pos <span class="sy0">!=</span> std<span class="sy0">::</span><span class="kw4">string</span><span class="sy0">::</span><span class="me2">npos</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="co1">// find delimiter (end of the word)</span>
end_pos <span class="sy0">=</span> text.<span class="me1">find_first_of</span><span class="br0">(</span>delims<span class="sy0">,</span> beg_pos<span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// insert substring into set</span>
words.<span class="me1">insert</span><span class="br0">(</span>text.<span class="me1">substr</span><span class="br0">(</span>beg_pos<span class="sy0">,</span>
end_pos <span class="sy0">==</span> std<span class="sy0">::</span><span class="kw4">string</span><span class="sy0">::</span><span class="me2">npos</span> <span class="co1">// if end_pos is "not found"</span>
<span class="sy0">?</span> std<span class="sy0">::</span><span class="kw4">string</span><span class="sy0">::</span><span class="me2">npos</span> <span class="co1">// then "all remaining characters"</span>
<span class="sy0">:</span> end_pos <span class="sy0">-</span> beg_pos<span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
<span class="co1">// find beginning of the next word</span>
beg_pos <span class="sy0">=</span> text.<span class="me1">find_first_not_of</span><span class="br0">(</span>delims<span class="sy0">,</span> end_pos<span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="co1">// display words</span>
std<span class="sy0">::</span><span class="me2">copy</span><span class="br0">(</span>words.<span class="me1">begin</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> words.<span class="me1">end</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">,</span> std<span class="sy0">::</span><span class="me2">ostream_iterator</span><span class="sy0"><</span>std<span class="sy0">::</span><span class="me2">string</span><span class="sy0">></span><span class="br0">(</span>std<span class="sy0">::</span><a href="http://www.opengroup.org/onlinepubs/009695399/functions/cout.html"><span class="kw3">cout</span></a><span class="sy0">,</span> <span class="st0">"<span class="es1">\n</span>"</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">;</span>
<span class="kw1">return</span> <span class="nu0">0</span><span class="sy0">;</span>
<span class="br0">}</span></pre>
<p>
Related Topics: <a href="../string/find.html" class="wikilink1" title="string:find">find</a>, <a href="../string/find_first_not_of.html" class="wikilink1" title="string:find_first_not_of">find_first_not_of</a>, <a href="../string/find_first_of.html" class="wikilink1" title="string:find_first_of">find_first_of</a>, <a href="../string/find_last_not_of.html" class="wikilink1" title="string:find_last_not_of">find_last_not_of</a>, <a href="../string/find_last_of.html" class="wikilink1" title="string:find_last_of">find_last_of</a>, <a href="../string/rfind.html" class="wikilink1" title="string:rfind">rfind</a>, <a href="../string/substr.html" class="wikilink1" title="string:substr">substr</a>
</p>
</div>
<!-- wikipage stop -->
</div>
<div class="clearer"> </div>
<div class="stylefoot">
<div class="meta">
<div class="user">
</div>
<!--
<div class="doc">
string/npos.txt · Last modified: 10/11/2009 12:25 by 124.255.99.70 </div>
-->
</div>
</div></div></body>
</html>