gpt4 book ai didi

c++ - 在 C++ 中查找字符串中的子字符串

转载 作者:行者123 更新时间:2023-11-30 02:31:57 27 4
gpt4 key购买 nike

我正在从一个文本文件中读取行以查找它们的状态,该状态有 3 个选项,可以是 OUT 或 Discontinued 或 Cancelled

我是这样做的:

ifstream inFile("DVD_list.txt");
string line;
char status;
while ( getline (inFile,line) ) {
std::size_t pos = line.find("Out" || "Discontinued"|| "Cancelled");
}

return 0;

如何将 find 函数与 or 表达式一起使用?

最佳答案

if((pos = line.find("Out")) != string::npos)
{
// handle Out
}
else if((pos = line.find("Discontinued")) != string::npos)
{
// handle Discontinued
}
else if((pos = line.find("Cancelled")) != string::npos)
{
// handle Cancelled
}
else
{
// not found
}

可能,您需要确保没有检测到误报,例如“一些示例文本没有输出”!

关于c++ - 在 C++ 中查找字符串中的子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36977013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com