用algorithm中copy方法复制string中的一段到另一个string中

share_feelings 2009-05-15 01:20:30
std::string str_temp;

std::string::const_iterator iter_current;
std::string::const_iterator iter_x;
std::string::iterator iter_temp;

iter_temp=str_temp.begin();
std::copy(iter_current,iter_x,iter_temp);

结果编译通过,但是运行出错,调试时说这里复制出错:
Unhandled Exception in tes.exe ox:***** :Aceess Violation

大家看看这是怎么回事????
...全文
176 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
用insert方法行了。。
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
8楼不行,报错..
pengzhixi 2009-05-15
  • 打赏
  • 举报
回复
std::copy(iter_current,iter_x,back_inserter(str_temp));
pengzhixi 2009-05-15
  • 打赏
  • 举报
回复
std::copy(iter_current,iter_x,bakc_inserter(str_temp));应该可以
share_feelings 2009-05-15
  • 打赏
  • 举报
回复
void StringDeal::PolynomialStringDeal(const std::string& str,std::vector<double>& coeff,std::vector<int>& exp)
{
assert(StringCheck::PolynomialCheck(str)<0);

std::string str_temp;

std::string::const_iterator iter_positive;
std::string::const_iterator iter_negative;
std::string::const_iterator iter_current;
std::string::const_iterator iter_x;
std::string::iterator iter_temp;

iter_current=str.begin();
iter_positive=std::find(iter_current,str.end(),'+');
iter_negative=std::find(iter_current,str.end(),'-');
while(iter_positive!=str.end() || iter_negative!=str.end())
{
std::cout<<*iter_current<<std::endl;
if(iter_positive-iter_negative>0)
{
iter_x=std::find(iter_current,iter_negative,'x');
std::copy(iter_current,iter_x,str_temp.begin());
coeff.push_back(atof(str_temp.c_str()));
if(iter_negative-(iter_x+2)>0)
{
//str_temp.clear();
std::copy(iter_x+2,iter_negative,str_temp.begin());
exp.push_back(atoi(str_temp.c_str()));
}
iter_current=iter_negative;
}
else
{
iter_x=std::find(iter_current,iter_positive,'x');
iter_temp=str_temp.begin();
std::copy(iter_current,iter_x,str_temp.begin());
coeff.push_back(atof(str_temp.c_str()));
if(iter_positive-(iter_x+2)>0)
{
// str_temp.clear();
std::copy(iter_x+2,iter_positive,str_temp.begin());
exp.push_back(atoi(str_temp.c_str()));
}
iter_current=iter_positive;
}
// str_temp.clear();
iter_positive=std::find(iter_current,str.end(),'+');
iter_negative=std::find(iter_current,str.end(),'-');
}
}

这是完整的一个函数,处理多项式的字符串,把系数存入coeff,指数存入exp中。。
pengzhixi 2009-05-15
  • 打赏
  • 举报
回复
用insert
str_temp.insert(str_temp.begin(),iter_current,iter_x)
crst_zh 2009-05-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lori227 的回复:]
std::string::const_iterator iter_current;
std::string::const_iterator iter_x;

这两个有赋值吗~
[/Quote]

初值都没赋,你期望它指向哪个呢?

看到内存访问错误第一个就该想到指针
lori227 2009-05-15
  • 打赏
  • 举报
回复
std::string::const_iterator iter_current;
std::string::const_iterator iter_x;

这两个有赋值吗~
nuoshueihe 2009-05-15
  • 打赏
  • 举报
回复
std::string str_temp; 

std::string::iterator iter_current;
std::string::iterator iter_x;
std::string::iterator iter_temp;

iter_temp=str_temp.begin();
std::copy(iter_current,iter_x,iter_temp);
nuoshueihe 2009-05-15
  • 打赏
  • 举报
回复
一个是const_iterator
一个是iterator
类型不匹配

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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