473,666 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::vector<cha r> or std::ostringstr eam

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
Jul 22 '05 #1
3 2796

"Chris" <ch***@tuxweb.o rg> wrote in message
news:10******** *****@corp.supe rnews.com...
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


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
Jul 22 '05 #2
> vector<char> in your streambuf derived class for reading.


for writing, of course.

john
Jul 22 '05 #3
John Harrison wrote:
vector<char > in your streambuf derived class for reading.

for writing, of course.

john

Thanks for the help john. thats the path i went/am still going down :) .
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5674
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have seen commercial programs print output to the screen as well as to a log file. depending on the user and other situations, i might want to turn off one of the outputs or maybe even both outputs. so, i want a single line with operator << function...
4
20043
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this variable into a form I can display in a text box, but i'm not sure what to expect inside of the variable, whether I can just treat it like an array e.t.c. Any help would be appreciated. Thanks,
0
8444
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7386
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5664
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4368
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2771
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1775
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.