0

I need to search an array of c-strings for a substring.

I created what I thought would return me the answer but it is only syntactically correct but semantically wrong, but I'm not sure where I have gone wrong.

There is also a sub-question to this. Which I will ask after showing you the example I tried.

#include <boost/algorithm/string/find.hpp>

const char Months[12][20] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

void Test()
{
    typedef boost::iterator_range<boost::range_iterator<const char [20]>::type > constCharRange;
    boost::iterator_range<char const *> ir = find_first("January", months);
}

ir.first == ir.last

The results of the iterator show that I have not written this correctly.

I'm not sure whether the fact that the first parameter is actually const char [8] is having a detrimental effect.

My main question what should I do to correct it, and the supplemental question is how can I extract the type that find_first requires from constCharRange or indeed any such typedef.

Edit:

I see that I have used end incorrectly. I've then managed to get slightly different example to work however they are not compatible with the definitions I actually have to use (I can add to the code but not change the existing definition).

const std::string Months[]= { /*same data as before*/
void StringTest2()
{
    const std::string* sptr =0;
    sptr = std::find(boost::begin(Months), boost::end(Months), std::string("February"));
    if (sptr)
    {
        string sResult(*sptr);
    }
}

Another Test

const char* Months[]= { /*same data as before*/
void StringTest3()
{
    const char **sptr = std::find(boost::begin(Months), boost::end(Months), "February");
    if (sptr)
    {
        string sResult(*sptr);
    }
}

Here is the nearest I can get, but I can't seem to get the return type spec correct

void StringTest4()
{
    const char Months[12][20]Months[]= { /*same data as before*/
    std::find(boost::begin(Months), boost::end(Months), "February");
}

1 Answer 1

0

I can correct StringTest4 to do exact matches, however this solution won't work for partial ones.

void StringTest4()
{
    boost::range_iterator<const char [12][20]>::type sptr = std::find(boost::begin(Months), boost::end(Months), std::string("February"));
    if (sptr)
    {
        string sResult(*sptr);
    }
}

Note the passing of std::string rather than just "February" if we don't do this then std::find will just be doing a pointer comparison which would therefore result in failure.

I would still prefer to use this with string_algo, but I'm still not sure which bits I would need for it to successfully work with const char Months[12][20]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.