以下代码是为运算研究中的问题填充一个零和一个零的矩阵 - 我将为您提供详细信息。我遇到的问题是有6个嵌套循环,即使代码编译完美,最后的print语句也不会执行(但我不会得到任何错误消息)。任何人都可以告诉我为什么会发生这种情况,并且,如果6个嵌套循环太多而无法以这种方式处理,是否会有其他方法来实现相同的。提前谢谢。
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<vector<int> > A(480, std::vector<int>(100000));
int index = 1;
int N;
int N2;
int N3;
int N4;
int N5;
vector<int> indices1;
vector<int> indices2;
vector<int> indices3;
vector<int> indices4;
vector<int> indices5;
for (int s1 = 0; s1 <= 480 - 24 + 1; s1++)
{
if (s1 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s2 = s1 + 24 + 24 - 1; s2 <= 480 - 24 + 1; s2++)
{
if (s2 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s3 = s2 + 24 + 24 - 1; s3 <= 480 - 24 + 1; s3++)
{
if (s3 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s4 = s3 + 24 + 24 - 1; s4 <= 480 - 24 + 1; s4++)
{
if (s4 + 24 + 24 - 1 <= 480 - 24 + 1)
{
for (int s5 = s4 + 24 + 24 - 1; s5 <= 480 - 24 + 1; s5++)
{
// generate print5
for (int pos = 0; pos <= 10; pos++)
{
indices1.push_back(s1 + pos);
indices1.push_back(s1 + 13 + pos);
indices1.push_back(s2 + pos);
indices1.push_back(s2 + 13 + pos);
indices1.push_back(s3 + pos);
indices1.push_back(s3 + 13 + pos);
indices1.push_back(s4 + pos);
indices1.push_back(s4 + 13 + pos);
indices1.push_back(s5 + pos);
indices1.push_back(s5 + 13 + pos);
}
std::sort(indices1.begin(), indices1.end());
// now loop over print5, N is number of elements in indices1
N = 11 * 10;
for (int ind1 = 0; ind1 < N; ind1++)
{
A [ indices1[ind1] ][index] = 1;
}
index = index + 1;
}
}
//else statement
//generate print4
for (int pos = 0; pos <= 10; pos++)
{
indices2.push_back(s1 + pos);
indices2.push_back(s1 + 13 + pos);
indices2.push_back(s2 + pos);
indices2.push_back(s2 + 13 + pos);
indices2.push_back(s3 + pos);
indices2.push_back(s3 + 13 + pos);
indices2.push_back(s4 + pos);
indices2.push_back(s4 + 13 + pos);
}
std::sort(indices2.begin(), indices2.end());
//now loop over print4
N2 = 11 * 8;
for (int ind2 = 0; ind2 < N2; ind2++)
{
A [ indices2[ind2] ][index] = 1;
}
index = index + 1;
}
}
//else statement
// generate print3
for (int pos = 0; pos <= 10; pos++)
{
indices3.push_back(s1 + pos);
indices3.push_back(s1 + 13 + pos);
indices3.push_back(s2 + pos);
indices3.push_back(s2 + 13 + pos);
indices3.push_back(s3 + pos);
indices3.push_back(s3 + 13 + pos);
}
std::sort(indices3.begin(), indices3.end());
//now loop over print3
N3 = 11 * 6;
for (int ind3 = 0; ind3 < N3; ind3++)
{
A [ indices3[ind3] ][index] = 1;
}
index = index + 1;
}
}
//else statement
//generate print2
for (int pos = 0; pos <= 10; pos++)
{
indices4.push_back(s1 + pos);
indices4.push_back(s1 + 13 + pos);
indices4.push_back(s2 + pos);
indices4.push_back(s2 + 13 + pos);
}
std::sort(indices4.begin(), indices4.end());
//now loop over print2
N4 = 11 * 4;
for (int ind4 = 0; ind4 < N4; ind4++)
{
A [ indices4[ind4] ][index] = 1;
}
index = index + 1;
}
}
//last else statement
//generate print1
for (int pos = 0; pos <= 10; pos++)
{
indices5.push_back(s1 + pos);
indices5.push_back(s1 + 13 + pos);
}
std::sort(indices5.begin(), indices5.end());
//now loop over print1
N5 = 11 * 2;
for (int ind5 = 0; ind5 < N5; ind5++)
{
A [ indices5[ind5] ][index] = 1;
}
index = index + 1;
}
// now print elements - I only print till element 50 here, since I dont actually know how many
// columns I need, therefore I initialized that to a big number (since I will need a lot)
for (int i = 0; i < 480; i++)
{
for (int j = 0; j < 50; j++)
{
cout << A[i][j];
}
}
return 0;
}
答案 0 :(得分:1)
如果你输入:
if (s5 % 100 == 0)
cout << s1 << ' ' << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5
<< '\r' << std::flush;
正好在
之下for (int s5 = s4 + 24 + 24 - 1; s5 <= 480 - 24 + 1; s5++)
{
您将了解算法的速度(并且您会立即看到它&#39; l&#34;永远不会结束)。
我还没有深入研究你的问题所以我不会尝试不同的方法。保持算法,你需要解决一些问题:
你应该检查(并在某处重置)index
变量:它变得越来越大,你会得到一个分段错误(A[indices1[ind1]][index]
)。< / p>
indices1
,... indices5
向量会迅速变大,std::sort
函数会占用大量时间。
在我看来,你并没有使用向量中的所有元素,例如:
std::sort(indices1.begin(), indices1.end());
N = 11 * 10;
for (int ind1 = 0; ind1 < N; ++ind1)
A [indices1[ind1]][index] = 1;
如果您只需要第一个N
元素,则可以使用std::partial_sort
:
N = 11 * 10;
std::partial_sort(indices1.begin(), indices1.begin() + N, indices1.end());
这会更快(但可能不够快)。