天天看点

从C++11到C++23(五)C++20新增starts_with和ends_with用法

不得不说,

C++20

真是越来越接近

Python

语法。

Python

中有两个函数分别是

startswith()

函数与

endswith()

函数,功能都十分相似,

startswith()

函数判断文本是否以某个或某几个字符开始,

endswith()

函数判断文本是否以某个或某几个字符结束。

例如

>>> s = "hello world"
>>> s.startswith("h")
True
>>> s.startswith("he")
True
>>> s.startswith("hello")
True
>>> s.startswith("hellow")
False
>>> s.endswith("d")
True
>>> s.endswith("ld")
True
>>> s.endswith("world")
True
>>> s.endswith("World")
False
           

代码链接见:https://wandbox.org/permlink/7xCsErs34UbcjEhD

在C++20中新增了

starts_with

ends_with

语法,写出上述等效代码如下:

// gcc >=10,
// g++ test.cpp -Wall -Wextra -std=c++2a
#include <iostream>
#include <string>
 
int main()
{
    std::string str = "hello world"; 
    bool flag1 = str.starts_with("h");
    bool flag2 = str.starts_with("he"); 
    bool flag3 = str.starts_with("hello"); 
    bool flag4 = str.starts_with("hellow"); 
    bool flag5 = str.ends_with("d");
    bool flag6 = str.ends_with("ld");
    bool flag7 = str.ends_with("world");
    bool flag8 = str.ends_with("World");
    std::cout << " flag1 is: " << flag1 << std::endl;
    std::cout << " flag2 is: " << flag2 << std::endl;
    std::cout << " flag3 is: " << flag3 << std::endl;
    std::cout << " flag4 is: " << flag4 << std::endl;
    std::cout << " flag5 is: " << flag5 << std::endl;
    std::cout << " flag6 is: " << flag6 << std::endl;
    std::cout << " flag7 is: " << flag7 << std::endl;
    std::cout << " flag8 is: " << flag8 << std::endl;
    return 0;
}
           

加上泛型写一个判断前缀函数如下,代码链接:https://wandbox.org/permlink/InHOgBMRdEvCMIzf

// gcc >=10,
// g++ test.cpp -Wall -Wextra -std=c++2a
// C++ program to illustrate the use
// of starts_with()
#include <iostream>
#include <string>
#include <string_view>
using namespace std;

// Function template to check if the
// given string starts with the given
// prefix or not
template <typename PrefixType>
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;
}

           

加上泛型写一个判断后缀函数如下,代码链接:https://wandbox.org/permlink/p2ERynCRulVWbWqZ

// gcc >=10,
// g++ test.cpp -Wall -Wextra -std=c++2a
// C++ program to illustrate the use
// of ends_with()
#include <iostream>
#include <string>
#include <string_view>
using namespace std;

// Function template to check if the
// given string ends_with given string
template <typename SuffixType>
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');
}

           

继续阅读