Tuesday, September 28, 2010

C++ 0x and boost in Visual studio 2010

first download and install the compiled binary from http://www.boostpro.com/download/

so you can skip the compiling the boost by yourself.

open VC++, get a new console application

image

then set the additional include to the the path for boost

path_setting

paste the following code to the main cpp file

// boost_acc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <vector>
#include <algorithm>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/moment.hpp>

using namespace std;
using namespace boost::accumulators;

void showPtr(void*& ptr){
cout << "Input pointer address is: " << ptr <<endl;
}

int main(){

int x = 1; auto y = x;
void* p=&x;showPtr(p);
p=&y;showPtr(p);
typedef decltype(y) Tv;
// Create a vector object with each element set to 1.
vector<Tv> v(10);
// Define an accumulator set for calculating the mean and the 2nd moment ...
accumulator_set<Tv, stats<tag::mean, tag::moment<2>>> acc;

// Assign each element in the vector to the sum of the previous two elements.
generate_n(v.begin() + 2, v.size() - 2, [=,&acc]() mutable throw() -> Tv {
// Generate current value.Update previous two values.
auto n = x + y; x = y; y = n;
acc(n);
return n;
});

for_each(v.begin(), v.end(), [](const Tv& n) { cout << &n << " "<<endl; });

// Display the results ...
std::cout << "Mean: " << mean(acc) << std::endl;
std::cout << "Moment: " << moment<2>(acc) << std::endl;

return 0;
}


press ctrl+F5, you can see the code being compiled and executed.



read more about c++ 0x at http://en.wikipedia.org/wiki/C%2B%2B0x

No comments: