I need a function that needs to check if the input (std::string) is unique and return it's corresponding enum value.
I already have been able to implement this function with just a simple vector, which checks if the input is unique.
std::string checkIfInputUnique(std::string inputString, std::vector<std::string> commands)
{
std::transform(inputString.begin(), inputString.end(),inputString.begin(), ::toupper);
std::string uniqueCommand = "";
std::string commandInVector = "";
bool uniqueCommandFlag = true;
for(std::string command: commands){
int i = 0;
int length = inputString.length();
std::string tempUniqueCommand = "";
while(i != length){
if(inputString.at(i) != command.at(i)){
break;
}
tempUniqueCommand.push_back(inputString.at(i));
i++;
if(command.length() < inputString.length()){
break;
}
}
if(tempUniqueCommand.length() > uniqueCommand.length()){
uniqueCommandFlag = true;
uniqueCommand = tempUniqueCommand;
commandInVector = command;
}
else if (tempUniqueCommand.length() == uniqueCommand.length()){
uniqueCommandFlag = false;
}
}
if(uniqueCommand.length() == 0){
return "NOT FOUND";
}
else if(uniqueCommandFlag == false){
return "NOT UNIQUE";
}
else{
return commandInVector;
}
}
For example: if my inputString = "HEL" and my vector containst{"HELLO", "WORLD"}, it returns "HELLO".
However, instead of a vector, I now have a map of an enum (key) and a string (value), and when the string in the map is unique, it should return it's key.
enumE checkifInputUniqueMap(std::string inputString, std::unordered_map<enumE, std::string> commands){};
For example if my inputString = "HEL" and my map = {{enumE::HELLO, "HELLO"},{enumE::WORLD, "WORLD"}} and enum class enumE{ HELLO,WORLD, NOTUNIQUE, NOTFOUND
};, it should return enumE::HELLO.
I tried to adapt the code above for the vector to suit this function, but I am not really getting anywhere.
Based on the description of your example, here is a quick implementation . Of course, the logic may not be entirely what you wrote, but I am sure you can tweak it.
std::string
has a std::string::find
function which will find a substring for you?Feel free to play around with that function and implement your logic.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.