Gah, problems >.<

Pages: 12
But are you writing this as a test of your own skills or because you honestly think this will be preferable over the STL?
I hope it's not the latter, because the STL is an excellent library and it's going to be more efficient than writing a bunch of wrapper functions for it.
I'm writing these to be, not more useful than STL (because my I/O functions rely on it), but to have all the functionality that I need. As far as my casing functions, I do believe they are more useful than the STL to me particularly because 1) they aren't wrapping any STL functions, 2) They work by themselves on either strings or chars (depending on what is passed), and 3) I don't think STL provides case switching without doing the same thing I did (correct me if I'm wrong). This is partially a learning experience for me and partially me making libraries of many common functions that I need/will need throughout my CSC projects without having to rewrite the same code over and over.
Well the case functions are superseding c library material, not STL material. cctype has all that stuff. I'm not sure if it's done differently though.
The one problem with the <cctype> library is that toupper()/tolower() doesn't allow you to pass entire strings. So in order to re-case an entire string you would need to write a for loop each time containing the function toupper()/tolower() which re-cases each character individually. Basically, all I've done is included the for loop inside an overloaded version of my character upperCase()/lowerCase() that accepts strings and does the same thing with the for loop. so now instead of:

string someString = "blah";
for (int i = 0; i < int(someString.length()); i++)
tolower(someString[i]);

all i have to type is:

string someString = "blah";
lowerCase(someString);


Also, switchCase() doesn't have an equivalent in <cctype> that I know of. The idea is: instead of having to write the same block of code each time to do something, I write 1 function call each time to do the task.
Last edited on
Why would you ever need to switch the case on a string?
BTW,

boost::lambda does also have a for_each, declared in boost::lambda::ll.

http://www.boost.org/doc/libs/1_41_0/doc/html/lambda/le_in_details.html#lambda.nested_stl_algorithms


I think that it is OK for OP, as a college student, to proceed with doing what s/he is doing, as long as s/he
understands that (should OP be looking to become a professional programmer one day) in a professional
world the above is not the greatest of solutions.

Better solutions would be, IMO:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char upperCase( char ch ) {
    return static_cast<char>( toupper( ch ) );
}

template< typename FwdIter >
FwdIter upperCase( FwdIter first, FwdIter last, bool firstOnly = false ) {
    if( firstOnly && first != last ) {
        *first = upperCase( ch );
        return first;
    }

    for( FwdIter tmp( first ); tmp != last; ++tmp )
        *tmp = upperCase( *tmp );
    return first;
}

std::string upperCase( std::string str, bool firstOnly = false ) {
    upperCase( str.begin(), str.end(), firstOnly );
    return str;
}


Topic archived. No new replies allowed.
Pages: 12