ostream_iterator and vector< pair<int, string> >

S

subramanian100in

Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>

int main()
{
typedef pair<int, string> is;
vector<is> v;

// store values in v

ofstream ofs("output_file_word_count");
ostream_iterator<is> osi(ofs);

copy(v.begin(), v.end(), osi); ------------------------> this line
does not compile.

return 0;
}

Is it possible to print pair<int, string> with ostream_iterator ?

For example if I had vector<string>, I am able to use
ostream_iterator.

Kindly explain.

Thanks
V.Subramanian
 
K

Kai-Uwe Bux

Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>

int main()
{
typedef pair<int, string> is;
vector<is> v;

// store values in v

ofstream ofs("output_file_word_count");
ostream_iterator<is> osi(ofs);

copy(v.begin(), v.end(), osi); ------------------------> this line
does not compile.

return 0;
}

Is it possible to print pair<int, string> with ostream_iterator ?

For example if I had vector<string>, I am able to use
ostream_iterator.

In order to use ostream_iterator<T>, you need to overload

std::eek:stream& operator<< ( std::eek:stream&, T const &);

or some variation thereof so that the expression

ostr << t

is well-formed when t is of type T.


Such an overload does not exist for std::pair<S,T> (regardless of S and T).
You need to provide one.

Note that there can be an additional problem arising from lookup rules. Your
operator<< needs to be found through Koenig lookup, and since pair<> is a
template in the standard namespace, that might (but need not) prove tricky.


Best

Kai-Uwe Bux
 
C

Chris ( Val )

Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>

[snip]

std::eek:stream& operator << ( std::eek:stream& out,
const std::pair<int, std::string> rhs )
{
return out << rhs.first << rhs.second << '\n';
}

Just because you use an iterator that works for std::string
doesn't mean that it will work for std::pair<>. You need to
provide an overload so the compiler knows how to send it to
the output stream.

Cheers,
Chris Val
 
J

James Kanze

Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>
int main()
{
typedef pair<int, string> is;
vector<is> v;
// store values in v
ofstream ofs("output_file_word_count");
ostream_iterator<is> osi(ofs);
copy(v.begin(), v.end(), osi); ------------------------> this line
does not compile.
return 0;
}
Is it possible to print pair<int, string> with
ostream_iterator ?

Yes and no. ostream_iterator requires an operator>>. There is
no operator>> for std::pair< int, std::string >, and no way to
define one in a way that ostream_iterator will can find it. And
of course, you don't want to be able to define one anyway, since
if you could, other developers in the project could as well, and
you'd end up with clashes.

What you can do is define a formatting class, which will convert
implicitly from std::pair< int, std::string >, and use an
ostream_iterator with that. Something like:

class FmtIntStringPair
{
public:
FmtIntStringPair( std::pair< int, std::string > const& obj )
: myObj( &obj )
{
}

friend std::eek:stream&
operator>>( std::eek:stream& dest, FmtIntStringPair const& obj )
{
dest << "[" << obj.myObj->first
<< ": " << obj.myObj->second << "]" ;
// Or whatever formatting you want...
return dest ;
}

private:
// Use pointer so assignment is defined...
std::pair< int, std::string > const*
myObj ;
} ;

Then use std::ostream_iterator said:
For example if I had vector<string>, I am able to use
ostream_iterator.

There *is* an >> operator for std::string. In std::, where it
can be found.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top