locked
How do I do this is C++

    Question

  • I've searched for it and have not been able to find it :) in c# we can extended a System class with the following method

    public static class Extender
    {
           public static bool ContainsOnly(this string left, char[] right)
            {
                foreach (char c in left)
                {
                    if (!right.ToCharArray().Contains(c))
                        return false;
                }
                return true;
            }
    }

    I now how to get the same result but the nice thing about this functionality is that the ContainsOnly method above appears directly off of a string object like this

    string abc = "abcdefg";
    if(abc.ContainsOnly("abcdefghigjlmnop"))
         Debug.WriteLine("Success");

    I have plenty of c++ background but not a lot in cli or cx so how can I accomplish a similar result in a windows store app

    i.e.

    Platform::String^ abc = "abcdefg";
    if(abc->ContainsOnly("abcdefghigklmnop"))
        OutputDebugString(L"Success");


    Friday, June 21, 2013 4:18 PM

Answers

  • No, but this ordinarily shouldn't be a problem: C++ is multiparadigm (supports procedural programming, object-oriented programming, generic programming), so usually if there isn't a purely OOP way, there's another way ;-) Extensions of functionality like this one are ordinarily handled using non-member functions (possibly nested in an appropriate namespace, often also as a function template to handle various kinds of strings). I do see your point about the IDE convenience, though.

    On a side note, this functionality is supported by the Boost String Algorithms Library: http://boost.org/libs/algorithm/string/

    In particular, consider the predicates in the "contains" family: http://www.boost.org/doc/libs/release/doc/html/string_algo/reference.html#header.boost.algorithm.string.predicate_hpp

    // EDIT: no, this is probably not exactly what you're looking, I think I got suggested by the "contains" name too much. Take a look at one of the following: {boost::algorithm::all, boost::algorithm::find_regex, boost::algorithm::find_all_regex, boost::algorithm::regex_finder}.

    Example:
    http://coliru.stacked-crooked.com/view?id=59e69b8d5f1f4cca2d0325c650e28f1c-61c3814520a8d4318f681038dc4b4da7

    #include <iostream>
    #include <ios>
    #include <string>
    #include <boost/algorithm/string/predicate.hpp>
    
    int main()
    {
        std::string str("Hello world!");
        std::cout << "str = " << str << '\n';
        
        using boost::algorithm::contains;  // case sensitive 
        using boost::algorithm::icontains; // case insensitive
    
        std::cout << std::boolalpha;
        
        // case sensitive 
        bool contains_hello = contains(str, "hello"); // false
        std::cout << "contains_hello = " << contains_hello << '\n';
    
        // case insensitive 
        bool icontains_hello = icontains(str, "hello"); // true
        std::cout << "icontains_hello = " << icontains_hello << '\n';
    
        std::cout << std::noboolalpha;
    
        return 0;
    }
    


    • Edited by MattPD Saturday, June 22, 2013 12:02 AM Added more algorithms.
    • Marked as answer by BubbaRichard Saturday, June 22, 2013 3:04 AM
    Friday, June 21, 2013 11:51 PM

All replies

  • I do not think that C++ has extension methods like C#.
    Friday, June 21, 2013 7:59 PM
  • Runtime classes are sealed and cannot be inherited from.
    Friday, June 21, 2013 10:50 PM
  • No, but this ordinarily shouldn't be a problem: C++ is multiparadigm (supports procedural programming, object-oriented programming, generic programming), so usually if there isn't a purely OOP way, there's another way ;-) Extensions of functionality like this one are ordinarily handled using non-member functions (possibly nested in an appropriate namespace, often also as a function template to handle various kinds of strings). I do see your point about the IDE convenience, though.

    On a side note, this functionality is supported by the Boost String Algorithms Library: http://boost.org/libs/algorithm/string/

    In particular, consider the predicates in the "contains" family: http://www.boost.org/doc/libs/release/doc/html/string_algo/reference.html#header.boost.algorithm.string.predicate_hpp

    // EDIT: no, this is probably not exactly what you're looking, I think I got suggested by the "contains" name too much. Take a look at one of the following: {boost::algorithm::all, boost::algorithm::find_regex, boost::algorithm::find_all_regex, boost::algorithm::regex_finder}.

    Example:
    http://coliru.stacked-crooked.com/view?id=59e69b8d5f1f4cca2d0325c650e28f1c-61c3814520a8d4318f681038dc4b4da7

    #include <iostream>
    #include <ios>
    #include <string>
    #include <boost/algorithm/string/predicate.hpp>
    
    int main()
    {
        std::string str("Hello world!");
        std::cout << "str = " << str << '\n';
        
        using boost::algorithm::contains;  // case sensitive 
        using boost::algorithm::icontains; // case insensitive
    
        std::cout << std::boolalpha;
        
        // case sensitive 
        bool contains_hello = contains(str, "hello"); // false
        std::cout << "contains_hello = " << contains_hello << '\n';
    
        // case insensitive 
        bool icontains_hello = icontains(str, "hello"); // true
        std::cout << "icontains_hello = " << icontains_hello << '\n';
    
        std::cout << std::noboolalpha;
    
        return 0;
    }
    


    • Edited by MattPD Saturday, June 22, 2013 12:02 AM Added more algorithms.
    • Marked as answer by BubbaRichard Saturday, June 22, 2013 3:04 AM
    Friday, June 21, 2013 11:51 PM
  • I was kind of guessing that but I thought I would check.

    Thanks to everyone!

    Saturday, June 22, 2013 3:04 AM