http://www.gotw.ca/gotw/029.htm
And also, you could use boost (which may be the better idea anyway versus redefining a string class):
http://www.boost.org/doc/html/string_algo/quickref.html
http://www.gotw.ca/gotw/029.htm
And also, you could use boost (which may be the better idea anyway versus redefining a string class):
http://www.boost.org/doc/html/string_algo/quickref.html
Last edited by Cat; 09-07-2006 at 06:54 PM.
You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.
>> But it's just inconvenient to have to create a loop and all that crap!
Of course, a function would be better if it's being used a lot!
Code:void MyToUpper ( std::string &MyString ) // or - std::string MyToUpperReturn() if you wanted to return it. { for ( std::string::size_type i=MyString.begin(); i!=MyString.end(); i++ ) // or - for ( int i=0; i<(int)MyString.size(); i++ ) { MyString[i] = std::toupper( MyString[i] ); } }
Last edited by twomers; 09-09-2006 at 06:13 PM.
You can also use std::transform:
Code:#include <string> #include <iostream> #include <algorithm> int main() { std::string s("tHis iS Some SCRewy cRap"); std::transform(s.begin(), s.end(), s.begin(), toupper); std::cout << s << std::endl; std::cin.get(); }
Nice! I like this one the best, very clean! It uses the vector commands, which is cooooool Thanks for this!Originally Posted by anon
You do realize that transform had already been suggested twice in this thread, right?
Alas, there was no example before!...
Probably. But had he read about transform when it was suggested without code, it wouldn't have take him 2 days to solve this problem.
No. Coding is not for the lazy.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Mkay, maybe because I'm 16, and I have other things to do besides programming? So yes, I am lazy. It's not like I get paid, like you, so please stop caring about other people, and go smoke some weed because you seem tense!
Thank you son
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>> It's not like I get paid, like you,
Hmmm, we get paid for posting on the forums!?!? Why hasn't anyone told me that?
It might not work. C++ algorithms want a function object or function with C++ linkage. That means that the function needs to support name mangling. If toupper is declared with C linkage then the algorithm might not accept it. You should wrap toupper in a function with C++ linkage or function object.Originally Posted by Sentral
Sumimasen.Code:#include <cctype> #include <string> #include <iostream> #include <algorithm> // function object struct toUpper { int operator()(int ch) { return std::toupper(ch); } }; // function with C++ linkage int toLower(int ch) { return std::tolower(ch); } int main() { std::string s("tHis iS Some SCRewy cRap"); std::transform(s.begin(), s.end(), s.begin(), toUpper()); std::cout << s << std::endl; std::transform(s.begin(), s.end(), s.begin(), toLower); std::cout << s << std::endl; std::cin.get(); }
Yup, I get paid per post!Originally Posted by twomers
You get payed to do nothing and just wait for someone to post some code so that you decide if you like the answer or not?
Wow!
*smokes his pipe*
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
>Thank you son
LOL