Vector mapping

Is there a good way to transfer vectors? Here is an example of what I mean:

vec0 = [0,0,0,0,0,0,0,0,0,0,0]
vec1 = [1,4,2,7,3,2]
vec2 = [0,0,0,0,0,0,0,0,0]
vec2 = [7,2,7,9,9,6,1,0,4]
vec4 = [0,0,0,0,0,0]

mainvec =
[0,0,0,0,0,0,0,0,0,0,0,1,4,2,7,3,2,0,0,0,0,0,0,0,0,0,7,2,7,9,9,6,1,0,4,0,0,0,0,0,0]

Suppose mainvec does not exist (I just show it to you so you can see the general data structure.

Now say I want mainvec (12), which would be 4. Is there a good way to map the invocation of these vectors without just stitching them together in mainvec? I understand that I can make a bunch of if statements that check the mainvec index, and then I can compensate for each call depending on where the call is inside one of the vectors, for example, for example:

mainvec(12) = vec1(1)

which i could do:

mainvec(index)
if (index >=13)
    vect1(index-11);

I wonder if there is a concise way to do this without any claims. Any ideas?

+3
4

- ?

using System.Collections.Generic;
namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] vec0 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int[] vec1 = { 1, 4, 2, 7, 3, 2 };
            int[] vec2 = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            int[] vec3 = { 7, 2, 7, 9, 9, 6, 1, 0, 4 };
            int[] vec4 = { 0, 0, 0, 0, 0, 0 };
            List<int> temp = new List<int>();
            temp.AddRange(vec0);
            temp.AddRange(vec1);
            temp.AddRange(vec2);
            temp.AddRange(vec3);
            temp.AddRange(vec4);
            int[] mainvec = temp.ToArray();
        }
    }
}
+1

.

, :

var mainvec = new int[][]{vec0, vec1, vec2, vec3, vec4};

int desiredInd = 12, totalInd = 0, rowInd = 0, result;

while(rowInd < mainvec.Length && (totalInd + mainvec[rowInd].Length) <= desiredInd)
{ 
    totalInd += mainvec[rowInd++].Length;
}

if(rowInd < mainvec.Length && (desiredInd - totalInd) < mainvec[rowInd].Length)
{
  result = mainvec[rowInd][desiredInd - totalInd];
}
+1

, , .

, , .

+1

, , Concat, -, -. , :

var vec0 = new[] {0,0,0,0,0,0,0,0,0,0,0};
var vec1 = new[] {1,4,2,7,3,2};
var vec2 = new[] {0,0,0,0,0,0,0,0,0}; 
var vec3 = new[] {7,2,7,9,9,6,1,0,4};
var vec4 = new[] { 0, 0, 0, 0, 0, 0 };

var mainvec = vec0.Concat(vec1).Concat(vec2).Concat(vec3).Concat(vec4).ToList();
mainvec[12] == 1;

, , -, , , .

+1

Source: https://habr.com/ru/post/1750017/


All Articles