1 |
424 |
jeremybenn |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
2 |
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
3 |
|
|
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 13. String Classes</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><meta name="keywords" content=" ISO C++ , library " /><link rel="home" href="../spine.html" title="The GNU C++ Library Documentation" /><link rel="up" href="strings.html" title="Part V. Strings" /><link rel="prev" href="strings.html" title="Part V. Strings" /><link rel="next" href="bk01pt05ch13s02.html" title="Case Sensitivity" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 13. String Classes</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="strings.html">Prev</a> </td><th width="60%" align="center">Part V.
|
4 |
|
|
Strings
|
5 |
|
|
|
6 |
|
|
</th><td width="20%" align="right"> <a accesskey="n" href="bk01pt05ch13s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 13. String Classes"><div class="titlepage"><div><div><h2 class="title"><a id="manual.strings.string"></a>Chapter 13. String Classes</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="bk01pt05ch13.html#strings.string.simple">Simple Transformations</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s02.html">Case Sensitivity</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s03.html">Arbitrary Character Types</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s04.html">Tokenizing</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s05.html">Shrink to Fit</a></span></dt><dt><span class="sect1"><a href="bk01pt05ch13s06.html">CString (MFC)</a></span></dt></dl></div><div class="sect1" title="Simple Transformations"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="strings.string.simple"></a>Simple Transformations</h2></div></div></div><p>
|
7 |
|
|
Here are Standard, simple, and portable ways to perform common
|
8 |
|
|
transformations on a <code class="code">string</code> instance, such as
|
9 |
|
|
"convert to all upper case." The word transformations
|
10 |
|
|
is especially apt, because the standard template function
|
11 |
|
|
<code class="code">transform<></code> is used.
|
12 |
|
|
</p><p>
|
13 |
|
|
This code will go through some iterations. Here's a simple
|
14 |
|
|
version:
|
15 |
|
|
</p><pre class="programlisting">
|
16 |
|
|
#include <string>
|
17 |
|
|
#include <algorithm>
|
18 |
|
|
#include <cctype> // old <ctype.h>
|
19 |
|
|
|
20 |
|
|
struct ToLower
|
21 |
|
|
{
|
22 |
|
|
char operator() (char c) const { return std::tolower(c); }
|
23 |
|
|
};
|
24 |
|
|
|
25 |
|
|
struct ToUpper
|
26 |
|
|
{
|
27 |
|
|
char operator() (char c) const { return std::toupper(c); }
|
28 |
|
|
};
|
29 |
|
|
|
30 |
|
|
int main()
|
31 |
|
|
{
|
32 |
|
|
std::string s ("Some Kind Of Initial Input Goes Here");
|
33 |
|
|
|
34 |
|
|
// Change everything into upper case
|
35 |
|
|
std::transform (s.begin(), s.end(), s.begin(), ToUpper());
|
36 |
|
|
|
37 |
|
|
// Change everything into lower case
|
38 |
|
|
std::transform (s.begin(), s.end(), s.begin(), ToLower());
|
39 |
|
|
|
40 |
|
|
// Change everything back into upper case, but store the
|
41 |
|
|
// result in a different string
|
42 |
|
|
std::string capital_s;
|
43 |
|
|
capital_s.resize(s.size());
|
44 |
|
|
std::transform (s.begin(), s.end(), capital_s.begin(), ToUpper());
|
45 |
|
|
}
|
46 |
|
|
</pre><p>
|
47 |
|
|
<span class="emphasis"><em>Note</em></span> that these calls all
|
48 |
|
|
involve the global C locale through the use of the C functions
|
49 |
|
|
<code class="code">toupper/tolower</code>. This is absolutely guaranteed to work --
|
50 |
|
|
but <span class="emphasis"><em>only</em></span> if the string contains <span class="emphasis"><em>only</em></span> characters
|
51 |
|
|
from the basic source character set, and there are <span class="emphasis"><em>only</em></span>
|
52 |
|
|
96 of those. Which means that not even all English text can be
|
53 |
|
|
represented (certain British spellings, proper names, and so forth).
|
54 |
|
|
So, if all your input forevermore consists of only those 96
|
55 |
|
|
characters (hahahahahaha), then you're done.
|
56 |
|
|
</p><p><span class="emphasis"><em>Note</em></span> that the
|
57 |
|
|
<code class="code">ToUpper</code> and <code class="code">ToLower</code> function objects
|
58 |
|
|
are needed because <code class="code">toupper</code> and <code class="code">tolower</code>
|
59 |
|
|
are overloaded names (declared in <code class="code"><cctype></code> and
|
60 |
|
|
<code class="code"><locale></code>) so the template-arguments for
|
61 |
|
|
<code class="code">transform<></code> cannot be deduced, as explained in
|
62 |
|
|
<a class="ulink" href="http://gcc.gnu.org/ml/libstdc++/2002-11/msg00180.html" target="_top">this
|
63 |
|
|
message</a>.
|
64 |
|
|
|
65 |
|
|
At minimum, you can write short wrappers like
|
66 |
|
|
</p><pre class="programlisting">
|
67 |
|
|
char toLower (char c)
|
68 |
|
|
{
|
69 |
|
|
return std::tolower(c);
|
70 |
|
|
} </pre><p>(Thanks to James Kanze for assistance and suggestions on all of this.)
|
71 |
|
|
</p><p>Another common operation is trimming off excess whitespace. Much
|
72 |
|
|
like transformations, this task is trivial with the use of string's
|
73 |
|
|
<code class="code">find</code> family. These examples are broken into multiple
|
74 |
|
|
statements for readability:
|
75 |
|
|
</p><pre class="programlisting">
|
76 |
|
|
std::string str (" \t blah blah blah \n ");
|
77 |
|
|
|
78 |
|
|
// trim leading whitespace
|
79 |
|
|
string::size_type notwhite = str.find_first_not_of(" \t\n");
|
80 |
|
|
str.erase(0,notwhite);
|
81 |
|
|
|
82 |
|
|
// trim trailing whitespace
|
83 |
|
|
notwhite = str.find_last_not_of(" \t\n");
|
84 |
|
|
str.erase(notwhite+1); </pre><p>Obviously, the calls to <code class="code">find</code> could be inserted directly
|
85 |
|
|
into the calls to <code class="code">erase</code>, in case your compiler does not
|
86 |
|
|
optimize named temporaries out of existence.
|
87 |
|
|
</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="strings.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="strings.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="bk01pt05ch13s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Part V.
|
88 |
|
|
Strings
|
89 |
|
|
|
90 |
|
|
</td><td width="20%" align="center"><a accesskey="h" href="../spine.html">Home</a></td><td width="40%" align="right" valign="top"> Case Sensitivity</td></tr></table></div></body></html>
|