473,397 Members | 2,077 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,397 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 4154

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.