本文整理汇总了C++中StringList::keep_if方法的典型用法代码示例。如果您正苦于以下问题:C++ StringList::keep_if方法的具体用法?C++ StringList::keep_if怎么用?C++ StringList::keep_if使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringList
的用法示例。
在下文中一共展示了StringList::keep_if方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TestKeepIf
bool TestKeepIf() {
BEGIN_TEST;
StringList list;
const char* original[] = {"", "foo", "bar", "baz", "qux",
"quux", "corge", "grault", "garply", "waldo",
"fred", "plugh", "xyzzy", "thud", ""};
const char* expected1[] = {"bar", "corge", "grault", "garply", "plugh"};
const char* expected2[] = {"corge", "grault", "garply", "plugh"};
const char* expected3[] = {"garply"};
for (size_t i = 0; i < arraysize(original); ++i) {
list.push_back(original[i]);
}
// Null string has no effect
list.keep_if(nullptr);
EXPECT_TRUE(Match(&list, original, 0, arraysize(original)));
// Empty string matches everything
list.keep_if("");
EXPECT_TRUE(Match(&list, original, 0, arraysize(original)));
// Match a string
list.keep_if("g");
EXPECT_TRUE(Match(&list, expected2, 0, arraysize(expected2)));
// Match a string that would have matched elements in the original list
list.keep_if("ar");
EXPECT_TRUE(Match(&list, expected3, 0, arraysize(expected3)));
// Use a string that doesn't match anything
list.keep_if("zzz");
EXPECT_TRUE(list.is_empty());
// Reset and apply both matches at once with logical-or
StringList substrs;
substrs.push_back("g");
substrs.push_back("ar");
list.clear();
for (size_t i = 0; i < arraysize(original); ++i) {
list.push_back(original[i]);
}
list.keep_if_any(&substrs);
EXPECT_TRUE(Match(&list, expected1, 0, arraysize(expected1)));
// Reset and apply both matches at once with logical-and
list.clear();
for (size_t i = 0; i < arraysize(original); ++i) {
list.push_back(original[i]);
}
list.keep_if_all(&substrs);
EXPECT_TRUE(Match(&list, expected3, 0, arraysize(expected3)));
END_TEST;
}