高手们!解释解释这个std::string的append的问题!

meiyxq 2013-09-28 03:14:12
代码如下:
读取大文件,按照给定分割符对不同的数据项进行合并,合并后的值就是query的值

std::vector<string> pParamresult = split(strParam,pattern1);
std::vector<int> Indexresult;
for(int i =0;i<pParamresult.size();i++)
{
Indexresult.push_back(atoi(pParamresult[i].c_str()));
}
while (in && !in.eof())
{
queries.clear();
while (getline(in, query))
{
std::vector<string> result=split(query,pattern);
query.erase();
for(int i =0;i<Indexresult.size();i++)
{
query.append(result[Indexresult[i]]);
query.append(pattern2);
}
queries[query] += 1;
}
}


但是程序运行段时间后,会出现如下问题:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b79de1 in std::string::append(std::string const& () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0 0x00007ffff7b79de1 in std::string::append(std::string const& () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#1 0x0000000000402877 in processInput(char const*, boost::function<void (std::string const&, long)> const& (file=0x61a140 "/data/Index_2013092500_5323_eth0.log", cb=...) at st.cpp:78
#2 0x0000000000402ed9 in sffle (nbuckets=10, argc=2, argv=0x7fffffffe550) at st.cpp:150
#3 0x0000000000403cbd in main (argc=7, argv=0x7fffffffe6c at st.cpp:371
此中append 值该如何操作是安全的!
...全文
518 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
meiyxq 2013-09-28
  • 打赏
  • 举报
回复
是第一行!也就是 query.append(result[Indexresult[i]]);这个!不知道这个地方哪里有错!
TAXUEc750 2013-09-28
  • 打赏
  • 举报
回复
query.append(result[Indexresult[i]]); query.append(pattern2); 这两行代码不知道你出错的是哪行,append里面的参数你给是不是指针变量,如果是指针要考虑指针为空的情况

23,115

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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