I have the usual vector of integer values ​​'vector v'. It is filled with values ​​by the user, and the dimension of the vector after filling will be exactly divisible by 6. I need to divide this vector into as many vectors as there are groups of 6 numbers each. For example, if the user entered 12 numbers, then the vector should be divided into 2 vectors of 6 numbers each; if 24 numbers, then on 4 vectors, etc. Tell me please, I was searching, but I can’t find (The difficulty is that even after the user has entered everything, I can determine how many vectors I need by writing "a = v.size () / 6", but I don’t know how to break this vector into this number in a loop.

#include<iostream> #include<vector> using namespace std; int main() { int a; vector<int> v1; for (cin; ; ) { //получение всех значений cin >> a; if (a == -1) { break; } v1.push_back(a); } int size = v1.size() / 6; //количество векторов, на которые нужно разбить вектор for (int i = 0; i < size; i++) { //разбиение вектора } return 0; } 
  • Do you already have a code? Add, there is so - Alex Tremasov
  • In what form do you need a result? vector<vector<int>> good or not? - Harry
  • @Harry is good, any kind, as long as he does what he needs) - Victoria Kovalenko
  • @GinTasan added - Victoria Kovalenko

1 answer 1

Here it is .

 #include <vector> #include <iostream> using namespace std; int main(int argc, const char * argv[]) { vector<int> v; for(int a; cin >> a ; ) { if (a == -1) break; v.push_back(a); } vector<vector<int>> res(v.size()/6); for(int i = 0; i < v.size(); i++) { res[i/6].push_back(v[i]); } for(auto& w: res) { for(auto i: w) cout << i << " "; cout << endl; } } 
  • Thank you very much! - Victoria Kovalenko