“munmap_chunk():无效指针”与向量上的算法

时间:2012-07-20 08:58:44

标签: c++ c++11 g++ std glibc

使用 munmap_chunk():无效指针

压缩下一个代码
#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;
}

输出

6 4 6 4 6 0

但预计是

6 4 6 6 4 6

1 个答案:

答案 0 :(得分:1)

template < class InputIterator1, class InputIterator2,
           class OutputIterator, class BinaryOperator >
  OutputIterator transform ( InputIterator1 first1, InputIterator1 last1,
                             InputIterator2 first2, OutputIterator result,
                             BinaryOperator binary_op );

你试图在剩余的位置插入剩余的元素剩余的东西。发现...(不好主意。)

正确的代码将是

#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(), m_bigs.begin(), [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;
}