我试图在类中使用指针变量,但它给了我一个错误
Car.cpp:15:16: 错误: ->* 的右手操作数具有非成员指针类型 'int *' return this->*tires;
这是我的程序
主程序
#include <iostream>
#include "Car.h"
using namespace std;
int main(){
int x = 5;
Car honda("honda", &x);
cout << honda.getBrand() << " " << honda.getTires() << endl;
x = 6;
cout << honda.getBrand() << " " << honda.getTires() << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
车.h
#ifndef CAR_H
#define CAR_H
#include <string>
using namespace std;
class Car {
private:
string brand;
int *tires;
public:
Car(string brand, int *tires);
string getBrand();
int getTires(); …Run Code Online (Sandbox Code Playgroud) 所以我一直在研究子字符串搜索算法,并发现大多数算法(如 kmp 和 rabin-karp 算法)在进行一些字符串匹配之前需要额外的时间复杂度来进行预处理时间。这样做有什么好处吗?为什么他们不立即跳到字符串匹配,以便大 O 时间复杂度不会下降到 O(m+n)?我尝试通过简单地跳过预处理时间来创建一个我认为是 O(n) 的子串算法(如果我错了,请纠正我)。我想知道为什么人们不这样做,请参考下面的 C 代码。
int search(char hay[], char needle[], int hayLen, int needleLen){
int found;
int i = 0;
while (i < (hayLen - needleLen + 1)){
if (hay[i] == needle[0]){
found = 1;
for (int j=0; j<needleLen; j++){
if (hay[i] != needle[j]){
found = 0;
break;
}
i++;
}
if (found)
return i - needleLen;
}
else
i++;
}
return -1;
}
Run Code Online (Sandbox Code Playgroud)
编辑:
删除了 strlen 函数以避免任何不需要的时间复杂性
我只是想将 2 个数字相乘并将它们存储在一个无符号 long long 变量中。然而结果似乎是错误的,我也确保结果不会导致任何溢出发生。为什么它计算出错误的数字?
#include <stdio.h>
int main() {
unsigned long long num = 0;
num = 260000000 * 25;
printf("num = %llu\n", num);
num = 0;
for (int i=0; i<25; i++){
num += 260000000;
}
printf("num = %llu\n", num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Result:
num = 18446744071619617024
num = 6500000000
Run Code Online (Sandbox Code Playgroud)
请注意,在第一个示例中,我直接相乘,它产生了错误的数字,而第二个示例通过手动循环它们给出了正确的答案。