<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: 13.4 &#8212; Stream classes for strings</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Auasp</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-95706</link>
		<dc:creator>Auasp</dc:creator>
		<pubDate>Thu, 14 Jul 2011 04:14:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-95706</guid>
		<description>I Don&#039;t see any erase() function called.</description>
		<content:encoded><![CDATA[<p>I Don&#8217;t see any erase() function called.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gammerz</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-91788</link>
		<dc:creator>Gammerz</dc:creator>
		<pubDate>Fri, 10 Sep 2010 17:10:33 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-91788</guid>
		<description>The text for this example still refers to erase().</description>
		<content:encoded><![CDATA[<p>The text for this example still refers to erase().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C++ Tutorial and Online Ebook</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-87437</link>
		<dc:creator>C++ Tutorial and Online Ebook</dc:creator>
		<pubDate>Wed, 30 Jun 2010 01:13:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-87437</guid>
		<description>[...] 13.4 Stream classes for strings [...]</description>
		<content:encoded><![CDATA[<p>[...] 13.4 Stream classes for strings [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: serenity</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-73626</link>
		<dc:creator>serenity</dc:creator>
		<pubDate>Fri, 04 Dec 2009 14:15:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-73626</guid>
		<description>Templates (covered later in this tutorial) can help you create a pretty nice, generic Convert function, that converts between arbitrary compatible values, such as strings and different number types (and even custom classes, if they overload operator&lt;&lt; and operator&gt;&gt; properly).

&lt;pre&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

template &lt;typename In_type, typename Out_type&gt;
Out_type Convert(const In_type &amp;value) {
    Out_type out;
    std::stringstream s;
    s &lt;&lt; value;
    s &gt;&gt; out;
    return out;
}

int main() {
    std::string strNum = &quot;1234&quot;;
    int dNum = Convert&lt;std::string, int&gt;(strNum);
    std::cout &lt;&lt; strNum &lt;&lt; &quot; == &quot; &lt;&lt; dNum &lt;&lt; std::endl;

    std::cout &lt;&lt; &quot;Enter a floating point value: &quot;;
    float fNum;
    std::cin &gt;&gt; fNum;
    std::string strFloat = Convert&lt;float, std::string&gt;(fNum);
    std::cout &lt;&lt; fNum &lt;&lt; &quot; == &quot; &lt;&lt; strFloat &lt;&lt; std::endl;

    return 0;
}
&lt;!--formatted--&gt;&lt;/pre&gt;

Output:
&lt;pre&gt;
1234 == 1234
Enter a floating point value: 481.3
481.3 == 481.3
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Templates (covered later in this tutorial) can help you create a pretty nice, generic Convert function, that converts between arbitrary compatible values, such as strings and different number types (and even custom classes, if they overload operator&lt;&lt; and operator&gt;&gt; properly).</p>
<pre>
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

template &lt;typename In_type, typename Out_type&gt;
Out_type Convert(const In_type &amp;value) {
    Out_type out;
    std::stringstream s;
    s &lt;&lt; value;
    s &gt;&gt; out;
    return out;
}

int main() {
    std::string strNum = &quot;1234&quot;;
    int dNum = Convert&lt;std::string, int&gt;(strNum);
    std::cout &lt;&lt; strNum &lt;&lt; &quot; == &quot; &lt;&lt; dNum &lt;&lt; std::endl;

    std::cout &lt;&lt; &quot;Enter a floating point value: &quot;;
    float fNum;
    std::cin &gt;&gt; fNum;
    std::string strFloat = Convert&lt;float, std::string&gt;(fNum);
    std::cout &lt;&lt; fNum &lt;&lt; &quot; == &quot; &lt;&lt; strFloat &lt;&lt; std::endl;

    return 0;
}
<!--formatted--></pre>
<p>Output:</p>
<pre>
1234 == 1234
Enter a floating point value: 481.3
481.3 == 481.3
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 17.2 &#8212; std::string construction and destruction</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-68710</link>
		<dc:creator>Learn C++ - &#187; 17.2 &#8212; std::string construction and destruction</dc:creator>
		<pubDate>Sun, 20 Sep 2009 18:11:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-68710</guid>
		<description>[...] 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&gt;&gt;, or via the str() function). For more information on std::ostringstream, see Lesson 13.4 &#8212; Stream classes for strings. [...]</description>
		<content:encoded><![CDATA[<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&gt;&gt;, or via the str() function). For more information on std::ostringstream, see Lesson 13.4 &#8212; Stream classes for strings. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-27038</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 11 Sep 2008 07:00:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-27038</guid>
		<description>You are correct, using erase() doesn&#039;t work because it clears a copy of the buffer.

That is interesting about the mixing of str() and the &lt;&lt; operator.  Doing so is kind of like mixing metaphors though -- better to stick with one or the other.</description>
		<content:encoded><![CDATA[<p>You are correct, using erase() doesn&#8217;t work because it clears a copy of the buffer.</p>
<p>That is interesting about the mixing of str() and the << operator.  Doing so is kind of like mixing metaphors though &#8212; better to stick with one or the other.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-27037</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Thu, 11 Sep 2008 06:56:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-27037</guid>
		<description>Thanks, I fixed the typo and made the distinction between &gt;&gt; and str() more clear.</description>
		<content:encoded><![CDATA[<p>Thanks, I fixed the typo and made the distinction between >> and str() more clear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grant</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-26266</link>
		<dc:creator>Grant</dc:creator>
		<pubDate>Thu, 04 Sep 2008 13:54:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-26266</guid>
		<description>Hi Alex,

There is an error in the section &lt;em&gt;Clearing a stringstream for reuse&lt;/em&gt;. The second suggestion of using erase does not work. I think this is because os.str() returns a &lt;em&gt;copy&lt;/em&gt; of the buffer and erase then just erases the copy leaving the original in tact. So the code in this case will still print &quot;Hello World!&quot;

Another point to note is that setting a buffer using str(&quot;xx&quot;) and then using the &lt;&lt; operator on the stream will not append (as I at first thought) but will overwrite. So the following code:

	stringstream os;
	os.str(&quot;xxxxxxxxxx&quot;);
	os &lt;&lt; &quot;World!&quot;;
	cout &lt;&lt; os.str() &lt;&lt; endl;



will output:

World!xxxx</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>There is an error in the section <em>Clearing a stringstream for reuse</em>. The second suggestion of using erase does not work. I think this is because os.str() returns a <em>copy</em> of the buffer and erase then just erases the copy leaving the original in tact. So the code in this case will still print &#8220;Hello World!&#8221;</p>
<p>Another point to note is that setting a buffer using str(&#8220;xx&#8221;) and then using the &lt;&lt; operator on the stream will not append (as I at first thought) but will overwrite. So the following code:</p>
<p>	stringstream os;<br />
	os.str(&quot;xxxxxxxxxx&quot;);<br />
	os &lt;&lt; &quot;World!&quot;;<br />
	cout &lt;&lt; os.str() &lt;&lt; endl;</p>
<p>will output:</p>
<p>World!xxxx</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grant</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-26250</link>
		<dc:creator>Grant</dc:creator>
		<pubDate>Thu, 04 Sep 2008 10:39:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-26250</guid>
		<description>Hi Alex,
First a typo in the &gt;&gt; example, the comment says &quot;print the numbers separated by a dash&quot; but the code just prints the first value.

Second you need to include  &lt; sstream &gt;  to get at stringstream.

Third, the behaviour of &gt;&gt; and str() is subtly different as shown by this code:

	stringstream os;
	os &lt;&lt; &quot;12345 67.89&quot;; // insert a string of numbers into the stream
	string strValue;
	os &gt;&gt; strValue;	// extract 1st value
	cout &lt;&lt; &quot;value1 &quot; &lt;&lt; strValue &lt;&lt; endl;
	os &gt;&gt; strValue;	// extract 2nd value
	cout &lt;&lt; &quot;value2 &quot; &lt;&lt; strValue &lt;&lt; endl;
	cout &lt;&lt; &quot;whole string &quot; &lt;&lt; os.str() &lt;&lt; endl; // print whole string!


which gives the following output:

value1 12345
value2 67.89
whole string 12345 67.89

In other words the &gt;&gt; operator takes values from the string, moving along every time it is used, but the str() function always returns the whole string no matter how many times it is called (even after using &gt;&gt;)</description>
		<content:encoded><![CDATA[<p>Hi Alex,<br />
First a typo in the &gt;&gt; example, the comment says &#8220;print the numbers separated by a dash&#8221; but the code just prints the first value.</p>
<p>Second you need to include  &lt; sstream &gt;  to get at stringstream.</p>
<p>Third, the behaviour of &gt;&gt; and str() is subtly different as shown by this code:</p>
<p>	stringstream os;<br />
	os &lt;&lt; &quot;12345 67.89&quot;; // insert a string of numbers into the stream<br />
	string strValue;<br />
	os &gt;&gt; strValue;	// extract 1st value<br />
	cout &lt;&lt; &quot;value1 &quot; &lt;&lt; strValue &lt;&lt; endl;<br />
	os &gt;&gt; strValue;	// extract 2nd value<br />
	cout &lt;&lt; &quot;value2 &quot; &lt;&lt; strValue &lt;&lt; endl;<br />
	cout &lt;&lt; &quot;whole string &quot; &lt;&lt; os.str() &lt;&lt; endl; // print whole string!</p>
<p>which gives the following output:</p>
<p>value1 12345<br />
value2 67.89<br />
whole string 12345 67.89</p>
<p>In other words the &gt;&gt; operator takes values from the string, moving along every time it is used, but the str() function always returns the whole string no matter how many times it is called (even after using &gt;&gt;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Learn C++ - &#187; 13.3 &#8212; Output with ostream and ios</title>
		<link>http://www.learncpp.com/cpp-tutorial/134-stream-classes-for-strings/comment-page-1/#comment-14434</link>
		<dc:creator>Learn C++ - &#187; 13.3 &#8212; Output with ostream and ios</dc:creator>
		<pubDate>Mon, 05 May 2008 02:31:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-programming/134-stream-classes-for-strings/#comment-14434</guid>
		<description>[...] 2007      Prev/Next Posts   &#171; 13.2 &#8212; Input with istream &#124; Home &#124; 13.4 &#8212; Stream classes for strings &#187;     Wednesday, March 12th, 2008 at 3:10 [...]</description>
		<content:encoded><![CDATA[<p>[...] 2007      Prev/Next Posts   &laquo; 13.2 &#8212; Input with istream | Home | 13.4 &#8212; Stream classes for strings &raquo;     Wednesday, March 12th, 2008 at 3:10 [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

