关于boost::regex的问题,求解决

winglq 2010-07-27 02:36:47
// 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;
}
...全文
116 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 winglq 的回复:]

引用 14 楼 taodm 的回复:

google “临时变量”

股沟了下,量太大,看了几篇都不像,求明说,或贴个网址也成,谢
[/Quote]
知道了,谢谢了,呵呵
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 taodm 的回复:]

google “临时变量”
[/Quote]
股沟了下,量太大,看了几篇都不像,求明说,或贴个网址也成,谢
taodm 2010-07-27
  • 打赏
  • 举报
回复
google “临时变量”
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 taodm 的回复:]

楼主理解t[1].str()发生了什么么?
[/Quote]
Effects: Returns sub-expression sub as a string: string_type((*this)[sub]).
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 taodm 的回复:]

楼主理解t[1].str()发生了什么么?
[/Quote]
查找到的字符串的第一个子串吧,请直接说原因吧,呵呵,新手
taodm 2010-07-27
  • 打赏
  • 举报
回复
楼主理解t[1].str()发生了什么么?
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 ayw215 的回复:]

引用 8 楼 winglq 的回复:

引用 7 楼 ayw215 的回复:

//_sum+=lexical_cast<int>(num);
这句bad_cast了?

是啊,num指向一个空字符串。试过把模板函数换成smatch的形参还是这样

都发现问题所在了
那就干掉它吧
[/Quote]
在学boost说以想知道为什么,呵呵
ayw215 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 winglq 的回复:]

引用 7 楼 ayw215 的回复:

//_sum+=lexical_cast<int>(num);
这句bad_cast了?

是啊,num指向一个空字符串。试过把模板函数换成smatch的形参还是这样
[/Quote]
都发现问题所在了
那就干掉它吧
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 ayw215 的回复:]

//_sum+=lexical_cast<int>(num);
这句bad_cast了?
[/Quote]
是啊,num指向一个空字符串。试过把模板函数换成smatch的形参还是这样
ayw215 2010-07-27
  • 打赏
  • 举报
回复
//_sum+=lexical_cast<int>(num);
这句bad_cast了?
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ayw215 的回复:]

引用 3 楼 winglq 的回复:

引用 1 楼 ayw215 的回复:

t[1].str().c_str();返回的其实是const char*

错了,加了const 的,不是编译错误是运行错误。vs2008

单步调一下看啊
看你的强转成功了么
[/Quote]
为什么会有强转啊?就是因为num是空指针。上下两句话不是应该没区别吗
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 taodm 的回复:]

呃,具体编译出错消息。另外,注意行数别数错了。
再另外,请先google“提问的智慧”学习。
你提的问题和regex搭不上啊。
[/Quote]
是运行时错误:bad cast。
但不是因为类型转化的原因,不知道为什么num里是空指针
ayw215 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 winglq 的回复:]

引用 1 楼 ayw215 的回复:

t[1].str().c_str();返回的其实是const char*

错了,加了const 的,不是编译错误是运行错误。vs2008
[/Quote]
单步调一下看啊
看你的强转成功了么
winglq 2010-07-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 ayw215 的回复:]

t[1].str().c_str();返回的其实是const char*
[/Quote]
错了,加了const 的,不是编译错误是运行错误。vs2008
taodm 2010-07-27
  • 打赏
  • 举报
回复
呃,具体编译出错消息。另外,注意行数别数错了。
再另外,请先google“提问的智慧”学习。
你提的问题和regex搭不上啊。
ayw215 2010-07-27
  • 打赏
  • 举报
回复
t[1].str().c_str();返回的其实是const char*

24,853

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧