mercredi 4 février 2015

Runtime error while using string inside for each

In this code, I receive the following runtime error:



terminate called after throwing an instance of 'std::out_of_range'

what(): basic_string::replace bash: line 1: 9471 Aborted

(core dumped)



As far as I am concerned, it means that I have manipulated the vector inside the for each loop, while I have not done that.



#include <iostream>
#include <string>
#include <vector>


std::string replace(std::string text,
std::string find,
std::string replace)
{
return(text.replace(text.find(find), find.length(), replace));
}

int main()
{
std::vector<std::string> mylist={"col1","col2","col3","col4","col5"};

for(const std::string item: mylist)
{
std::cout<<replace(item,"cell","item")<<std::endl;
}

return 0;
}

Aucun commentaire:

Enregistrer un commentaire