我知道数组需要有一个const int来初始化,所以我在main中有这个.我想要这个主要是因为我希望能够在必要时轻松修改这些数字.
const int magicWordCount = 10;
compareWords(magicWordCount);
Run Code Online (Sandbox Code Playgroud)
该功能的声明是:
void compareWords(const int);
Run Code Online (Sandbox Code Playgroud)
定义:
void Words::compareWords(const int magicWordCount)
{
std::string magic[magicWordCount] = {};
convertToStringArray(magicBuffer, magicBufferLength);
}
Run Code Online (Sandbox Code Playgroud)
当我这样做时,intellisense告诉我,定义中的"magicWordCount"下划线,表达式必须具有常量值.我对这个值不恒定的地方感到困惑.思考?
我说这个代码会在O(N)时间执行吗?这是迭代map <int,vector <int >>.
int counter = 0;
unsigned int u = 0;
unsigned int v = 0;
bool notDone = true;
while (notDone) {
if (u < map.size()) {
if (v < map.at(u).size()) {
if (map.at(u)[v] == -1) {
++v;
}
else {
++counter;
++v;
}
}
else {
v = 0;
++u;
}
} else {
notDone = false;
}
}
Run Code Online (Sandbox Code Playgroud)