'How to store every sequentially increasing sequence in a vector into new vectors

I have a vector with the following elements: std::vector<int> vectorOfInts{ 95, 137, 138, 139, 140, 156, 157, 158, 159 };

Problem: I'm trying to store each sequence in a new vector, where each sequence is defined as a list of sequentially increasing values (that are increasing by 1).

Some properties of vectorOfInts:

  • The number of sequences are known, but the length of each sequence are unknown as well as the first and last element in each sequence.
  • All the elements are always sorted, from lowest to highest.
  • A sequence is defined by a list of numbers, where the next number is 1 greater than the previous number.
  • The shortest length of a sequence is 1.

For this example, there are 3 sequences: [95], [137, 138, 139, 140] and [156, 157, 158, 159]. 95 is the first and last element of sequence 1, 137 is the first element of sequence 2 and 140 is the last element of sequence 2, and 156 is the first element of sequence 3 and 159 is the last element of sequence 3.

I have to store the sequences in a new and different vector for each sequence, i.e., std::vector<int> vec1 = {95}, std::vector<int> vec2 = {137, 138, 139, 140}, and std::vector<int> vec3 = {156, 157, 158, 159}.

I think I have to loop through the vectorOfInts and check if the number at the next index is 1 greater than the number on the current index. I have also thought about partitioning the vectorOfInts according to some criteria.

Some pseudocode:

#include <vector>

int main()
{
  std::vector<int> vectorOfInts{ 95, 137, 138, 139, 140, 156, 157, 158, 159 };
  std::vector<int> vec1;
  std::vector<int> vec2;
  std::vector<int> vec3;
  for (size_t i = 0; i < vectorOfInts.size(); i++)
  {
    vec1.push_back(vectorOfInts[i])
    if (vectorOfInts[i + 1]  is one greater than vectorOfInts[i])
    {
      vec1.push_back(vectorOfInts[i]);
    }
    else
    {
      vec2.push_back(vectorOfInts[i]);
    }
  }
return 0;
}


Solution 1:[1]

You can use this algorithm: Create a vector of vectors that contains one vector containing the first element of the input vector. These are the output vectors. For each element of the input vector after the first, if the element is not previous element +1, then push a new vector to the output. Insert the current element to the last output vector.

Solution 2:[2]

I solved my problem thanks to @eerorika

Here is the code:

std::vector<int> vectorOfInts{ 95, 137, 138, 139, 140, 156, 157, 158, 159 };
std::vector<std::vector<int>> matrixOfIndicies;
matrixOfIndicies.push_back(std::vector<int>(1, vectorOfInts[0]));

for (size_t i = 1; i < vectorOfInts.size(); i++)
{
    if (vectorOfInts[i] != (vectorOfInts[i - 1] + 1))
    {
        matrixOfIndicies.push_back(std::vector<int>(1, vectorOfInts[i]));
    }
    else
    {
        matrixOfIndicies.back().push_back(vectorOfInts[i]);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 eerorika
Solution 2 jrn6270