CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: string_type Issues?

  1. #1
    Join Date
    Feb 2009
    Posts
    135

    string_type Issues?

    Code:
    size_type GetLongestOption(size_type longest_option, string menu_option_index[num_menu_options])
    {
        int current_menu_option = 0;
    
        longest_option = menu_option_index[0];
    
        while (current_menu_option != num_menu_options)
        {
            if (current_menu_option.size() > longest_option)
            {
                longest_option = menu_option_index[current_menu_option];
            }
            ++current_menu_option;
        }
        return longest_option;
    }
    I have #include <string> and using namespace std, but it doesn't seem to want to recognize a size_type function or a variable passed to the function of a size_type. I was wondering if could get any help on that, whether it helps me use size_type or simply telling me that's not possible or less than ideal and showingh me an alternative method.

    [SOLVED]
    Last edited by BleaS; February 24th, 2009 at 03:05 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: string_type Issues?

    In this case, size_type is typedef-ed in std::basic_string, so you'll need to use it like this: [std::]string::size_type.

    P.S.
    You're mismatching types...
    Last edited by Plasmator; February 24th, 2009 at 01:32 PM.

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: string_type Issues?

    It should be std::string::size_type or string::size_type.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Join Date
    Feb 2009
    Posts
    135

    Re: string_type Issues?

    I tried those too, despite the using namespace. But I'll try again, thanks. I might be doing something else wrong.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,768

    Re: string_type Issues?

    Note that you have other problems, e.g., current_menu_option is an int, but you call current_menu_option.size().

    Note also that your function signature is not correct, since num_menu_options should probably be another parameter.

    EDIT:
    Putting things into context, it looks like your aim is to find the longest string in the array menu_option_index. If so, you should have two parameters: the menu_option_index array and the size of the array. longest_option should be an ordinary local variable, not a parameter. Instead of std::string::size_type, you probably should use std::size_t... or instead of the array, use a std::vector<std::string> which is passed by const reference.

    EDIT #2:
    Actually finding the longest string can be done with std::max_element, e.g.,
    Code:
    namespace
    {
        class CompareStringLength
        {
        public:
            bool operator()(const std::string& lhs, const std::string& rhs) const
            {
                return lhs.length() < rhs.length();
            }
        };
    }
    
    std::size_t GetLongestOption(const std::string* menu_option_index, std::size_t size)
    {
        return std::max_element(
            menu_option_index, menu_option_index + size,
            CompareStringLength()) - menu_option_index;
    }
    Last edited by laserlight; February 24th, 2009 at 03:17 PM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)