Using tolower in transform

T

TheDD

Hello,

i don't manage to use the tolower() function:

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>

using std::cout;
using std::ctype;
using std::endl;
using std::locale;
using std::string;
using std::transform;
using std::use_facet;

typedef ctype< char > char_ctype_facet;

int
main()
{
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

cout << str << endl;
return 0;
}

$ g++ -W -Wall -std=c++98 -pedantic tolower.cc
tolower.cc: In function `int main()':
tolower.cc:24: error: 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>)'

ct.tolower is <unknow type>

What am i doing wrong?
 
V

Victor Bazarov

TheDD said:
Hello,

i don't manage to use the tolower() function:

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>

using std::cout;
using std::ctype;
using std::endl;
using std::locale;
using std::string;
using std::transform;
using std::use_facet;

typedef ctype< char > char_ctype_facet;

int
main()
{
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

Wouldn't

transform(str.begin(), str.end(), str.begin(), tolower);

do the trick?
cout << str << endl;
return 0;
}

$ g++ -W -Wall -std=c++98 -pedantic tolower.cc
tolower.cc: In function `int main()':
tolower.cc:24: error: 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>)'

ct.tolower is <unknow type>

What am i doing wrong?

Not sure.

Victor
 
T

TheDD

Victor said:
[...]
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

Wouldn't

transform(str.begin(), str.end(), str.begin(), tolower);

do the trick?

yes it does, thx a lot.

But in this case, i guess the used locale is "C". What can i do to use
another one?
 
V

Victor Bazarov

TheDD said:
Victor Bazarov wrote:

[...]
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

Wouldn't

transform(str.begin(), str.end(), str.begin(), tolower);

do the trick?


yes it does, thx a lot.

But in this case, i guess the used locale is "C". What can i do to use
another one?

Good question. Could you temporarily set it as "global one to use"
somehow? I am not that familiar with locales, as you probably figured
out by now...

V
 
T

TheDD

Victor said:
Good question. Could you temporarily set it as "global one to use"
somehow? I am not that familiar with locales, as you probably figured
out by now...

Well i can use:

locale::global(locale(""));

but it's not really great. I'm going to read the docs one more time :)
 
V

Victor Bazarov

TheDD said:
i don't manage to use the tolower() function:

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>

using std::cout;
using std::ctype;
using std::endl;
using std::locale;
using std::string;
using std::transform;
using std::use_facet;

typedef ctype< char > char_ctype_facet;

int
main()
{
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

If you have to use a member function, try something like

std::transform(str.begin(), str.end(), str.begin(),
std::bind1st(std::mem_fun(&ctype<char>::tolower), ct));

I couldn't get it to compile, but dig in that direction.
cout << str << endl;
return 0;
}

$ g++ -W -Wall -std=c++98 -pedantic tolower.cc
tolower.cc: In function `int main()':
tolower.cc:24: error: 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>)'

ct.tolower is <unknow type>

What am i doing wrong?

To use a member function, you need 'mem_fun'.

Victor
 
R

rossum

Hello,

i don't manage to use the tolower() function:

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>

#include said:
using std::cout;
using std::ctype;
using std::endl;
using std::locale;
using std::string;
using std::transform;
using std::use_facet;

typedef ctype< char > char_ctype_facet;

int
main()
{
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

cout << str << endl;
return 0;
}

$ g++ -W -Wall -std=c++98 -pedantic tolower.cc
tolower.cc: In function `int main()':
tolower.cc:24: error: 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>)'

ct.tolower is <unknow type>

What am i doing wrong?

You need to include a library that includes tolower.

rossum
(Who has done the same sort of thing more often than he cares to
remember.)
 
T

TheDD

Le 28/05/2004 à 23:29:31 said:
You need to include a library that includes tolower.

rossum
(Who has done the same sort of thing more often than he cares to
remember.)

same error message, doesn't seem to be that.
 
T

TheDD

Victor said:
If you have to use a member function, try something like

std::transform(str.begin(), str.end(), str.begin(),
std::bind1st(std::mem_fun(&ctype<char>::tolower), ct));

I couldn't get it to compile, but dig in that direction.

Well i don't have a member function.

This works:

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>
#include <functional>

using std::cout;
using std::endl;
using std::locale;
using std::string;
using std::transform;
using std::bind2nd;

struct s : std::binary_function< char, locale, char >
{
char operator ()(char c, const locale & loc) const
{
/* use:
* template<typename CharT>
* CharT std::tolower ( CharT c,
* const locale & __loc
* )
*/
return std::tolower(c, loc);
}
};

int
main()
{
locale loc("");
string str("Hello world.");

// use s operator ()
transform(str.begin(), str.end(),
str.begin(), bind2nd(s(), loc));

cout << str << endl;
return 0;
}


but is really not classy.
 
V

Victor Bazarov

TheDD said:
Well i don't have a member function.

If you didn't, why were you trying to say

ct.tolower

? Isn't the notation '.' meant for member access? I must
be missing something here...

V
 
K

Klaus Eichner

TheDD said:
Hello,

i don't manage to use the tolower() function:

#include <algorithm>
#include <iostream>
#include <locale>
#include <string>

using std::cout;
using std::ctype;
using std::endl;
using std::locale;
using std::string;
using std::transform;
using std::use_facet;

typedef ctype< char > char_ctype_facet;

int
main()
{
locale loc("");
const char_ctype_facet& ct = use_facet< char_ctype_facet >(loc);
string str("Hello world.");

transform(str.begin(), str.end(),
str.begin(), ct.tolower);

cout << str << endl;
return 0;
}

$ g++ -W -Wall -std=c++98 -pedantic tolower.cc
tolower.cc: In function `int main()':
tolower.cc:24: error: 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>)'

ct.tolower is <unknow type>

What am i doing wrong?

I am not sure, but it might be related to:
Bug#144409: g++-3.0: does not support transform(begin,end,begin,tolower)
idiom
http://lists.debian.org/debian-gcc/2002/04/msg00092.html
 

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

Forum statistics

Threads
473,961
Messages
2,570,130
Members
46,689
Latest member
liammiller

Latest Threads

Top