Jump to content
  • Advertisement
Sign in to follow this  
Rohithzhere

Understanding the code

This topic is 3677 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I was going through a few samples of msdn . I found this set of code. I am able to partially understand it. I would need ur help to understand the rest.
Here goes the code
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
#include <string>

using namespace std;


typedef vector < float > FloatArray;
typedef vector < string > StringArray;
typedef ostream_iterator <float, char, char_traits <char> > FloatOstreamIt; 

int main ()
{
	// a vector of floats
	FloatArray rgFA;

	// an ostream iterator that outputs a float to cout terminated
	// by a space
	FloatOstreamIt OstreamIt(cout,"\n");

	// Initialize the array to 1,1/2,1/3,...
	for (int i=0; i<100; i++) rgFA.push_back(i+1);

	// Print the array
	copy(rgFA.begin(),rgFA.end(),OstreamIt);
	cout << endl;




	//// Initialize array of strings
	//StringArray rgs;
	//rgs.push_back("This ");
	//rgs.push_back("is ");
	//rgs.push_back("one ");
	//rgs.push_back("sentence. ");

	//// Concatenate the strings in the array and print the sentence
	//cout << "The concatenated vector of strings: "
	//	<< accumulate(rgs.begin(),rgs.end(),string(""))
	//	<< endl;
}




What i didnt understand was the third parameter in the following line.. typedef ostream_iterator <float, char, char_traits <char> > FloatOstreamIt; what is the char_traits<char> stuff? . First parameter is the input to the custom cout Second is the output Atleast am i right with these two? Thanks and Regards Rohith.H.N

Share this post


Link to post
Share on other sites
Advertisement
Standard library stream objects are template classes. std::ostream is actually a typedef for std::basic_ostream<char, std::char_traits<char> >. When you use an ostream_iterator, the second and third template paramters need to match the template parameters for the stream you're writing to. Since std::cout is a std::ostream, the ostream_iterator has to have char and std::char_traits<char> as the second and third template parameters.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!