c++ - std::find not operating as expected -
std::find not evaluating expected.
i have vector lexeme_ defined as
static const std::string delimiters_[] = {" ", ",", "(", ")", ";", "=", ".", "*", "-"}; static std::vector<std::string> lexeme_(std::begin(delimiters_), std::end(delimiters_));
i have evaluation using std::find
defined as
while ( std::find(lexeme_.begin(),lexeme_.end(),std::string(&commandline_.at(position_))) == lexeme_.end()) { // concat each successive alphanumeric character 'token' token += commandline_.at(position_); // update index 'commandline' position_ += 1; }
the evaluation supposed compare char
in lexeme_ char
in commandline similar java expression
!lexeme.contains(character.tostring(commandline.charat(position)))
the evaluation supposed compare char
s , if determines char
in delimiters
satisfied in comparison, while loop exit.
testcase
#include<algorithm> #include<iostream> static const std::string delimiters_[] = {" ", ",", "(", ")", ";", "=", ".", "*", "-"}; static std::vector<std::string> lexeme_(std::begin(delimiters_), std::end(delimiters_)); std::string commandline = "check me"; while (std::find(lexeme_.begin(),lexeme_.end(),std::string(&commandline_.at(position_))) == lexeme_.end()) { std::cout "i should stop printing when encountering space ' ' << std::endl; }
the constructor temporary comparison string incorrect. isn't building single-character string, it's building string starting @ character , going end of original string, if you're lucky - there might std::string
implementation out there somewhere doesn't automatically 0 terminate internal buffer.
so instead of this:
std::string(&commandline_.at(position_))
use:
std::string(1, commandline_.at(position_))
Comments
Post a Comment