向量的std::find()出错
Error with std::find() for a vector
我得到了RUN FAILED (exit value 1, total time: 493ms)
。当我试图检查向量中是否有数组中的元素时,我得到了它:
if (find(cycles.begin(), cycles.end(), permutation[i]) == cycles.end()) {
startCycle = permutation[i];
break;
}
程序的完整代码:
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define TESTING_FILE_IN
//#define TESTING_FILE_OUT
#define DEBUG
//#define SHOW_TIMING
vector< int > cycles;
int permutation[1001];
/*
*
*/
int main() {
#ifdef TESTING_FILE_IN
freopen("in.txt", "r", stdin);
#endif
int ind, startCycle, n, count, elemProc;
scanf("%d", &n); //Number of elements in the permutation
for (int i = 0; i < n; i++) {
cin >> permutation[i];
}
// Calculate cycles
startCycle = 1;
while (true) {
cycles.push_back(ind + 1);
elemProc++;
ind = permutation[ind] - 1;
if (ind == startCycle) {
cycles.push_back(startCycle);
cycles.push_back(-1);
count++;
for (int i = 0; i < n; i++) {
if (find(cycles.begin(), cycles.end(), permutation[i]) == cycles.end()) {
startCycle = permutation[i];
break;
}
}
}
if (elemProc == n)
break;
}
cout << count << endl;
for (int i = 0; i < cycles.size(); ++i) {
if (cycles[i] != -1)
cout << cycles[i] << " ";
else
cout << endl;
}
return 0;
}
当我对一段进行搜索的代码进行注释时,它会构建并运行良好。希望你能帮助我。提前谢谢。
当您定义局部变量时,您可能会认为它们的初始值自动为零。事实并非如此对于不带初始值设定项的内置类型的非静态局部变量,其初始值可以是任何东西。
v----you should initialize these local variables
int ind, startCycle, n, count, elemProc;
您可以将其定义为
int ind = 0, startCycle = 0, n = 0, count = 0, elemProc = 0;
在将ind
用作索引之前,似乎没有给它一个初始值
ind = permutation[ind] - 1;
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 使用运算符==的std::find时出错
- 在用户定义对象上调用std::find时出错
- 向量的std::find()出错
- 尝试使用从 ptr_map::find 返回的迭代器时出错