473,231 Members | 2,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

vectorstream analogous to std::stringstream

Is there a vectorstream class that implements the functionality similar to
std::stringstream but with std::vector, not std::string?

cheers,
Marcin
Feb 7 '06 #1
5 4128

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:0g*******************@newsfe7-gui.ntli.net...
Is there a vectorstream class that implements the functionality similar to
std::stringstream but with std::vector, not std::string?


What would you want it to do that stringstream doesn't?

-Mike
Feb 7 '06 #2
In article <0g*******************@newsfe7-gui.ntli.net>,
"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote:
Is there a vectorstream class that implements the functionality similar to
std::stringstream but with std::vector, not std::string?


A vector of what? Such a beast doesn't exist, but it is easy to copy the
contents to a vector...

void copy_to_vec( iostream& ios, vector<char>& vec )
{
vec.clear();
ios << noskipws; // optional
copy( istream_iterator<char>( ios ), istream_iterator<char>(),
back_inserter( vec ) );
}

Why do you want to do this? There is probably a better way to do what
you want.

--
Magic depends on tradition and belief. It does not welcome observation,
nor does it profit by experiment. On the other hand, science is based
on experience; it is open to correction by observation and experiment.
Feb 7 '06 #3

"Marcin Kalicinski" <ka****@poczta.onet.pl> wrote in message
news:0g*******************@newsfe7-gui.ntli.net...
| Is there a vectorstream class that implements the functionality
similar to
| std::stringstream but with std::vector, not std::string?
|
| cheers,
| Marcin
|

No, since a std::string is a predefined entity while a vector could be a
vector of anything.

However, encapsulating a templated vector and overloading a global
operator<< and operator>> to stream a vector's contents is a trivial
exercise with great benefits.

_____
// Container.h

#include <vector>
#include <algorithm>
#include <iterator>

template< class T >
class Container
{
std::vector< T > vt;
public:
Container() : vt() { }
Container(const Container& copy_)
{
vt = copy_.vt; // or std::swap
}
~Container() { }
/* member functions */
void push_back(T t)
{
vt.push_back(t);
}
/* friends */
friend std::ostream& operator<<(std::ostream&, const Container&);
}; // class Container

template< class T >
std::ostream& operator<<(std::ostream& os, const Container< T >& cont)
{
std::copy( cont.vt.begin(),
cont.vt.end(),
std::ostream_iterator< T >(os, " ") );
return os;
}
__________
// test.cpp

#include <iostream>
#include <ostream>
#include "Container.h"

int main()
{
Container<int> container;
for (int i = 0; i < 10; ++i)
{
container.push_back(i);
}
std::cout << container << std::endl;

Container<int> c2(container); // copy
std::cout << c2 << std::endl;

return 0;
}

/*
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
*/

And that Container can be a container of anything, including a container
of a complex user-type with itself having an appropriate operator<<
overload.

To give you a perspective of the power and flexibility involved, lets
have the container display its size and then its contents. Thats a
simple modification.

template< class T >
std::ostream& operator<<(std::ostream& os, const Container< T >& cont)
{
os << "container size = " << cont.vt.size() << "\n"; // mod
std::copy( cont.vt.begin(),
cont.vt.end(),
std::ostream_iterator< T >(os, " ") );
return os;
}

/*
container size = 10
0 1 2 3 4 5 6 7 8 9
container size = 10
0 1 2 3 4 5 6 7 8 9
*/

Do you see why no std::vectorstream was ever included with the standard?
What if i wanted a comma between the elements? What if i wanted no
spaces?

While some languages limit you to a finite number of tools and
possibilities: standard C++ begs, pleads and becons you to create,
fashion and expand what is available with relatively little effort.

Feb 7 '06 #4
>> Is there a vectorstream class that implements the functionality similar
to std::stringstream but with std::vector, not std::string?


What would you want it to do that stringstream doesn't?


I'm using boost::serialization, and I want it to serialize my data into raw
memory. Using stringstream it would look like that:

std::stringstream stream;
binary_oarchive oa(stream);
oa << some_object;
std::string buffer = stream.str(); // #1
char *raw = buffer.data(); // #2

#1 - redundant string copy operation
#2 - data() is potentially costly because string does not guarantee that its
contents are continuous in the memory

Because of that I'd like to use std::vector instead of std::string.

cheers,
Marcin
Feb 7 '06 #5
Marcin Kalicinski wrote:
Is there a vectorstream class that implements the functionality similar to
std::stringstream but with std::vector, not std::string?


There is none in the standard C++ library but it is actually pretty
easy to create streams which read or write from a 'std::vector<char>'.
The class for reading from an 'std::vector<char>' would look
something like this:

class vectorinbuf:
public std::streambuf
{
public:
vectorinbuf(std::vector<char>& vec) {
this->setg(&vec[0], &vec[0], &vec[0] + vec.size());
}
};
class ivectorstream:
private virtual vectorinbuf,
public std::istream
{
public:
ivectorstream(std::vector<char>& vec):
vectorinbuf(vec),
std::istream(this)
{
}
};

The class for writing to a vector is a little bit more complex but
not much:

class vectoroutbuf:
public std::streambuf
{
public:
vectoroutbuf(std::vector<char>& vec):
m_vector(vec)
{
}
int overflow(int c)
{
if (c != std::char_traits<char>::eof())
m_vector.push_back(c);
return std::char_traits<char>::not_eof(c);
}
private:
std::vector<char> m_vector;
};
class ovectorstream:
private virtual vectoroutbuf,
public std::ostream
{
public:
ovectorstream(std::vector<char>& vec):
vectoroutbuf(vec),
std::ostream(this)
{
}
};

The behavior of the output stream is not tuned for performance,
though: writing of each individual character goes through a
virtual function call. This can be improved by some form of
buffering and synchronization. For example, the class could use
the initial size of the 'std::vector<char>' as a buffer instead
of appending to the vector. In this case, it would be necessary
to have a suitable call which adjusts the size of the vector to
the actual number of characters written.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence
Feb 8 '06 #6

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

Similar topics

6
by: Giampiero Gabbiani | last post by:
Is it possible to reset a std::stringstream in order to reuse it once more? I tried with flush() method without any success... Thanks in advance Giampiero
2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
1
by: magix | last post by:
I got this reply in my previous post a month ago: May I know, how can I automatically create the folder if it doesn't exist ? In previous reply, it said: -------------------------...
2
by: akitoto | last post by:
Hi there, I am using the std::stringstream to create a byte array in memory. But it is not working properly. Can anyone help me? Code: #include <vector> #include <sstream> #include...
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.