Thread: Trying to upper case a string

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    21

    Trying to upper case a string

    I know there is a much easier way to do this with a for loop but I am trying to learn the STL algorithms so that's why I am doing this. I have my function like so:
    Code:
    void Words::UpperCase(void)
    {
    	for_each(words_.begin(), words_.end(), uppIt);
    }
    
    void uppIt(std::string &par)
    {
    	for_each(par.begin(), par.end(), toupper);
    }
    Here, words_ is a vector of strings. It is not working for some reason, my strings are still not capitalized. Please advice, thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    toupper() does not modify its parameter, it returns the upper case character.

    You need an "uppIt" for characters too.

    gg

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am trying to learn the STL algorithms so that's why I am doing this
    Then it would be a good idea to mention that the operation passed to std::for_each isn't assigned back to each element of the container. std::for_each is more like this:
    Code:
    template <typename In, typename Op>
    Op for_each ( In first, In last, Op op )
    {
      while ( first != last )
        op ( *first++ );
    
      return op;
    }
    What you want is std::transform, which is similar to std::for_each and actually assigns the result of the operation somewhere, which can be the input container:
    Code:
    std::transform ( par.begin(), par.end(), par.begin(), std::toupper );
    However, because there may be two versions of std::toupper depending on the headers you include, this is actually illegal because std::transform doesn't know which std::toupper to call. This problem isn't unique to std::transform. For example, if in the code you gave, you included both <cctype> and <iostream>, you've managed to include both a single argument version of std::toupper and a two argument version that accepts a locale as the second parameter. The safest fix is to create your own function (or better yet, function object) that picks the right version for you:
    Code:
    class upper {
    public:
      int operator() ( int c ) const { return std::toupper ( c ); }
    };
    
    std::transform ( par.begin(), par.end(), par.begin(), upper() );
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  4. Convert a string to upper case?
    By jpp1cd in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 07:49 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM