C++ 在C+;中的另一个字符串中检查一个字符串+; #包括 #包括 使用名称空间std; int main(){ 字符串str_1=“Gandalf”; 字符串str_2=“dal”; 对于(int i=0;i
您可以尝试以下方法:C++ 在C+;中的另一个字符串中检查一个字符串+; #包括 #包括 使用名称空间std; int main(){ 字符串str_1=“Gandalf”; 字符串str_2=“dal”; 对于(int i=0;i,c++,arrays,string,char,C++,Arrays,String,Char,您可以尝试以下方法: #include <iostream> #include <string> using namespace std; int main() { string str_1 = "Gandalf"; string str_2 = "dal"; for (int i = 0; i <= str_1.length() - 2; i++) for (int j = 0; j <= str_2.length(
#include <iostream>
#include <string>
using namespace std;
int main() {
string str_1 = "Gandalf";
string str_2 = "dal";
for (int i = 0; i <= str_1.length() - 2; i++)
for (int j = 0; j <= str_2.length(); j++) {
if (str_2[j] == str_1[i]) {
if (str_2[j + 1] == str_1[i + 1]) {
if (str_2[j + 2] == str_1[i + 2])
cout << "true";
}
}
}
return 0;
}
for(int i=0;i std::cout下面的函数find
基本上再现了std::string::find
的行为(没有起始位置参数)。您需要:
- 循环通过外部字符串,并在每个步骤:
- 循环第二个字符串,检查每个字符
- 如果其中任何一个失败,请返回到外部循环
- 如果我们一直通过内部循环,那么第二个字符串就在那里,并返回外部循环中的当前位置
- 如果第一个字符串中的空间不足,请跳过其余字符串
希望这些评论能清楚地说明这一点。我还包括一个小的实用函数,用于将找到的位置转换为真/假,以及一些测试
for (int i = 0; i < str_1.length() - str_2.length(); i++) {
bool is_same = true;
for (int j = 0; j < str_2.length(); j++) {
if (str[i + j] != str_2[j]) {
is_same = false;
break;
}
}
if (is_same) {
std::cout << "true" << std:endl;
}
}
#包括
#包括
#包括
std::string::size\u类型查找(const std::string&s1,
常量std::string和s2)
//返回s1内s2的位置,
//如果不存在,则为NPO。
{
使用size\u type=std::string::size\u type;
大小\类型curPos=0;
size_type lim=s1.size();
size_type innerLim=s2.size();
对于(;curpos)您想检查str_1
是否包含str_2
?是的,str_1总是比ste_2长。我不必使用find()这是用于类或其他东西的。是吗?其思想基本上是实现find
?我将删除我的答案并创建一个新答案。例如:str_1=“osmanyilmaz”str_2=furkan,但它们可以是另一个词。但是str_1总是更大。如果str_2在str_1中,则显示true。如果str_2不在str_1中,则显示false。我不能使用find()这个空白处故意留白。@BoBTFish在结尾处提出了一个非常重要的观点。记下他所做的事情。使用这个想法。理解它。写下你自己的简单版本并提交。尝试提交BoBTFish刚刚做的事情会让你在本学期剩下的时间里很难打分,因为很明显你是带着重要的已有知识,否则你会因为剽窃而被打得头晕目眩。窃取想法至少是编程的一半,但计算机科学对此不屑一顾。
#include <iomanip>
#include <iostream>
#include <string>
std::string::size_type find(const std::string& s1,
const std::string& s2)
// return the position of s2 within s1,
// else npos if it is not present.
{
using size_type = std::string::size_type;
size_type curPos = 0;
size_type lim = s1.size();
size_type innerLim = s2.size();
for (; curPos<lim; ++curPos) { // loop through s1
if (lim < curPos+innerLim) {
break; // not enough space left
}
size_type innerPos = 0;
for(; innerPos < innerLim // loop through s2, while matching
&& curPos + innerPos < lim
&& s1[innerPos+curPos] == s2[innerPos];
++innerPos) ; // do nothing in the loop
if (innerPos == innerLim) { // matched the whole loop
return curPos;
}
}
return std::string::npos; // never matched
}
bool contains(const std::string& s1,
const std::string& s2)
{
return find(s1, s2)!=std::string::npos;
}
int main()
{
std::cout
<< std::boolalpha
<< contains("abc", "") << '\n' // true
<< contains("abc", "abc") << '\n' // true
<< contains("abc", "bc") << '\n' // true
<< contains("abc", "abcd") << '\n' // false
<< contains("abc", "abd") << '\n' // false
<< contains("abc", "xyz") << '\n';// false
}