Splitting/dividing vector

Hi everyone. I would like to ask. I got a task to split or divide vector and copy it into new vector. I don't have idea how to do it. Is there any ways to do it? Any advice greatly appreciated.

The situation is as follows:
In the following code, vector has been summed up and each time the amount of the summation reaches 15, zero will be inserted. But now, in the next task I need to split the vector each time the summation reaches 15. The new vector need to be creates from each vector row.

Original vector:
0 5 3 2 4 6 7 1 6 5 3 1 8 6 1 9 3 2 1
0 0 6 7 1 6 5 3 9 3 2 1 7 6 2 5 3 2 4
0 8 6 1 9 3 3 1 6 7 1 5 2 3 4 6 5 3 1

Here is the output I got after inserting zero:
0 5 3 2 4 0 6 7 1 0 6 5 3 1 0 8 6 1 0 9 3 2 1
0 0 6 7 1 0 6 5 3 0 9 3 2 1 0 7 6 2 0 5 3 2 4
0 8 6 1 0 9 3 3 0 1 6 7 1 0 5 2 3 4 0 6 5 3 1

Now, after split, the output should be as:

New vector for row 1:
0 5 3 2 4
0 6 7 1
0 6 5 3 1
0 8 6 1
0 9 3 2 1
New vector for row 2:
0 0 6 7 1
0 6 5 3
0 9 3 2 1
0 7 6 2
0 5 3 2 4
New vector for row 3:
0 8 6 1
0 9 3 3
0 1 6 7 1
0 5 2 3 4
0 6 5 3 1

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

const int row = 3;	 
const int col = 19;

vector<vector<int> > vec(row, vector <int>(col, 0));
vector<vector<int> > new_vec_row1(5, vector <int>(5, 0));
vector<vector<int> > new_vec_row2(5, vector <int>(5, 0));
vector<vector<int> > new_vec_row3(5, vector <int>(5, 0));

int main () 
{ 
    // Input file
	ifstream inpData("myData.txt");
	
	// Assign data into array 
	cout << "Original vector: "; 
		cout << endl;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			inpData >> vec[i][j];
			cout << "  " << vec[i][j];
		}
		cout << endl;
	}
	cout << endl;
	
	// Close input files
	inpData.close();

	for (int i = 0; i < row; i++)
	{
		int sum = 0;
		int Q = 15;
    
		// Iterator used to store the position of searched element 
		vector<int>::iterator itr; 
	
		for (itr = vec[i].begin(); itr != vec[i].end(); itr++)
		{
			if (sum + *itr > Q)
			{
				// inserting an element invalidates the iterator, get a new one
				itr = vec[i].insert(itr, 0);
				sum = 0;
			}
			sum += *itr;
		}
	}

	// Print new vector 
	cout << "New vector with inserting element: "; 
		cout << endl;
    for (int i = 0; i < vec.size(); i++) 
	{
		for (int j = 0; j < vec[i].size(); j++)
		{
			cout << "  " << vec[i][j];
		}
		cout << endl;
	}   
    cout << endl; 

	system("pause");
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <sstream>

using namespace std;

const int row = 3;	 
const int col = 19;


vector<vector<int> > vec(row, vector <int>(col, 0));

// I've placed all target vectors themselve into a vector :)

//vector<vector<int> > new_vec_row1(5, vector <int>(5, 0));
//vector<vector<int> > new_vec_row2(5, vector <int>(5, 0));
//vector<vector<int> > new_vec_row3(5, vector <int>(5, 0));

using ull = unsigned long long;

// I've put the data of the file into a istringstream for
// making able testing the program at an online compiler :)
istringstream inpData(
"0 5 3 2 4 6 7 1 6 5 3 1 8 6 1 9 3 2 1 \
0 0 6 7 1 6 5 3 9 3 2 1 7 6 2 5 3 2 4 \
0 8 6 1 9 3 3 1 6 7 1 5 2 3 4 6 5 3 1"
);

int main () 
{ 
    // Input file
//	ifstream inpData("myData.txt");
	
	// Assign data into array 
	cout << "Original vector: "; 
		cout << endl;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < col; j++)
		{
			inpData >> vec[i][j];
			cout << "  " << vec[i][j];
		}
		cout << endl;
	}
	cout << endl;
	
	// Close input files
//	inpData.close();

	for (int i = 0; i < row; i++)
	{
		int sum = 0;
		int Q = 15;
    
		// Iterator used to store the position of searched element 
		vector<int>::iterator itr; 
	
		for (itr = vec[i].begin(); itr != vec[i].end(); itr++)
		{
			if (sum + *itr > Q)
			{
				// inserting an element invalidates the iterator, get a new one
				itr = vec[i].insert(itr, 0);
				sum = 0;
			}
			sum += *itr;
		}
	}

	// Print new vector 
	cout << "New vector with inserting element: "; 
		cout << endl;
    for (ull i = 0; i < vec.size(); i++) 
	{
		for (ull j = 0; j < vec[i].size(); j++)
		{
			cout << "  " << vec[i][j];
		}
		cout << endl;
	}   
    cout << endl; 


    // splitting the vectors
    vector<vector<vector<int>>> splitted_v;  // Holds all three overall vectors
    vector<vector<int>> splitted_row_v;   // Holds the vector of a row
    vector<int> splitted_num_v;          // Holds a sequence
    for (ull row = 0; row < vec.size(); row++)
    {   
        // Handling each row
        bool zero_at_begin = true;
        for (ull col = 0; col < vec[row].size(); col++)
        {
            // Handling preceding zeros specially
            int n = vec[row][col];
            if (n != 0) zero_at_begin = false;
            if (n == 0 && zero_at_begin)
            {
                splitted_num_v.push_back(n);
                continue;
            }
            if ( vec[row][col] == 0)
            {
                splitted_row_v.push_back(splitted_num_v);
                splitted_num_v.clear();
            }
            splitted_num_v.push_back( vec[row][col] );
        }
        splitted_row_v.push_back( splitted_num_v );
        splitted_num_v.clear();
        splitted_v.push_back( splitted_row_v);
        splitted_row_v.clear();
    }
    
    // Printing the splitted vectors:        
    for (ull i = 0; i < splitted_v.size(); i++)
    {
        std::cout << "New vector for row " << i+1 << ":\n";
        for (ull row = 0; row < splitted_v[i].size(); row++)
        {
            for (ull col = 0; col < splitted_v[i][row].size(); col++)
            {
                cout << "  " << splitted_v[i][row][col];
            }
            cout << endl;
        }
    }
    
        
	system("pause");
	return 0;
}
http://coliru.stacked-crooked.com/a/50a5f5fff775f1ba
Maybe there is a shorter solution possibele by using the std algorithm library facilities.
Last edited on
The vector has a constructor that accepts a range: http://www.cplusplus.com/reference/vector/vector/vector/

Given iterators foo and bar that are valid for vector gaz and bar==foo+n:
1
2
std::vector copy( foo, bar );
// use the copy 


Considering that you will append copies to a list:
1
2
3
vector<vector<int>> list;

list.emplace_back( foo, bar ); // append a copy 
Hi nuderobmonkey and keskiverto,

Thanks a lot for the help from both of you. I will try to learn from your code and will try to find out other possible solution using std algorithm.

Really appreciated.
Topic archived. No new replies allowed.