使用 string_algo/ranges 在 C 字符串数组中搜索子字符串

发布于 2024-12-18 14:33:59 字数 1607 浏览 2 评论 0原文

我需要在 C 字符串数组中搜索子字符串。

我创建了我认为会返回答案的内容,但它只是语法上正确但语义上错误,但我不确定我哪里出了问题。

这还有一个子问题。在向您展示我尝试过的示例后我会问这个问题。

#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

迭代器的结果显示我这个写得不正确。

我不确定第一个参数实际上是 const char [8] 是否会产生有害影响。

我的主要问题是我应该做什么来纠正它,补充问题是如何从 constCharRange 或任何此类 typedef 中提取 find_first 所需的类型。

编辑:

我发现我错误地使用了 end 。然后,我设法获得了稍微不同的示例,但是它们与我实际必须使用的定义不兼容(我可以添加到代码中,但不能更改现有的定义)。

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);
    }
}

这是我能得到的最接近的另一个测试

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);
    }
}

,但我似乎无法获得正确的返回类型规范

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

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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-12-25 14:33:59

我可以更正 StringTest4 以进行精确匹配,但是此解决方案不适用于部分匹配。

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);
    }
}

请注意 std::string 的传递而不仅仅是 "February" 如果我们不这样做,那么 std::find 将只进行指针比较,因此导致失败。

我仍然更愿意将其与 string_algo 一起使用,但我仍然不确定它需要哪些位才能成功与 const char Months[12][20] 一起使用

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]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文