Joh*_*nes 2 c++ boost stl segmentation-fault
#include <iostream>
#include <algorithm>
#include <vector>
#include <boost/array.hpp>
#include <boost/bind.hpp>
int main() {
boost::array<int, 4> a = {45, 11, 67, 23};
std::vector<int> v(a.begin(), a.end());
std::vector<int> v2;
std::transform(v.begin(), v.end(), v2.begin(),
boost::bind(std::multiplies<int>(), _1, 2));
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
}
Run Code Online (Sandbox Code Playgroud)
运行时,这会产生令人毛骨悚然的分段错误.请告诉我我哪里出错了.
Jam*_*lis 12
v2你打电话时的大小为零transform.您需要调整大小v2以使其至少具有v与调用之前一样多的元素transform:
v2.resize(v.size());
Run Code Online (Sandbox Code Playgroud)
或者你可以std::back_inserter在电话中使用transform:
std::transform(v.begin(), v.end(), std::back_inserter(v2), boost::bind(std::multiplies<int>(), _1, 2));
Run Code Online (Sandbox Code Playgroud)