📜  在C++ 20中带有示例的starts_with()和ends_with()

📅  最后修改于: 2021-05-30 19:32:17             🧑  作者: Mango

在本文中,我们将使用C++ 20中的示例讨论starts_with()ends_with()

以。。开始()

此函数有效地检查字符串是否以给定前缀开头。该函数用std :: basic_string和std :: basic_string_view编写。

句法:

在以上语法中,前缀可以是:

  • 字符串
  • 字符串视图
  • 单字符或具有以空字符结尾的C样式字符串

starts_with()具有不同类型的前缀:

函数的所有三种重载形式都有效地返回std :: basic_string_view (data(),size())。starts_with(x);

参数它需要一个字符序列或一个字符才能与字符串的开头进行比较。

返回值:它返回布尔值true或false,指示以下内容:

  • True:如果字符串以前缀开头。
  • False:如果字符串不以前缀开头。

程序1:

下面的程序演示了C++中starts_with()的概念:

C++
// C++ program to illustrate the use
// of starts_with()
#include 
#include 
#include 
using namespace std;
  
// Function template to check if the
// given string starts with the given
// prefix or not
template 
void if_prefix(const std::string& str,
               PrefixType prefix)
{
    cout << "'" << str << "' starts with '"
         << prefix << "': "
         << str.starts_with(prefix)
         << endl;
}
  
// Driver Code
int main()
{
    string str = { "geeksforgeeks" };
  
    if_prefix(str, string("geek"));
  
    // prefix is string
    if_prefix(str, string_view("geek"));
  
    // prefix is string view
    if_prefix(str, 'g');
  
    // prefix is single character
    if_prefix(str, "geek\0");
  
    // prefix is C-style string
    if_prefix(str, string("for"));
  
    if_prefix(str, string("Geek"));
  
    if_prefix(str, 'x');
  
    return 0;
}


C++
// C++ program to illustrate the use
// of ends_with()
#include 
#include 
#include 
using namespace std;
  
// Function template to check if the
// given string ends_with given string
template 
void if_suffix(const std::string& str,
               SuffixType suffix)
{
    cout << "'" << str << "' ends with '" << suffix << "': " << str.ends_with(suffix) << std::endl;
}
  
// Driver Code
int main()
{
    string str = { "geeksforgeeks" };
  
    if_suffix(str, string("geeks"));
  
    // suffix is string
    if_suffix(str, string_view("geeks"));
  
    // suffix is string view
    if_suffix(str, 's');
  
    // suffix is single character
    if_suffix(str,
              "geeks\0");
  
    // suffix is C-style string
    if_suffix(str, string("for"));
  
    if_suffix(str, string("Geeks"));
  
    if_suffix(str, 'x');
  
    if_suffix(str, '\0');
}


输出:

此函数有效地检查字符串是否以给定的后缀结尾。该函数用std :: basic_string和std :: basic_string_view编写。

句法:

在以上语法中,后缀可以是:

  • 字符串
  • 字符串视图
  • 单个字符或C风格的字符串空字符。

以()不同类型的后缀结尾:

函数的所有三种重载形式都有效地返回std :: basic_string_view (data(),size())。ends_with(x);

参数它需要一个字符序列或一个字符才能与字符串末尾进行比较。

返回值:返回布尔值true或false,指示以下内容:

  • True:如果字符串以后缀结尾。
  • False:如果字符串不以后缀结尾。

程式2:

下面的程序演示了C++中ends_with()的概念:

C++

// C++ program to illustrate the use
// of ends_with()
#include 
#include 
#include 
using namespace std;
  
// Function template to check if the
// given string ends_with given string
template 
void if_suffix(const std::string& str,
               SuffixType suffix)
{
    cout << "'" << str << "' ends with '" << suffix << "': " << str.ends_with(suffix) << std::endl;
}
  
// Driver Code
int main()
{
    string str = { "geeksforgeeks" };
  
    if_suffix(str, string("geeks"));
  
    // suffix is string
    if_suffix(str, string_view("geeks"));
  
    // suffix is string view
    if_suffix(str, 's');
  
    // suffix is single character
    if_suffix(str,
              "geeks\0");
  
    // suffix is C-style string
    if_suffix(str, string("for"));
  
    if_suffix(str, string("Geeks"));
  
    if_suffix(str, 'x');
  
    if_suffix(str, '\0');
}

输出:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”