简体   繁体   English

制作一个在唯一时自动检测的 function

[英]Make a function that autodetects when unique

I need to have a function in c++ that gets an element from a vector of strings as soon as the input is unique.我需要在 c++ 中有一个 function ,只要输入是唯一的,它就会从字符串向量中获取一个元素。

For example, my vector contains {"DELETE", "HELP", "GO FORWARD", "GO BACKWARDS"} .例如,我的向量包含{"DELETE", "HELP", "GO FORWARD", "GO BACKWARDS"}

If my input is then "H" , the function should output "HELP" If the input is "H 55" , it should ignore the 55 and still output "HELP" .如果我的输入是"H" ,那么 function 应该是 output "HELP"如果输入是"H 55" ,它应该忽略 55 并且仍然是 Z78E6221F6393D13566Z "HELP"

However, when I add a new element "HELLO" to my vector, it should return "NOT UNIQUE" .但是,当我在向量中添加一个新元素"HELLO"时,它应该返回"NOT UNIQUE"

I already tried a bunch of things, but I am not really getting anywhere.我已经尝试了很多东西,但我并没有真正取得任何进展。

I think a 2 dimensional vector would be a possible solution, but I have not been able to make it work:我认为二维向量将是一个可能的解决方案,但我无法使其工作:

["DELETE", "D"]
["DELETE", "DE"]
["DELETE", "DEL"]
["DELETE", "DELE"]
["DELETE", "DELET"]
["DELETE", "DELETE"]
["HELP", "HELP"]
["HELLO", "HELL"]
["HELLO", "HELLO"]
...

With a (custom) Trie , you might do something like:使用(自定义) Trie ,您可能会执行以下操作:

class Node
{
public:
    std::map<char, Node> children; // Might be a std::array<std::unique_ptr<Node>, 26>
    bool end_of_word = false;
    std::size_t count = 0; // number of words from that point

    Node() = default;

    const Node* get(char c) const
    {
        auto it = children.find(c);

        if (it == children.end()) {
            return nullptr;   
        }
        return &it->second;
    }
};

class Trie
{
    Node root;
public:
 
    void add(const std::string& word)
    {
        Node* node = &root;
        for (const char c : word) {
            node->count++;
            node = &node->children[c];
        }
        node->count++;
        node->end_of_word = true;
    }

    bool contains(const std::string& word) const
    {
        const Node* node = &root;
        for (const auto& c : word) {
            node = node->get(c);
            if (node == nullptr) {
                return false;
            }
        }
        return node->end_of_word;
    }
    
    std::optional<std::string> word_from_prefix(const std::string& s)
    {
        std::string res;
        const Node* node = &root;
        for (const auto& c : s) {
            auto next = node->get(c);
            if (next == nullptr) {
                break;
            }
            res += c;
            node = next;
        }
        if (node->count != 1) return std::nullopt;
        while (!node->end_of_word) {
            auto& [c, next] = *node->children.begin();
            res += c;
            node = &next;
        }
        return res;
    }
};

Demo演示

Make a function that autodetects when unique制作一个在唯一时自动检测的 function

One possible way of doing this is as shown below:一种可能的方法如下所示:

#include <iostream>
#include <vector>
#include <string>
std::string checkUnique(const std::string& searchString, const std::vector<std::string>& inputVector)
{
    int count = 0;
    std::string foundString;
    for(const std::string &element: inputVector)
    {
        if(element.at(0) == searchString.at(0))
        {
            foundString = element;
            ++count; //increment count
        }
    }
    if(count == 0)
    {
        return "NOT FOUND";
    }
    else if(count == 1)
    {
        return foundString;
    }
    else 
    {
        return "NOT UNIQUE";
    }
}
int main()
{
  
    std::vector<std::string> inputVector{"DELETE", "HELP", "GO FORWARD", "GO BACKWARDS"};
    
    std::string searchString = "H";
    
    //call the function for different cases 
    
    std::cout << checkUnique(searchString, inputVector) <<std::endl;;//prints HELP
    
    std::cout << checkUnique("H", inputVector) << std::endl;  //prints HELP
    
    std::cout << checkUnique("H 55", inputVector) << std::endl; //prints HELP 
    
    //add "HELLO" into the vector 
    inputVector.push_back("HELLO");
    
    std::cout << checkUnique("H", inputVector) << std::endl;  //prints NOT UNIQUE 

    std::cout << checkUnique("H 55", inputVector) << std::endl;  //prints NOT UNIQUE
    return 0;
}

The output of the program can be seen here .程序的output可以看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 进行自动选择 function 以在输入唯一时获取向量中的元素 - Make an autoselect function that gets element in vector when input is unique 调用函数时,unique_lock是否已解锁? - Is unique_lock unlocked when a function is called? 如何使用std :: make_unique函数和接口类? - How to work with std::make_unique function and interface classes? 将 std::make_unique 与 GetProfileBinary function 调用一起使用 - Using std::make_unique with the GetProfileBinary function call C ++将已删除函数与unique_ptr,make_unique一起使用 - C++ use of deleted function with unique_ptr, make_unique 没有重载函数“ std :: make_unique”的实例与参数列表匹配,但可与unique_ptr构造函数一起使用 - no instance of overloaded function “std::make_unique” matches the argument list, but works with unique_ptr constructor 调用 std::make_unique 时出现分段错误 - Segmentation fault when calling std::make_unique 分配数组时是否可以将 arguments 传递给 std::make_unique() ? - Is it possible to pass arguments to std::make_unique() when allocating an array? std :: make_unique可以将函数的输出作为参数吗? - Can std::make_unique take the output of a function as an argument? 有没有办法将指定不同类的 std::make_unique 传递给函数 - Is there a way to pass std::make_unique with different classes specified into a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM