关于boost::regex的问题,求解决
// regtest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include "boost\regex.hpp"
#include "boost\lexical_cast.hpp"
using std::string ;
using namespace boost;
using std::cout;
using std::endl;
class regex_callback
{
public:
regex_callback():_sum(0){}
template<typename T>void operator()(const T& t)
{
char* num=t[1].str().c_str();
_sum+=lexical_cast<int>(t[1].str().c_str());
//可以编译通过
//_sum+=lexical_cast<int>(num);
//不知道为什么上面这句是错误的???????
}
int sum()
{
return _sum;
}
private:
int _sum;
};
int _tmain(int argc,_TCHAR*argv[])
{ // 3 digits, a word, any character, 2 digits or "N/A", // a space, then the first word again
string str("9,39,3,2,32,3,8");
regex reg("(\\d+),?");
regex_callback cb;
boost::sregex_iterator beg(str.begin(),str.end(),reg);
sregex_iterator end;
cout<<for_each(beg,end,cb).sum()<<endl;
return 0;
}