C++ 在由换行符分隔的字符串中查找特定文本
我想在句子列表中找到一个特定的字符串。每个句子都是一行,用C++ 在由换行符分隔的字符串中查找特定文本,c++,string,C++,String,我想在句子列表中找到一个特定的字符串。每个句子都是一行,用\n分隔。当到达换行符时,当前搜索应停止并在下一行开始新搜索 我的计划是: #include <iostream> #include <string.h> using namespace std; int main(){ string filename; string list = "hello.txt\n abc.txt\n check.txt\n" cin >> filena
\n
分隔。当到达换行符时,当前搜索应停止并在下一行开始新搜索
我的计划是:
#include <iostream>
#include <string.h>
using namespace std;
int main(){
string filename;
string list = "hello.txt\n abc.txt\n check.txt\n"
cin >> filename;
// suppose i run programs 2 times and at 1st time i enter abc.txt
// and at 2nd time i enter abc
if(list.find(filename) != std::string::npos){
//I want this condition to be true only when user enters complete
// file name. This condition also becoming true even for 'abc' or 'ab' or even for 'a' also
cout << file<< "exist in list";
}
else cout<< "file does not exist in list"
return 0;
}
#包括
#包括
使用名称空间std;
int main(){
字符串文件名;
string list=“hello.txt\n abc.txt\n check.txt\n”
cin>>文件名;
//假设我运行程序两次,第一次输入abc.txt
//第二次我进入abc
if(list.find(filename)!=std::string::npos){
//我希望只有当用户输入complete时,此条件才为true
//文件名。即使对于“abc”或“ab”或“a”,此条件也将变为真
cout首先,我不会将文件列表保存在单个字符串中,但我会使用任何类型的列表或向量。
然后,如果您需要将列表保存在字符串中(出于应用程序逻辑中的某种原因),我将在向量中分离字符串,然后循环遍历向量的元素,检查元素是否正是搜索的元素。
要拆分元素,我将执行以下操作:
std::vector<std::string> split_string(const std::string& str,
const std::string& delimiter)
{
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos)
{
strings.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
}
std::vector split_string(const std::string&str,
常量std::字符串和分隔符)
{
std::向量字符串;
std::string::size\u type pos=0;
std::string::size\u type prev=0;
while((pos=str.find(delimiter,prev))!=std::string::npos)
{
字符串。向后推(str.substr(prev,pos-prev));
上一个=位置+1;
}
//获取最后一个子字符串(或仅当未找到分隔符时)
字符串。推回(str.substr(prev));
返回字符串;
}
您可以看到函数工作的示例
然后只需使用该函数并将代码更改为:
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main(){
string filename;
string list = "hello.txt\n abc.txt\n check.txt\n"
cin >> filename;
vector<string> fileList = split_string(list, "\n");
bool found = false;
for(int i = 0; i<fileList.size(); i++){
if(fileList.at(i) == file){
found = true;
}
}
if(found){
cout << file << "exist in list";
} else {
cout << "file does not exist in list";
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串文件名;
string list=“hello.txt\n abc.txt\n check.txt\n”
cin>>文件名;
向量文件列表=拆分字符串(列表“\n”);
bool-found=false;
对于(inti=0;i首先,我不会将文件列表保存在单个字符串中,但我会使用任何类型的列表或向量。
然后,如果您需要将列表保存在字符串中(出于应用程序逻辑中的某种原因),我将在向量中分离字符串,然后循环遍历向量的元素,检查元素是否正是搜索的元素。
要拆分元素,我将执行以下操作:
std::vector<std::string> split_string(const std::string& str,
const std::string& delimiter)
{
std::vector<std::string> strings;
std::string::size_type pos = 0;
std::string::size_type prev = 0;
while ((pos = str.find(delimiter, prev)) != std::string::npos)
{
strings.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
// To get the last substring (or only, if delimiter is not found)
strings.push_back(str.substr(prev));
return strings;
}
std::vector split_string(const std::string&str,
常量std::字符串和分隔符)
{
std::向量字符串;
std::string::size\u type pos=0;
std::string::size\u type prev=0;
while((pos=str.find(delimiter,prev))!=std::string::npos)
{
字符串。向后推(str.substr(prev,pos-prev));
上一个=位置+1;
}
//获取最后一个子字符串(或仅当未找到分隔符时)
字符串。推回(str.substr(prev));
返回字符串;
}
您可以看到函数工作的示例
然后只需使用该函数并将代码更改为:
#include <iostream>
#include <string.h>
#include <vector>
using namespace std;
int main(){
string filename;
string list = "hello.txt\n abc.txt\n check.txt\n"
cin >> filename;
vector<string> fileList = split_string(list, "\n");
bool found = false;
for(int i = 0; i<fileList.size(); i++){
if(fileList.at(i) == file){
found = true;
}
}
if(found){
cout << file << "exist in list";
} else {
cout << "file does not exist in list";
}
return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main(){
字符串文件名;
string list=“hello.txt\n abc.txt\n check.txt\n”
cin>>文件名;
向量文件列表=拆分字符串(列表“\n”);
bool-found=false;
对于(int i=0;i列表。find
将仅在字符串列表中查找子字符串,但如果要比较整个字符串直到找到\n
,则可以标记列表
,并放入一些向量
为此,您可以将字符串list
放入std::istringstream
中,并使用std::getline
从中生成一个std::vector
,如下所示:
std::istringstream ss(list);
std::vector<std::string> tokens;
std::string temp;
while (std::getline(ss, temp)){
tokens.emplace_back(temp);
}
list.find
只会在字符串list
中找到子字符串,但是如果您想比较整个字符串直到找到\n
,您可以标记列表
并放入一些向量
为此,您可以将字符串list
放入std::istringstream
中,并使用std::getline
从中生成一个std::vector
,如下所示:
std::istringstream ss(list);
std::vector<std::string> tokens;
std::string temp;
while (std::getline(ss, temp)){
tokens.emplace_back(temp);
}
使用std::regex
可能是。这与我有一种奇怪的感觉,你只希望条件匹配整行时为真,即直到\n
为止。告诉我我是否错了。是的,穆罕默德·艾哈迈德就是这样。你必须只有一个字符串,其中包含所有这些文件名?不是向量或其他东西?使用一个std::regex
可能是。这完全是我的重复。我有一种奇怪的感觉,你只希望条件为真,如果它匹配整行,即直到\n
。告诉我我是否错了。是的,穆罕默德·艾哈迈德是这样的。你必须只有一个字符串,其中包含所有这些文件名?不是向量或什么的?为什么不t在第一位设置一个std::vector
?@πνταῥεῖ 是的,当然。我认为作为字符串的列表是问这个问题的人的必要条件。如果不是这样的话,那么是的,你是对的。我会编辑这个问题,让它清楚为什么不在第一位设置一个std::vector
ῥεῖ 是的,当然。我认为作为一个字符串的列表是问这个问题的人的必要条件。如果不是这样的话,那么是的,你是对的。我会编辑这个问题以澄清问题