Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/164.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ If语句无法激活_C++_Arrays_Function_C++11_Char - Fatal编程技术网

C++ If语句无法激活

C++ If语句无法激活,c++,arrays,function,c++11,char,C++,Arrays,Function,C++11,Char,我正在尝试创建一个函数,该函数应该执行以下操作: -取两个字符数组,一个比另一个小,并确定较小的字符数组是否是较大数组的子集。例如: 数组A:{s”,“c”,“h”,“o”,“o”,“l};数组B:{“c”,“h”}。我有义务使用指针递增/递减操作。这是我编写的代码: int locator(char *Bigptr, char *Smallptr) { int count = 0; for (; *Bigptr != '\0'; Bigptr++) { if (*Sma

我正在尝试创建一个函数,该函数应该执行以下操作:

-取两个字符数组,一个比另一个小,并确定较小的字符数组是否是较大数组的子集。例如: 数组A:
{s”,“c”,“h”,“o”,“o”,“l}
;数组B:
{“c”,“h”}
。我有义务使用指针递增/递减操作。这是我编写的代码:

int locator(char *Bigptr, char *Smallptr) {
int count = 0;
    for (; *Bigptr != '\0'; Bigptr++) {
        if (*Smallptr == *Bigptr) {
            for (; (*Smallptr == *Bigptr) != '\0'; Smallptr++, Bigptr++) {}
            if (*Smallptr == '\0') {
                return count;
            }
            else {
                cout << "small is not the subset of big" << endl;
                return 0;
            }
        }
        count++;
    }
return 0; 
int定位器(char*Bigptr,char*Smallptr){
整数计数=0;
对于(;*Bigptr!='\0';Bigptr++){
如果(*Smallptr==*Bigptr){
对于(;(*Smallptr==*Bigptr)!='\0';Smallptr++,Bigptr++){
如果(*Smallptr=='\0'){
返回计数;
}
否则{

难道你有几件事做错了吗。 看看这个返工:

int locator(char *Bigptr, char *Smallptr) {
    int count = 0;
    for (; *Bigptr != '\0'; Bigptr++) {
        if (*Smallptr == *Bigptr) {
            for (; *Smallptr == *Bigptr && *Bigptr!='\0'; Smallptr++, Bigptr++) {
                count++;
            }
            if (*Smallptr == '\0') {
                return count;
            }
            break; //this is necesary in order not to stay in the main for loop
        }
    }
    std::cout << "small is not the subset of big" << std::endl;

    return count;
}
int定位器(char*Bigptr,char*Smallptr){
整数计数=0;
对于(;*Bigptr!='\0';Bigptr++){
如果(*Smallptr==*Bigptr){
对于(;*Smallptr==*Bigptr&&*Bigptr!='\0';Smallptr++,Bigptr++){
计数++;
}
如果(*Smallptr=='\0'){
返回计数;
}
break;//为了不停留在主for循环中,这是必需的
}
}

std::cout线路中使用的逻辑

for (; (*Smallptr == *Bigptr) != '\0'; Smallptr++, Bigptr++) {}
这是不对的

您需要的是:

for (; (*Smallptr == *Bigptr) && *SmallPtr != '\0'; Smallptr++, Bigptr++)
{
   ++count;
}

您对
循环的第二个
的理解不正确

if (*Smallptr == *Bigptr) {
    for (; (*Smallptr == *Bigptr) != '\0'; Smallptr++, Bigptr++) {}
由于您已经确定(在该语句的
if
条件中)该
*Smallptr==*Bigptr
,该比较给出了一个非零(
true
)结果

测试
(*Smallptr==*Bigptr)!='\0'
将非零结果与值为零的
char
进行比较。非零值从不等于零(至少,与标准整数类型(包括
bool
)进行比较),因此循环没有效果

与您的描述一致的循环是

for (; (*Smallptr == *Bigptr) && *SmallPtr != '\0'; Smallptr++, Bigptr++) {}

它检查两个字符是否相等,并且两者都是非零。

,假定这是C++,您可能想利用标准库中已经存在的功能。我将按此顺序做一些事情:

bool is_subset(std::string big, std::string small) { 

    std::sort(big.begin(), big.end());
    std::sort(small.begin(), small.end());

    std::string result;
    std::set_difference(small.begin(), small.end(),
                        big.begin(), big.end(),
                        std::back_inserter(result));
    return result.empty();
}

这并不能满足使用指针的要求,但在我看来,这是一种更好的方法。

您的参数是字符数组的数组,而不是字符数组。您需要提供一个完整的示例。当我运行它时,它命中了return count,其中count==1。我可能因为传递了正确的数据类型而出错。您对a和B的声明听起来是错的。谢谢你的回答,我明白你的意思,但我有几点跟进。首先,你提到循环没有效果。这是什么意思?我用我的原始代码创建了一个虚拟变量,看看循环是否工作,它是否真的“循环”。此外,即使在测试了你的建议之后,下面的if语句e for循环仍然是错误的。为什么会是这种情况?您建议的改进不应该确保*Smallptr确实“指向”了“\0”。再次感谢您的回答,我非常感谢。您的
for
语句的结束条件在到达之前是正确的。因此,循环体(在您的情况下,这不起任何作用)或continuation语句(在您的情况下,
Smallptr++,Bigptr++
)如我所述,如果
*SmallPtr!=*BigPtr
*SmallPtr
为零,循环将停止。如果这不是您想要的,请修改以适应。嘿,José,因为我是新成员,所以我没有否决或否决的能力,所以不是我。但是,我尝试了您的代码,问题仍然存在。因此,这可能是另一个人否决你的评论的原因。你是对的。我错过了一个“中断”。此外,我认为返回计数总是更有趣。因此用户总是可以获得匹配字符的数量。现在代码已经过测试,它真的做了它应该做的事情。
bool is_subset(std::string big, std::string small) { 

    std::sort(big.begin(), big.end());
    std::sort(small.begin(), small.end());

    std::string result;
    std::set_difference(small.begin(), small.end(),
                        big.begin(), big.end(),
                        std::back_inserter(result));
    return result.empty();
}