ostream_iterator and vector< pair<int, string> >

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • subramanian100in@yahoo.com, India

    #1

    ostream_iterator and vector< pair<int, string> >

    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, stringis;
    vector<isv;

    // store values in v

    ofstream ofs("output_fil e_word_count");
    ostream_iterato r<isosi(ofs);

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

    return 0;
    }

    Is it possible to print pair<int, stringwith ostream_iterato r ?

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

    Kindly explain.

    Thanks
    V.Subramanian

  • Chris ( Val )

    #2
    Re: ostream_iterato r and vector&lt; pair&lt;int, string&gt; &gt;

    On Sep 30, 8:17 pm, "subramanian10. ..@yahoo.com, India"
    <subramanian10. ..@yahoo.comwro te:
    Consider the following piece of code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <utility>
    #include <iterator>
    #include <algorithm>
    [snip]

    std::ostream& operator << ( std::ostream& out,
    const std::pair<int, std::stringrhs )
    {
    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

    Comment

    • James Kanze

      #3
      Re: ostream_iterato r and vector&lt; pair&lt;int, string&gt; &gt;

      On Sep 30, 12:17 pm, "subramanian10. ..@yahoo.com, India"
      <subramanian10. ..@yahoo.comwro te:
      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, stringis;
      vector<isv;
      // store values in v
      ofstream ofs("output_fil e_word_count");
      ostream_iterato r<isosi(ofs);
      copy(v.begin(), v.end(), osi); ------------------------this line
      does not compile.
      return 0;
      }
      Is it possible to print pair<int, stringwith
      ostream_iterato r ?
      Yes and no. ostream_iterato r requires an operator>>. There is
      no operator>for std::pair< int, std::string >, and no way to
      define one in a way that ostream_iterato r 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_iterato r with that. Something like:

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

      friend std::ostream&
      operator>>( std::ostream& dest, FmtIntStringPai r 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_it erator< FmtIntStringPai r for output.
      For example if I had vector<string>, I am able to use
      ostream_iterato r.
      There *is* an >operator for std::string. In std::, where it
      can be found.

      --
      James Kanze (GABI Software) email:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      Working...