【问题标题】:How to print strings in an array that start in yan and end in es c++如何在以yan开头并以es c ++结尾的数组中打印字符串
【发布时间】:2021-08-26 00:51:53
【问题描述】:

我的任务是:

将三个字符串输入到字符串数组中。首先,使用函数查找结束比较的长度,然后再次分别比较最后两个字符。 其次,使用字符串函数一次取前三个字符的子字符串进行比较。

{
    string stringarray[3] = {"yankee", "yes", "word"};
    for (int x = 0; x < 3; x++)
        string substringend1 = stringarray.substr(stringarray.length(stringarray[x]) - 2, stringarray.length(stringarray[x]));
        string substringend2 = stringarray.substr(stringarray.length(stringarray[x]) - 1, stringarray.length(stringarray[x]));
        string substringstart = stringarray.substr(0, 3);

        if (substringend1 == "e" && substringend2 == "s" || substringstart == "yan") {
            for (int y = 0; y < 3; y++)
                cout << stringarray[y];
        }
}

I know Im an idiot and this is a bad question format and whatever but I need help

【问题讨论】:

  • 所以...打印stringarray 中的字符串,前提是它们以yan 开头并以es 结尾?
  • 是的,但我不能让它工作
  • stackoverflow.com/questions/1878001/… 并且 C++20 同时拥有 std::string::starts_with()std::string::ends_with() 函数

标签: c++ arrays string


【解决方案1】:

C++20 让这变得非常简单:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
 
int main() {
    std::vector<std::string> in{"yankee", "yes", "word"};
 
    for(const auto& s : in)
    {
        if(s.starts_with("yan") || s.ends_with("es"))
            std::cout << s << '\n';
    }
 
 
    return 0;
}

实时示例:https://godbolt.org/z/6Eq3nnKrf

最难的部分是在编译器中找到 C++20 支持。更高版本的 gcc 有它,最新版本的 MSVC 有它。

如果没有 C++20,您需要找到 starts_with()ends_with() 的替代品。开始很容易,在这里有一个很好的答案:How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?

使用这个答案,我们可以编写一个非常简单的starts_with() 函数:

bool starts_with(const std::string& in, const std::string& prefix)
{
    return in.rfind(prefix, 0) == 0;
}

我们也可以以此为例来说明如何创建ends_with() 函数!

bool ends_with(const std::string& in, const std::string& prefix)
{
    return in.rfind(prefix) == (in.size() - prefix.size());
}

那么我们的主要代码只是稍微改动了一下:

int main()
{
    std::vector<std::string> in{"yankee", "yes", "word"};
 
    for(const auto& s : in)
    {
        if(starts_with(s, "yan") || ends_with(s, "es"))
            std::cout << s << '\n';
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    您可以使用std::compare 将字符串的开头与yan 进行比较,或者将字符串的结尾与es 进行比较:

    #include <iostream>
    #include <string>
    using namespace std;
    
    bool startWithString(string a, string mod)
    {
        if (a.length() < mod.length()) {return false;}
        return 0 == a.compare(0, mod.length(), mod);
    }
    
    bool endWithString(string a, string mod)
    {
        if (a.length() < mod.length()) {return false;}
        return  0 == a.compare(a.length()-mod.length(), mod.length(), mod);
    }
    
    int main()
    {
        string str[4] = {"yankee", "yes", "word", "yankes"};
    
        for (int i = 0; i < 4; i++)
        {
            string cur = str[i];
            if (startWithString(cur, "yan") || endWithString(cur, "es")) {cout << cur << endl;}
        }
    }
    

    结果:

    yankee
    yes
    yankes
    

    使用rfind() 也会产生类似的结果:

    bool startWithString(string a, string mod)
    {
        return a.rfind(mod, 0) == 0;
    }
    
    bool endWithString(string a, string mod)
    {
        return a.rfind(mod) == (a.length() - mod.length());
    }
    

    正如@Chad 提到的,C++20 还支持starts_with()ends_with(),这让事情变得更容易。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2012-09-13
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      • 2020-11-22
      • 2018-10-29
      相关资源
      最近更新 更多