gpt4 book ai didi

c++ - "munmap_chunk(): invalid pointer"带有 vector 算法

转载 作者:行者123 更新时间:2023-11-28 08:00:38 26 4
gpt4 key购买 nike

下一个代码被 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;
}

输出是

64个6个4个6个0

但预期是

64个6个6个4个6

最佳答案

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

你试图在 remainders.end() 位置插入元素 IN remainders.end()... 坏主意。

正确的代码是

#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;
}

关于c++ - "munmap_chunk(): invalid pointer"带有 vector 算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11576155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com