I recently try to check a substring of another string. The test thought resulting throwing an exception I couldn't quite understand:
#include <string>
#include <iostream>
bool solution(std::string const &str, std::string const &ending) {
// char string to put the substring from str
char tmpTest[ending.length()-1];
// copying sub-string the length of ending from str
std::size_t length = str.copy(tmpTest, ending.length(), str.length()-ending.length());
// add end of line
tmpTest[length] = '\0';
if (tmpTest == ending)
return true;
else
return false;
}
with the exception:
Caught std::exception, what(): basic_string::copy: __pos (which is 18446744073709551615) > this->size() (which is 3)
any hints? Thanks..