Topic: Stroustrup example and STL...
Author: "Robert C. Paulsen, Jr." <paulsen@ZAP.imailbox.com>
Date: 1998/09/23 Raw View
P.J. Plauger wrote:
>
> VC++ V5.0 has enough bugs in member templates to make
> support of the Standard C++ library problematic, so I left 'em
> out. What you can do instead is construct an empty vector,
> then use template function copy to fill the vector, as in:
>
> std::vector<std::string> vs();
> std::copy(ii, eos, std::back_inserter(vs));
>
> (NB: I haven't tested this snipped -- just shooting from the hip.)
>
I don't have MSVC++5.0, but the following works in 6.0.
In addition to your suggested change I had to change the definition of
the istream_iterators. Note that the two lines that had to be changed
cause this to fail under BC++5.02.
=====================================================================
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <functional> // would have been needed for less<>
#include <vector>
#include <assert.h>
using namespace std;
int main()
{
ifstream from("test.txt");
assert(from);
// istream_iterator<string, less<string> > ii(from);
// istream_iterator<string, less<string> > eos;
//
// the above doesn't work in MSVC++6.0 but the following does...
//
istream_iterator<string> ii(from);
istream_iterator<string> eos;
vector<string> vs;
copy(ii, eos, back_inserter(vs) );
return 0;
}
=====================================================================
--
Robert Paulsen http://paulsen.home.texas.net
If my return address contains "ZAP." please remove it. Sorry for the
inconvenience but the unsolicited email is getting out of control.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ryszard Kabatek <rysio@rumcajs.chemie.uni-halle.de>
Date: 1998/09/24 Raw View
The, free, egcs-1.1 compiler, http://egcs.cygnus.com,
does compile the sample.
It uses the SGI`s STL, http://www.sgi.com/Technology/STL/.
It`s a free implementation too.
I just did two little modifications to Your code.
The first was necessary for egcs, the second just for a check.
/*************************** CODE *************************/
#include <iostream>
#include <fstream>
//#include <istream> (R. K.)
#include <string>
#include <algorithm>
#include <vector>
int main()
{
std::ifstream from("test.txt");
if (!from)
{
std::cerr << "Could not open test.txt\n";
exit(1);
}
std::istream_iterator<std::string> ii(from);
std::istream_iterator<std::string> eos;
std::vector<std::string> vs(ii, eos);
// For a check. (R. K.)
for (size_t i = 0; i < vs.size(); ++i) std::cout << vs[i] << '\n';
return 0;
}
/************ END OF CODE ******************************/
$ cat test.txt
Bim bam bom.
$ g++ main.cc
$ a.out
Bim
bam
bom.
Reed Mangino wrote:
>
> On page 61 of Stroustrup's 3rd Edition of _The C++ Programming
> Language_ there is an example very similar to this one:
>
> /*************************** CODE *************************/
> #include <iostream>
> #include <fstream>
> #include <istream>
> #include <string>
> #include <algorithm>
> #include <vector>
>
> int main()
> {
>
> std::ifstream from("test.txt");
>
> if (!from)
> {
> std::cerr << "Could not open test.txt\n";
> exit(1);
> }
> std::istream_iterator<std::string> ii(from);
> std::istream_iterator<std::string> eos;
>
> std::vector<std::string> vs(ii, eos);
>
> return 0;
> }
> /************ END OF CODE ******************************/
>
> The result should be that vs contains the string data that
> was present in the test.txt file. I have not been able to
> find one C++ compiler that would successfully compile this
> source (I have also tried several STL implementations...)
>
> If you have any ideas... I would be happy to hear them!
> Reed
>
Ryszard Kabatek
--
Martin-Luther University Halle-Wittenberg
Department of Physical Chemistry
Geusaer Str. 88, 06217 Merseburg, Germany
Tel. +49 3461 46 2487 Fax. +49 3461 46 2129
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: mangino@pandora.planet.net (Reed Mangino)
Date: 1998/09/23 Raw View
On page 61 of Stroustrup's 3rd Edition of _The C++ Programming
Language_ there is an example very similar to this one:
/*************************** CODE *************************/
#include <iostream>
#include <fstream>
#include <istream>
#include <string>
#include <algorithm>
#include <vector>
int main()
{
std::ifstream from("test.txt");
if (!from)
{
std::cerr << "Could not open test.txt\n";
exit(1);
}
std::istream_iterator<std::string> ii(from);
std::istream_iterator<std::string> eos;
std::vector<std::string> vs(ii, eos);
return 0;
}
/************ END OF CODE ******************************/
The result should be that vs contains the string data that
was present in the test.txt file. I have not been able to
find one C++ compiler that would successfully compile this
source (I have also tried several STL implementations...)
Even if I use Stroustrup example verbatim, the results are
the same...
The compiler error that I get from VC++ 5.0 is (and I applogize
for the format of this error, I couldn't find a good way to parse
it so that it would be easy for you to read...)
:\TEMP\streamtest.cpp(21) : error C2664: 'std::vector<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char>>,class std::allocator<class
std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char>>
>>::vector<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char>>,class
std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char>>>>(unsigned int,const
class std::basic_strin
g<char,struct std::char_traits<char>,class std::allocator<char>> &,const
class std::allocator<class std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char>>> &)' : cannot convert
parameter 1 from 'class std::istream_iterator<
class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<char>>,char,struct std::char_traits<char>>' to 'unsigned
int'
If you have any ideas... I would be happy to hear them!
Reed
--
^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^-__-^^^-__-^^
Reed R. Mangino | The only problem with the
| gene pool is that there is
mangino@planet.net | no lifeguard
-----------------------------------------------------------
** Remove NOSPAM_ from my return address to respond to me **
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: 1998/09/23 Raw View
Reed Mangino <mangino@pandora.planet.net> wrote in article <6u8moo$9u2@jupiter.planet.net>...
> std::istream_iterator<std::string> ii(from);
> std::istream_iterator<std::string> eos;
>
> std::vector<std::string> vs(ii, eos);
> .....
> The result should be that vs contains the string data that
> was present in the test.txt file. I have not been able to
> find one C++ compiler that would successfully compile this
> source (I have also tried several STL implementations...)
Should work fine with several compilers that use the Dinkum
C++ Library, most notably IBM Visual Age V4.0. The compiler
must support member templates, because you're using the
template constructor:
template<class InIt>
vector(InIt first, InIt last);
VC++ V5.0 has enough bugs in member templates to make
support of the Standard C++ library problematic, so I left 'em
out. What you can do instead is construct an empty vector,
then use template function copy to fill the vector, as in:
std::vector<std::string> vs();
std::copy(ii, eos, std::back_inserter(vs));
(NB: I haven't tested this snipped -- just shooting from the hip.)
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com/hot_news.html
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]