R. Clayton (rclayton@monmouth.edu)
(no date)
I tried using the transform() generic algorithm to convert string to lower case
and got an error. What am I doing wrong?
Contrary to what I had on yesterday's slide, transform() takes four
arguments:
std::transform(src_start, src_end, dst_start, fun)
For example,
$ cat t.cc
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>
int main() {
std::string s = "HI BIFF!!!!";
std::transform(s.begin(), s.end(), s.begin(), tolower);
std::cout << "s = '" << s << "'.\n";
}
$ g++ -o t -ansi -pedantic -Wall t.cc
$ ./t
s = 'hi biff!!!!'.
$
And now, an extra special bounus: why I love C++. Some of you may have
noticed that I didn't use std::tolower in the transform() call, even though I
recommended that you tag standard functions for documentation. Why didn't I?
It turns out that the code won't compile with std::tolower:
$ cat t.cc
#include <string>
#include <algorithm>
#include <iostream>
#include <cctype>
int main() {
std::string s = "HI BIFF!!!!";
std::transform(s.begin(), s.end(), s.begin(), std::tolower);
std::cout << "s = '" << s << "'.\n";
}
$ g++ -c t.cc
t.cc: In function `int main()':
t.cc:9: no matching function for call to `transform(
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >,
__gnu_cxx::__normal_iterator<char*, std::basic_string<char,
std::char_traits<char>, std::allocator<char> > >, <unknown type>)'
$ CC -c t.cc
"t.cc", line 9: Error: Could not find a match for
std::transform<std::InputIterator, std::OutputIterator,
std::UnaryOperation>(char*, char*, char*, extern "C" int(int)).
1 Error(s) detected.
$
And, even better, at least in one case the problem appears to have something
to do with <iostream>:
$ cat t.cc
#include <string>
#include <algorithm>
// #include <iostream>
#include <cctype>
int main() {
std::string s = "HI BIFF!!!!";
std::transform(s.begin(), s.end(), s.begin(), std::tolower);
// std::cout << "s = '" << s << "'.\n";
}
$ g++ -c t.cc
$ CC -c t.cc
"t.cc", line 9: Error: Could not find a match for
std::transform<std::InputIterator, std::OutputIterator,
std::UnaryOperation>(char*, char*, char*, extern "C" int(int)).
1 Error(s) detected.
$
I have no idea why.
This archive was generated by hypermail 2.0b3 on Thu Dec 19 2002 - 20:30:05 EST