[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string and strstream and setw
From: |
Maurizio Loreti |
Subject: |
Re: string and strstream and setw |
Date: |
19 Feb 2001 13:48:32 +0100 |
address@hidden (Georg Ritter) writes:
> Hi,
>
> wrong behavior:
> When trying to align strings (<string>) with ios::manipulaltors they
> aren't:
>
> #include <string>
> #include <strtream>
> #include <iomanip>
> #include <iostream>
>
> int main()
> {
> ostrstream a, b;
> string str1("one");
> string str2("two");
>
> a.setf( ios::right );
> b.setf( ios::right );
>
> a << setw(20) << str1 << setw(20) << str2 << endl;
> b << setw(20) << str1.c_str() << setw(20) << str2.c_str() << endl;
>
> cout << "This does not work" << endl;
> cout << a;
>
> cout << "This does: " << endl;
> cout << b;
>
> return 0;
> }
>
> On an HP the code produces:
> This does not work
> one two
> This does:
> one two
Your example works perfectly with the g++ developement version
(20010205 snapshot), after:
- having replaced the non-standard header <strtream> with the standard
one <sstream>;
- having qualified all symbols belonging to namespace std, i.e. having
changed string to std::string, endl to std::endl, and so on;
- having changed the declaration of a and b to the standard type
std::ostringstream from the non-standard one ostrstream;
- having changed "cout << a;" to "std::cout << a.str();", assuming you
want the content of the string stream and not its address in memory;
- having changed in the same way "cout << b;".
Hope that helps, M.
--
Maurizio Loreti http://www.pd.infn.it/~loreti/mlo.html
Univ. of Padova, Dept. of Physics - Padova, Italy address@hidden
The instructions said to use Windows 98 or better - so I installed FreeBSD.