Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在C++;_C++_Arrays_String_Set_Containers - Fatal编程技术网

C++ 如何在C++;

C++ 如何在C++;,c++,arrays,string,set,containers,C++,Arrays,String,Set,Containers,我有两个单独的单词数组,例如: array1 = word1, word2, word3 array2 = word4, word5, word6 我试图根据用户输入(2个单词)匹配这两个数组。 例如,您输入“word1 word6”,程序会给您x。你输入“word3 word4”,程序会给你y。在每个数组中不需要/不应该匹配(因此输入“word1 word3”不应该给出任何错误) 现在,我正在考虑使用string::find查找输入字符串中每个数组的内容。然而,在那之后,我一直在思考如何获取

我有两个单独的单词数组,例如:

array1 = word1, word2, word3
array2 = word4, word5, word6
我试图根据用户输入(2个单词)匹配这两个数组。 例如,您输入“word1 word6”,程序会给您x。你输入“word3 word4”,程序会给你y。在每个数组中不需要/不应该匹配(因此输入“word1 word3”不应该给出任何错误)

现在,我正在考虑使用
string::find
查找输入字符串中每个数组的内容。然而,在那之后,我一直在思考如何获取这些结果(如果有的话)并将它们相互匹配

例如,我将
input.find(array1的内容)
,如果找到了某个内容,则使用该
array1[x]
,查看通过同一输入中的单独一行找到的与
array2[x]
的组合是否与第三个可能组合列表匹配。如果是这样,我会根据它是哪个组合来分割响应

我知道,如果我有一个可能匹配的列表,并在输入字符串中找到它,会更容易。但是我想把这两组单词分开,因为代码会更灵活(这样我会学到更多)


希望有人能给我一些关于如何继续的建议

C++针对这类问题有一种特殊的结构,称为“映射”

要查看是否存在特定对,可以使用
map::find

MyMapType::iterator i = my_map.find(std::make_pair(x, y));
if (i == my_map.end()) {
    std::cout << "Pair is not defined\n";
} else {
    // Pair is present
    std::cout << "Associated value is " << *i << "\n";
}
MyMapType::iterator i=my_-map.find(std::make_-pair(x,y));
if(i==my_map.end()){
根据我的理解:
  • 你有两套单词
  • 来自用户的2个单词和
  • 您想知道这两个单词是否包含在这些集合中,但不是来自同一集合
然后你可以做一些类似的事情:

inline const bool isIn(const std::set<std::word>& s, const std::string& e) {
    return s.find(e) != s.end();
}

...

std::set<std::string> wordSet1, wordSet2;
std::string word1, word2; // <-- from the user
...
if (isIn(wordSet1, word1) && isIn(wordSet2, word2)) {
    // success
}
else if (isIn(wordSet2, word1) && isIn(wordSet1, word2) {
    // success
}
else {
    // fail
}
inline const bool isIn(const std::set&s,const std::string&e){
返回s.find(e)!=s.end();
}
...
std::set wordSet1,wordSet2;
std::string word1,word2;//
和所有可能的组合可能是更合理的方法,但因为您写道:“我知道如果我有一个可能匹配的列表会更容易……但我想将两组单词分开”,这可能不是您想要的


我希望这能有所帮助。

以您喜欢的方式存储您的单词,并将可搜索的组合放入bloom过滤器中

最一般形式的伪代码…:

插入:

搜索:

关于bloom过滤器的一些注意事项:

  • 查东西很快
  • 使用一个好的,快速的散列函数。互联网上有很多
  • 它可以生成假阳性(X在过滤器中,而实际上不在过滤器中)
  • 它不能生成假阴性(X不在过滤器中)
  • 当bloom过滤器表示过滤器中存在某些内容时,您必须查看原始源数据以确保它确实存在

  • 我将这种方法用于一个应用程序防火墙,该防火墙是为了防止色情和相关垃圾进入网络而构建的,它将特定代码的速度提高了400倍,而不是存储在传统的地图或哈希表中。

    获取公共元素不是最简单的选择。不过,您确实需要分类输入

      int first[] = {5,10,15,20,25};
      int second[] = {50,40,30,20,10};
    
      it=std::set_intersection (first, first+5, second, second+5, v.begin());
    

    将生成一个包含20个元素的向量:10和20。(根据链接)。

    我不明白当你说“例如,你输入了”word1 word6“,程序会给你x”时,“x”是什么意思。下一句的问题也是一样:)你是指不带符号的短单词吗?对不起,我指的是一般结果-在这种情况下,我可能会根据用户输入的两组单词执行一些函数。首先你写了“数组”,然后写了关于创建临时“列表”,最后写了“两组单词”注意,这是3种不同类型的容器。实际上,我对数组和向量之间的差异有点困惑。我所需要的只是单词的“列表”(字典定义,而不是C++类型:P)。我会用向量来表示这个,但这个启发了我这个“练习”的朋友。提到数组,所以我觉得也有可能。真的很抱歉,因为我说我是新手,所以我可能使用C++术语,没有意义!+ 1,因为基于OP的额外评论,这实际上是他所寻找的。
    inline const bool isIn(const std::set<std::word>& s, const std::string& e) {
        return s.find(e) != s.end();
    }
    
    ...
    
    std::set<std::string> wordSet1, wordSet2;
    std::string word1, word2; // <-- from the user
    ...
    if (isIn(wordSet1, word1) && isIn(wordSet2, word2)) {
        // success
    }
    else if (isIn(wordSet2, word1) && isIn(wordSet1, word2) {
        // success
    }
    else {
        // fail
    }
    
    for words in wordArray:
        bloomFilter.add( words.hash() )
    
    found = false
    if bloomFilter.contains( searchedForWords.hash() ):
        if originalWordList.contains( words )
            found = true
    
      int first[] = {5,10,15,20,25};
      int second[] = {50,40,30,20,10};
    
      it=std::set_intersection (first, first+5, second, second+5, v.begin());