c++ - "binary '[' : no operator found -
i have 2 vectors of strings:
std::vector<std::string> savestring{"1", "3", "2", "4"}; // numbers std::vector<std::string> save2{"a", "b", "c", "d"}; // names and wish reorder latter based on former, ends being {"a", "c", "b", "d"}. tried this:
for (int i=0; i<savestring.size(); i++) { savestring[i] = save2[savestring[i]]; } but getting error:
"binary '[' : no operator found takes right-hand operand of type 'std::basic_string<_elem,_traits,_alloc>' (or there no acceptable conversion)"
what mean , wrong code?
the problem savestring[i] std::string whereas there should integer inside square brackets in save2[]. so, solution first convert std::string integer writing custom function.
so, change to:
// converts std::string int int toint( const std::string& obj ) { std::stringstream ss; ss << obj; int ret; ss >> ret; return ret; } for(int i=0;i<savestring.size();i++) { savestring[i]=save2[toint(savestring[i])]; } don't forget include sstream header writing #include <sstream> @ top.
Comments
Post a Comment