std::vector<char> or std::ostringstream

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    std::vector<char> or std::ostringstream

    Hi,

    I'm playing/developing a simple p2p file sharing client for an existing
    protocol.

    A 2 second description of the protocol is

    Message Length = int, 4 bytes
    Message Code = int, 4 bytes
    Data

    When creating messages is easy to use a std::ostringstr eam and when
    reading received messages, it easy to use a std::vector<cha r>.

    I unclear as to which is more appropriate/efficient for the task and
    was hoping some light could be shared on this, i have asked on irc
    and been told 'it depends'. What does it depend on? :)

    Thanks.
    Chris
  • John Harrison

    #2
    Re: std::vector&lt; char&gt; or std::ostringstr eam


    "Chris" <chris@tuxweb.o rg> wrote in message
    news:102us8pqpe rsjed@corp.supe rnews.com...[color=blue]
    > Hi,
    >
    > I'm playing/developing a simple p2p file sharing client for an existing
    > protocol.
    >
    > A 2 second description of the protocol is
    >
    > Message Length = int, 4 bytes
    > Message Code = int, 4 bytes
    > Data
    >
    > When creating messages is easy to use a std::ostringstr eam and when
    > reading received messages, it easy to use a std::vector<cha r>.
    >
    > I unclear as to which is more appropriate/efficient for the task and
    > was hoping some light could be shared on this, i have asked on irc
    > and been told 'it depends'. What does it depend on? :)
    >
    > Thanks.
    > Chris[/color]

    Well the most important point is that you create a Message class and that
    whatever representation you choose is internal to that. Apart from anything
    else it will isolate any changes to the representation from the rest of the
    program.

    The advantage of ostringstream is that its give you nice formatting, the
    advantage of vector<char> is that you can resize to fit the message length.
    Maybe the best answer would be a combination of the two. Write a
    std::streambuf derived class which wraps a vector<char> held as a reference
    (have a look at std::stringbuf, your code will be very similar). Now your
    Message class can use a vector<char> directly for reading and wrap the same
    vector<char> in your streambuf derived class for reading.

    Or something like that.

    john


    Comment

    • John Harrison

      #3
      Re: std::vector&lt; char&gt; or std::ostringstr eam

      > vector<char> in your streambuf derived class for reading.[color=blue]
      >[/color]

      for writing, of course.

      john


      Comment

      • Chris

        #4
        Re: std::vector&lt; char&gt; or std::ostringstr eam

        John Harrison wrote:[color=blue][color=green]
        >>vector<char > in your streambuf derived class for reading.
        >>[/color]
        >
        >
        > for writing, of course.
        >
        > john
        >
        >[/color]
        Thanks for the help john. thats the path i went/am still going down :) .

        Comment

        Working...