c++11 - C++ terminate called after throwing an instance of 'std::out_of_range', vector of string -


i getting error when running:

terminate called after throwing instance of 'std::out_of_range' what(): basic_string::substr

problem in part of code brand new , don't understand how should solve problem. content vector of strings.

    int i=1;     std::string v1, v2, weight;     while(!content.empty())     {         v1 = content[i].substr(2,1);         v2 = content[i].substr(5,1);         weight = content[i].substr(8,1);         i++;     } 

two main problems here.

your loop go on forever (or until murder ram sticks invalid accesses), because check vector not empty, rather checking i has reached total size.

for (auto& x : content) {     const std::string v1     = x.substr(2,1);     const std::string v2     = x.substr(5,1);     const std::string weight = x.substr(8,1);      // presumably these } 

then need fix substr operations, have wrong arguments , causing exception.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -