Next code crushed with munmap_chunk(): invalid pointer
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> modules = {3,5,7};
vector<int> remainders = {2,3,2};
vector<int> m_bigs(modules.size());
int main() {
int m_big = 1;
for(int r : remainders) { m_big *= r; };
transform(m_bigs.begin(), m_bigs.end(), remainders.begin(), remainders.end(), [m_big](int m, int r){ cout << m_big / r << endl; return m_big / r; });
for(int m : m_bigs) { cout << m << endl; };
return 0;
}
the output is
6
4
6
4
6
0
but expected is
6
4
6
6
4
6
You trying to insert elements IN remainders at position remainders.end()… bad idea.
Correct code will be