transform

  • Thread starter Saki Arkoudopoulos
  • Start date
S

Saki Arkoudopoulos

Hello,

I'm trying to convert a string with mixed upper- and lowercase
characters to all lowercase characters.
I found the following code on the Internet:
transform(temp.begin(), temp.end(), temp.begin(), tolower);

But does anyone know which lib one has to include?
I've included:

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


but none of these seem to do the job...


Thanks in advance,

Saki
 
S

Saki Arkoudopoulos

Saki said:
Hello,

I'm trying to convert a string with mixed upper- and lowercase
characters to all lowercase characters.
I found the following code on the Internet:
transform(temp.begin(), temp.end(), temp.begin(), tolower);

But does anyone know which lib one has to include?
I've included:

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


but none of these seem to do the job...


Thanks in advance,

Saki


these are the errors I keep getting:

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>)'


--Saki
 
M

msalters

Saki said:
Hello,

I'm trying to convert a string with mixed upper- and lowercase
characters to all lowercase characters.
I found the following code on the Internet:
transform(temp.begin(), temp.end(), temp.begin(), tolower);

But does anyone know which lib one has to include?

<cstdlib> and <cctype> for std::tolower,
<algorithm> for std::transform. Don't forget the std::, the
example is incorrect (there's no ::transform)

HTH,
Michiel Salters
 
S

Saki Arkoudopoulos

msalters said:
<cstdlib> and <cctype> for std::tolower,
<algorithm> for std::transform. Don't forget the std::, the
example is incorrect (there's no ::transform)

Thank you, I'll try it out right now.
The example is correct because I use "using namespace std;".

Greetings,

Saki
 
U

ulrich

Hello,

I'm trying to convert a string with mixed upper- and lowercase
characters to all lowercase characters.
I found the following code on the Internet:
transform(temp.begin(), temp.end(), temp.begin(), tolower);

But does anyone know which lib one has to include?
I've included:

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


but none of these seem to do the job...

i assume that you have a using namespace std somewhere.

maybe "tolower" is a custom functor not included in any standard library?
 
P

Paul Drummond

I remember having problems with transform but this works:

#include <algorithm>
#include <cctype.h>
#include <string>

std::string RDCUString::lowercase(std::string& s)
{
std::string s2 = s;
std::transform(s2.begin(), s2.end(), s2.begin(),
static_cast<int(*)(int)>(std::tolower));

return s2;
}
 
S

Saki Arkoudopoulos

Paul said:
I remember having problems with transform but this works:

#include <algorithm>
#include <cctype.h>
#include <string>

std::string RDCUString::lowercase(std::string& s)
{
std::string s2 = s;
std::transform(s2.begin(), s2.end(), s2.begin(),
static_cast<int(*)(int)>(std::tolower));

return s2;
}

Thank you very much Paul,

your example worked and was exactly what we needed!


Greetings,

Saki
 
I

Ioannis Vranos

Paul said:
I remember having problems with transform but this works:

#include <algorithm>
#include <cctype.h>
#include <string>

std::string RDCUString::lowercase(std::string& s)
{
std::string s2 = s;
std::transform(s2.begin(), s2.end(), s2.begin(),
static_cast<int(*)(int)>(std::tolower));

return s2;
}


This casting shouldn't be needed in an ISO C++ compliant compiler. Doesn't this compile
with your compiler?



#include <algorithm>
#include <cctype>
#include <string>


int main()
{
using namespace std;

string s= "AabCEWDdsfsdsdfewWEEW";

transform(s.begin(), s.end(), s.begin(), tolower);
}
 
H

Haro Panosyan

Ioannis said:
This casting shouldn't be needed in an ISO C++ compliant compiler.
Doesn't this compile with your compiler?

It compiles but as soon as I add
#inlcude <iostream>

then it complains. Any idea? I used g++ 3.4.3.
 
I

Ioannis Vranos

Haro said:
It compiles but as soon as I add
#inlcude <iostream>

then it complains. Any idea? I used g++ 3.4.3.


You are right. After including <iostream> it doesn't compile in my gcc 3.4.2 too, but
continues to compile with VC++ 2003 and Intel C++ 8.1. However I think we can consider it
a compiler defect, since implicit conversion from char to int should take place (after all
tolower() is aimed for chars).


A quick fix, is this:


#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>


inline char makelower(const char c)
{
return tolower(c);
}

int main()
{
using namespace std;

string s= "AabCEWDdsfsdsdfewWEEW";

transform(s.begin(), s.end(), s.begin(), makelower);
}
 

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,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top