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

Monday, September 20, 2010

24 小时精通Vim

vim作为linux下的经典editor,它的学习的确让我挣扎过,尤其是当我想打字可屏幕却毫无反应的时候,我很失望,于是发誓再也不碰这东西了。

几年来,我一直使用notepad++作为我的主要代码编辑器,所以工欲善其事,必先利其器。作为代码工匠,程序员往往需要一个能够一个能够提供浏览,搜索,编辑等方面强大的功能。而notepad++这几年的发展的确在这方面不断推陈出新,受到了很多程序员的喜爱。

但是,notepad++毕竟是win平台下的(估计收了MS好处),所以在linux下我急需一个能让我顺手的工具。其实选择并不多,无非就是command based, 比如emacs,vim 或是一个common editor user interface的工具(比如KDE下的kate),我思来想去,如果不使用前两个的其中一个,我很有可能会被同事鄙视,所以我开始对vim和emacs做了一个小比较,觉得vim虽然指令复杂,但是毕竟不用去搞lisp,可是emacs却还要再去学个没几个人用语言,投资成本比较高。于是,就先选择了vim作为学习的目标。

0-1 小时

其实就是了解下vim的历史,wikipedia是一个不错的source,里面信息比较全面,但是其实也对实际操作没什么大用,但是至少知道vim的大概工作原理和设计理念。

1-3 小时

在google上发现 Efficient Editing With vim 一文,地址如下,http://jmcpherson.org/editing.html, 看完以后,感触加深了,于是按照文中的操作理念练习了一下,发现有点明白了,可是还是老忘记指令,于是还是有点着急,毕竟这样的程度还是属于低效率的,根本无法顺利展开工作。

3-6 小时

我回想起了wiki上的内容,vim是可以customize的,既然我已经熟悉了notepad++的环境,为什么不能把我想要的功能map到vim上呢。于是开始了一段有趣的客制过程。

首先想到那些syntax highlights,这些不用说,Efficient Editing With vim 的作者在blog里的其他问章里已经给出一些,剩下的google一下基本就都出来了。

于是继续想加Google search,代码折叠,自动完成,显示行号这些基本的都搞定了。小庆祝一下,可是仔细一想,notepad++好像还有很多功能我还没移植过来,比如Tab浏览,鼠标双击自动搜索加highlights,而且一个很有用的功能,ctrl+S 保存文件。

其实,google一下,vim的gui版本gvim可以实现tab浏览,双击自动搜索加highlights也有现成的key mapping script, 直接复制,保存,启动gvim,呵呵,这些东西都有了。

6-10 小时

其实之前的工作已经可以完全应付工作需要了,但是还有些小细节我觉得不太满意,输入模式要按i, 而我觉得space键更好。

Tab虽然不错,可是现在所有的主流浏览器都是ctrl+T 开新Tab,Ctrl+W 关闭Tab,我发现gvim里面有个分页浏览,也就是说你可以任意横竖分割一个Tab里面的页面去显示你的各种文件。我想Notepad++里面的goto another view是vertical分页的,那我就用shift+T 来做同样的事吧。当然我还另加了ctrl+shift+left/right 用来快速切换Tab,这个也算是比较intuitive了。

鼠标滚轮在分页浏览的时候,我们有时需要同步滚动,这功能好像notepad++没有快捷键,我就设定为shift+鼠标中键去toggle,这样同步和不同步就随心所欲了。

最后,一个column selection, notepad++是Alt+leftmouse, 当然vim也就直接map了。

10-12 小时

经过那么一段时间的练习加客制,vim的使用已经很顺手了。其强大程度已经和notepad++一样,剩下的应该就是加强学习,掌握新功能了,这方面而言,notepad++只能靠作者来实现,而有了vim,我则可以随时DIY。

这也许就是高手们都喜欢vim的原因吧。

12-24

累了一天了,该睡觉了。一天征服了vim,该知足了。

 

=======================================

.vimrc 文件内容 (我的系统没有设Alt的keybinding,所以没有Alt组合的快捷键,你可以修改相关部分以满足自己的需求)

vim wiki 里面有很多设置代码,你在里面search可能比google 更快http://vim.wikia.com/wiki/Vim_Tips_Wiki

" set editor colors
:colors torte
:syntax enable

"setting from Jonathan's tutorial
:set nocompatible

:set autoindent
:set smartindent
:set showmatch
:set ruler
:set incsearch
:set tabstop=4
:set shiftwidth=4
":set vb t_vb=

" code folding
:set foldmethod=syntax
:set nofoldenable

" code typing
:set showmatch
:set matchtime=8
" show line number
:set number
:nmap <C-N><C-N> :set invnumber<CR>
:set numberwidth=3
":set cpoptions+=n
:highlight LineNr term=bold cterm=NONE ctermfg=Green ctermbg=NONE gui=NONE guifg=Green guibg=NONE
:set wrap!

" tab navigation
map <C-S-Left>  :tabp<CR>
map <C-S-Right>  :tabn<CR>
map <C-T>  :tabnew<CR>
map <C-W>  :close<CR>
map <S-T>  :vsp<CR>

" page navigation

set scb!
map <S-MiddleMouse>  :set scb!<CR>   

map <S-Left>  F
map <S-Right> f
map <C-Up>    (
map <C-Down>  )
map <A-Left>  0
map <A-Right> $

" files
" Use CTRL-S for saving, also in Insert mode

:imap <c-s> <Esc><c-s>
if has("gui_running")
  " If the current buffer has never been saved, it will have no name,
  " call the file browser to save it, otherwise just save it.
  :map <silent> <C-S> :if expand("%") == ""<CR>:browse confirm w<CR>:else<CR>:confirm w<CR>:endif<CR>
endif

"search
"map <2-LeftMouse> :normal *<CR>, this version jump cursor to next search result
map <2-LeftMouse> :let @/ = expand("<cword>")<CR>:set hlsearch<CR>
:hi Search guifg=black guibg=white
map <RightMouse> `.

"Editing

map <Space> i
map <C-LeftMouse> <C-V>