부스트 람다 rand
을 평가합니다. 당신은 그것을 bind
해야하는, 그래서는 람다 평가시에 평가 된 것 : 예상대로
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp> // for bind
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
int main()
{
namespace bl = boost::lambda;
typedef std::vector<int> int_vec;
static const size_t MaxListSize = 10;
static const int MaxSize = 20;
int_vec theList;
theList.resize(MaxListSize);
std::srand(static_cast<unsigned>(std::time(0)));
std::for_each(theList.begin(), theList.end(),
bl::_1 = bl::bind(std::rand) % MaxSize);
std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ');
}
이 작동합니다.
그러나 올바른 해결 방법은 generate_n
입니다. 왜 그것들을 덮어 쓰려고 0을 만들까요?
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <vector>
int main()
{
namespace bl = boost::lambda;
typedef std::vector<int> int_vec;
static const size_t MaxListSize = 10;
static const int MaxSize = 20;
int_vec theList;
theList.reserve(MaxListSize); // note, reserve
std::srand(static_cast<unsigned>(std::time(0)));
std::generate_n(std::back_inserter(theList), MaxListSize,
bl::bind(std::rand) % MaxSize);
std::for_each(theList.begin(), theList.end(), std::cout << bl::_1 << ' ');
}
감사합니다. Roger. :) – GManNickG
아, 훌륭한 답변입니다. – Demps