Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector of ostringstream

Reply
Thread Tools

vector of ostringstream

 
 
Alex Vinokur
Guest
Posts: n/a
 
      07-10-2003
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: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>:ush_back(const _Tp&) [with _Tp =
std:stringstream, _Alloc = std::allocator<std: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::char_traits<char>, std::allocator<char> >::basic_stringbuf(const
std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char>
>&)':

/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<char> >& std::basic_ios<char, std::char_traits<char>
>: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_iterato r<_Tp*, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp = std:stringstream,
_Alloc = std::allocator<std:stringstream>]'
/usr/include/c++/3.2/bits/stl_vector.h:496: instantiated from `void std::vector<_Tp, _Alloc>:ush_back(const _Tp&) [with _Tp =
std:stringstream, _Alloc = std::allocator<std:stringstream>]'
t.cpp:10: instantiated from here
/usr/include/c++/3.2/bits/ios_base.h:424: `std::ios_base&
std::ios_base: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::char_traits<char>, std::allocator<char> >:perator=(const
std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char>
>&)':

/usr/include/c++/3.2/streambuf:489: `std::basic_streambuf<_CharT, _Traits>&
std::basic_streambuf<_CharT, _Traits>: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
(E-Mail Removed)
http://www.simtel.net/pub/oth/19088.html
http://sourceforge.net/users/alexvn
==========================================


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      07-10-2003
Alex Vinokur wrote:
> 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


 
Reply With Quote
 
 
 
 
Thomas Maeder
Guest
Posts: n/a
 
      07-10-2003
"Alex Vinokur" <(E-Mail Removed)> writes:

> 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.
 
Reply With Quote
 
Guy Harrison
Guest
Posts: n/a
 
      07-11-2003
Alex Vinokur wrote:

> 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!


--
Guy Harrison
 
Reply With Quote
 
llewelly
Guest
Posts: n/a
 
      07-14-2003
"Alex Vinokur" <(E-Mail Removed)> writes:

> Is it possible to use vector<ostringstream> ?


No. ostringstream is not copyable.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
std::vector<char> or std::ostringstream Chris C++ 3 02-17-2004 08:45 PM



Advertisments