I have a 6D vector and I need to check neighborhood of each element (2 elements in each direction). Of course when I am on boundary of the vector, check leads in the Segmentation fault. All I can do is having switch with ton of cases. Is there any better way to solve this? I also thought of something like try-catch.
Asked
Active
Viewed 907 times
-2
-
1A 6D vector? Jesus... – DeiDei Jan 01 '18 at 16:20
-
Yeah I know, but there is no other way to do it. – Dominik Filyo Jan 01 '18 at 16:22
-
Something like a 6D vector sounds like a weird concept, which could probably improved by segmenting an appropriate 1d in memory space appropriately. – user0042 Jan 01 '18 at 16:23
-
Or perhaps breaking it up or abstracting it away into classes might make working with it easier and less error-prone. – DeiDei Jan 01 '18 at 16:24
-
This 6D vector represents 6D space for my planning software. It is probably possible to break it into smaller pieces, but then it would be much harder to program it. – Dominik Filyo Jan 01 '18 at 16:31
-
Use `vector::at()` instead of `[ ]` to access your elements. Second, don't mask your bugs by using `try/catch` -- actually fix them. – PaulMcKenzie Jan 01 '18 at 17:31
-
Thanks for advice. I don't know what bugs are you talking about. This isn't bug, and I can't use try/catch even if I wanted to (because it isn't exception). – Dominik Filyo Jan 01 '18 at 20:02
1 Answers
1
Still too bulky but it works:
#include <iostream>
#include <array>
#include <vector>
typedef std::vector<int> Vector1D;
typedef std::vector<Vector1D> Vector2D;
typedef std::vector<Vector2D> Vector3D;
typedef std::vector<Vector3D> Vector4D;
typedef std::vector<Vector4D> Vector5D;
typedef std::vector<Vector5D> Vector6D;
typedef std::array<size_t, 6> Path;
bool GetVectorPathElement(Vector6D const &vector6D, Path const &path, int &val)
{
size_t i = 0, k = path[i];
if (vector6D.size() > k)
{
Vector5D const &vector5D = vector6D[k];
k = path[++i];
if (vector5D.size() > k)
{
Vector4D const &vector4D = vector5D[k];
k = path[++i];
if (vector4D.size() > k)
{
Vector3D const &vector3D = vector4D[k];
k = path[++i];
if (vector3D.size() > k)
{
Vector2D const &vector2D = vector3D[k];
k = path[++i];
if (vector2D.size() > k)
{
Vector1D const &vector1D = vector2D[k];
k = path[++i];
if (vector1D.size() > k)
{
val = vector1D[k];
return true;
}
}
}
}
}
}
std::cout << "Invalid path " << k << " at index " << i << std::endl;
return false;
}
int main()
{
Vector1D vector1D = { 1,2,3,4,5,6 };
Vector2D vector2D = { vector1D, vector1D, vector1D, vector1D, vector1D };
Vector3D vector3D = { vector2D, vector2D, vector2D, vector2D };
Vector4D vector4D = { vector3D, vector3D, vector3D };
Vector5D vector5D = { vector4D, vector4D };
Vector6D vector6D = { vector5D };
Path path = { 0,0,2,1,4,5 };
int element;
if (GetVectorPathElement(vector6D, path, element))
{
std::cout << "Path: ";
for (auto i : path)
std::cout << i << " ";
std::cout << "\nElement value at destination: " << element << std::endl;
}
return 0;
}

Killzone Kid
- 6,171
- 3
- 17
- 37