vector of ostringstream

A

Alex Vinokur

Is it possible to use vector<ostringstream> ?

Here is what I have got.

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================

// File t.cpp
--------- C++ code : BEGIN ---------
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;

int main ()
{
vector<ostringstream> oss;

oss.push_back (ostringstream()); // Line#10
oss.front() << "ABCD" << endl;

cout << oss.front().str();

return 0;
}
--------- C++ code : END -----------


--------- Compilation : BEGIN ---------

$ g++ t.cpp

/usr/include/c++/3.2/bits/ios_base.h: In copy constructor `std::basic_ios<char,
std::char_traits<char> >::basic_ios(const std::basic_ios<char,
std::char_traits<char> >&)':
/usr/include/c++/3.2/bits/stl_construct.h:78: instantiated from `void std::_Construct(_T1*, const _T2&) [with _T1 =
std::eek:stringstream, _T2 = std::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >]'
/usr/include/c++/3.2/bits/stl_vector.h:492: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
std::eek:stringstream, _Alloc = std::allocator<std::eek:stringstream>]'
t.cpp:10: instantiated from here
/usr/include/c++/3.2/bits/ios_base.h:421: `std::ios_base::ios_base(const
std::ios_base&)' is private
/usr/include/c++/3.2/bits/stl_construct.h:78: within this context
/usr/include/c++/3.2/streambuf: In copy constructor `std::basic_stringbuf<char,
std::allocator<char> >::basic_stringbuf(const said:
/usr/include/c++/3.2/streambuf:486: `std::basic_streambuf<_CharT,
_Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&)
[with _CharT = char, _Traits = std::char_traits<char>]' is private
/usr/include/c++/3.2/bits/stl_construct.h:78: within this context
/usr/include/c++/3.2/bits/ios_base.h: In member function `std::basic_ios<char,
std::char_traits said:
::eek:perator=(const std::basic_ios<char, std::char_traits<char> >&)':
/usr/include/c++/3.2/bits/stl_vector.h:893: instantiated from `void std::vector<_Tp,
_Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<_Tp*, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std::eek:stringstream,
_Alloc = std::allocator<std::eek:stringstream>]'
/usr/include/c++/3.2/bits/stl_vector.h:496: instantiated from `void std::vector<_Tp, _Alloc>::push_back(const _Tp&) [with _Tp =
std::eek:stringstream, _Alloc = std::allocator<std::eek:stringstream>]'
t.cpp:10: instantiated from here
/usr/include/c++/3.2/bits/ios_base.h:424: `std::ios_base&
std::ios_base::eek:perator=(const std::ios_base&)' is private
/usr/include/c++/3.2/bits/stl_vector.h:893: within this context
/usr/include/c++/3.2/streambuf: In member function `std::basic_stringbuf<char,
std::char_traits<char>, std::allocator<char> >& std::basic_stringbuf<char,
std::allocator<char> >::operator=(const said:
/usr/include/c++/3.2/streambuf:489: `std::basic_streambuf<_CharT, _Traits>&
std::basic_streambuf<_CharT, _Traits>::eek:perator=(const
std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits =
std::char_traits<char>]' is private
/usr/include/c++/3.2/bits/stl_vector.h:893: within this context

--------- Compilation : END -----------

--
==========================================
Alex Vinokur
mailto:[email protected]
http://www.simtel.net/pub/oth/19088.html
http://sourceforge.net/users/alexvn
==========================================
 
G

Gianni Mariani

Alex said:
Is it possible to use vector<ostringstream> ?

Here is what I have got.

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================

// File t.cpp
--------- C++ code : BEGIN ---------
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;

int main ()
{
vector<ostringstream> oss;

oss.push_back (ostringstream()); // Line#10
oss.front() << "ABCD" << endl;

cout << oss.front().str();

return 0;
}
--------- C++ code : END -----------

This is probably not what you truly want to do.

An ostringstream is probably not assignable, meaning you can't "push" them.

The code below blows up as well.

#include <sstream>
#include <iostream>
using namespace std;

int main ()
{

ostringstream a;
ostringstream b;

a = b;

return 0;
}

vector objects need to be assignable so you're going to have a hard time
with vector<ostringstream>.

You could create :

vector<ostringstream *>

But then you need to know how to delete them. One suggestion is to
create a class like:

class foo
: public ostringstream,
public lifetime_manager
{

};


class foo_pointer
{

foo * m_foo;

public:

operator ostringstream &()
{
return * m_foo;
}

.... all other reference counting smart pointer things like
constructors, destructors and assignment operators ... I can give you a
template for these ...

};


Now you can write

vector<foo_pointer> oss;

oss.push_back (new foo());

oss.front() << "ABCD" << endl;


I know it's a bit terse, but depending on what you truly trying to do,
you might have a more appropriate alternative.

G
 
T

Thomas Maeder

Alex Vinokur said:
Is it possible to use vector<ostringstream> ?

No. Vector element types have to be copyable, which stream types aren't.

/usr/include/c++/3.2/bits/ios_base.h:421: `std::ios_base::ios_base(const
std::ios_base&)' is private

This is how the "noncopyability" is implemented.
 
G

Guy Harrison

Alex said:
Is it possible to use vector<ostringstream> ?

Nope, not like that. Never tried it myself until now but those errors are
basically telling you its impossible.
Here is what I have got.

===========================================
Windows 2000
CYGWIN_NT-5.0 1.3.22(0.78/3/2)
GNU gcc version 3.2 20020927 (prerelease)
===========================================

// File t.cpp
--------- C++ code : BEGIN ---------
#include <vector>
#include <sstream>
#include <iostream>
using namespace std;

int main ()
{
vector<ostringstream> oss;

oss.push_back (ostringstream()); // Line#10
oss.front() << "ABCD" << endl;

cout << oss.front().str();

return 0;
}

vector<string> methinks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top