Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.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++_Algorithm_C++11_Containers - Fatal编程技术网

C++ 求容器中最大现值的算法

C++ 求容器中最大现值的算法,c++,algorithm,c++11,containers,C++,Algorithm,C++11,Containers,我有以下问题,我有一个向量std::set,现在我想计算元素,它在大多数集合中。 例如: 如果集合是{1,2,3,4},{2,4,5}和{2,7,8},我希望算法输出2,因为2在3个集合中,其他元素不是。 我目前解决这个问题的尝试是使用一个映射,它将一个计数器映射到集合中的每个值,然后在所有集合上迭代。 我确信我需要迭代所有集合,但是我可以使用标题中的一些算法来解决这个问题吗?使用来解决每个的解决方案: std::set<std::set<std::string>> se

我有以下问题,我有一个向量
std::set
,现在我想计算元素,它在大多数集合中。 例如: 如果集合是{1,2,3,4},{2,4,5}和{2,7,8},我希望算法输出2,因为2在3个集合中,其他元素不是。 我目前解决这个问题的尝试是使用一个映射,它将一个计数器映射到集合中的每个值,然后在所有集合上迭代。
我确信我需要迭代所有集合,但是我可以使用
标题中的一些算法来解决这个问题吗?

使用
来解决每个
的解决方案:

std::set<std::set<std::string>> sets {s1,s2,s3,s4}; // incurs a copy on each set
std::unordered_map<std::string, int> all;
std::for_each(sets.begin(), sets.end(), [&all](const std::set<std::string> &s) { // outer loop: each set in sets
    std::for_each(s.cbegin(), s.cend(), [&all](const std::string &string) { // nested loop
         all[string]++;
    });
});
for (const auto &p : all)
    std::cout << p.first << " = " << p.second << "\n";

并计算交叉点:

#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>

int main()
{
    std::set<int> s1{ 1, 2, 3, 4 };
    std::set<int> s2{ 2, 4, 5 };
    std::set<int> s3{ 2, 7, 8 };

    std::multiset<int> s4;

    std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
        std::insert_iterator<std::multiset<int>>(s4, s4.begin()));

    std::set_intersection(s4.begin(), s4.end(), s3.begin(), s3.end(),
        std::insert_iterator<std::multiset<int>>(s4, s4.begin()));

    auto max = std::max_element(s4.begin(), s4.end(),
        [&s4](int a, int b) { return s4.count(a) < s4.count(b); });

    std::cout << "most present == " << *max << '\n';
}
#包括
#包括
#包括
#包括
int main()
{
std::集s1{1,2,3,4};
std::集s2{2,4,5};
std::集s3{2,7,8};
std::多集s4;
std::set_交叉点(s1.begin()、s1.end()、s2.begin()、s2.end(),
std::insert_迭代器(s4,s4.begin());
std::set_交叉点(s4.begin()、s4.end()、s3.begin()、s3.end(),
std::insert_迭代器(s4,s4.begin());
auto max=std::max_元素(s4.begin(),s4.end(),
[&s4](inta,intb){返回s4.count(a)std::cout
std::set
已排序。因此以下代码可能会快一点

#include <iostream>
#include <set>
#include <vector>

typedef std::set<int> Data;
typedef std::vector<Data> DataSet;
typedef std::vector<int> Result;

Result findIntersection(const DataSet& sets) {
    Result is; // intersection
    std::vector<Data::iterator> its;
    for (int i = 0; i < sets.size(); ++i) {
        its.push_back(sets[i].begin());
    }

    if (its.size() == 0) return is;
    if (its.size() == 1) {
        // return sets[0];
        return is;
    }

    while(its[0] != sets[0].end()) {
        const int sentinel = *its[0];
        int counter = 1;
        for (int j = 1; j < its.size(); ++j) {
            while (*its[j] < sentinel && its[j] != sets[j].end()) ++its[j];
            if (its[j] == sets[j].end()) return is;
            if (*its[j] != sentinel) break;
            ++its[j];
            ++counter;
        }

        if (counter == its.size()) is.push_back(sentinel);
        ++its[0];
    }

    return is;
}

int main() {
    Data s1{ 1, 2, 3, 4 };
    Data s2{ 2, 4, 5 };
    Data s3{ 2, 4, 7, 8 };
    DataSet data = {s1, s2, s3};

    Result is = findIntersection(data);
    std::copy(is.begin(), is.end(), std::ostream_iterator<int>(std::cout, " "));
    return 0;
}
#包括
#包括
#包括
typedef std::设置数据;
向量数据集;
typedef std::向量结果;
结果findIntersection(常量数据集和集合){
结果是;//交集
std::向量its;
对于(int i=0;i
如果您将“手动”地图解决方案作为参考,那就太好了。@hyde为什么?猜测它的作用很容易。@even一方面,当现有模板包含正确的变量等时,编写答案代码要容易得多。另一方面,有相关代码的问题更容易获得投票(这有助于获得好的答案),因为代码。第三,它有助于证明OP真正理解他们在问什么,并且“完成了他们的家庭作业”在询问之前。对于大型问题实例,似乎建议始终将两组最低基数相交-在第一步之后,这意味着当前相交与尚未处理的最小集合相交。
#include <iostream>
#include <algorithm>
#include <set>
#include <iterator>

int main()
{
    std::set<int> s1{ 1, 2, 3, 4 };
    std::set<int> s2{ 2, 4, 5 };
    std::set<int> s3{ 2, 7, 8 };

    std::multiset<int> s4;

    std::set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(),
        std::insert_iterator<std::multiset<int>>(s4, s4.begin()));

    std::set_intersection(s4.begin(), s4.end(), s3.begin(), s3.end(),
        std::insert_iterator<std::multiset<int>>(s4, s4.begin()));

    auto max = std::max_element(s4.begin(), s4.end(),
        [&s4](int a, int b) { return s4.count(a) < s4.count(b); });

    std::cout << "most present == " << *max << '\n';
}
#include <iostream>
#include <set>
#include <vector>

typedef std::set<int> Data;
typedef std::vector<Data> DataSet;
typedef std::vector<int> Result;

Result findIntersection(const DataSet& sets) {
    Result is; // intersection
    std::vector<Data::iterator> its;
    for (int i = 0; i < sets.size(); ++i) {
        its.push_back(sets[i].begin());
    }

    if (its.size() == 0) return is;
    if (its.size() == 1) {
        // return sets[0];
        return is;
    }

    while(its[0] != sets[0].end()) {
        const int sentinel = *its[0];
        int counter = 1;
        for (int j = 1; j < its.size(); ++j) {
            while (*its[j] < sentinel && its[j] != sets[j].end()) ++its[j];
            if (its[j] == sets[j].end()) return is;
            if (*its[j] != sentinel) break;
            ++its[j];
            ++counter;
        }

        if (counter == its.size()) is.push_back(sentinel);
        ++its[0];
    }

    return is;
}

int main() {
    Data s1{ 1, 2, 3, 4 };
    Data s2{ 2, 4, 5 };
    Data s3{ 2, 4, 7, 8 };
    DataSet data = {s1, s2, s3};

    Result is = findIntersection(data);
    std::copy(is.begin(), is.end(), std::ostream_iterator<int>(std::cout, " "));
    return 0;
}