【问题标题】:c++ return all of the elements that appear n times in a vector as a vectorc ++将向量中出现n次的所有元素作为向量返回
【发布时间】:2018-08-27 01:52:01
【问题描述】:

目标:返回向量A中出现N次的所有元素,并将结果放入向量B中。

预期结果:

--Begin With---

Vector A=(10,20,30,30,40,50,100,50,20,100,10,10,200,300)

Do some code to return the name of elements that appear in Vector A
when N=3

Result should be Vector B=(10) //because only 10 is in vector A N=3 times.

我的尝试: 我得到了放置在另一个向量中的所有元素的计数,但我没有可以返回所有出现 N 次的元素的部分。 如果这意味着速度的提高,我可以非常灵活地处理它

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>     // std::back_inserter
#include <algorithm>    // std::copy

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };

    std::vector<std::pair<int, int> > shows;
    int target1;
    int num_items1;
    int size = static_cast<int>(v.size());

    for(int x=0; x<size; x++)
    {
        target1 = v[x];

        num_items1 = std::count(v.begin(), v.end(), target1);

        shows.push_back(std::make_pair(target1, num_items1));

        std::cout << "number: " << target1 << " count: " << num_items1 << '\n';
    } 
}

接受的问题解决方案

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <iterator>     // std::back_inserter
#include <algorithm>    // std::copy
#include <set>
#include <map>

using namespace std;

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };

    std::vector<int> shows;
    std::map<int, int> freqOfv;

    for(const auto& i: v)
    {
        freqOfv[i]++;
    }

    std::set<int> s(v.begin(), v.end());

    int N = 2; //Can be read from stdin as well...

    for ( auto it = s.begin(); it != s.end(); it++ )

    {
        if(freqOfv[*it] ==N)
        {

            shows.push_back(*it);
        }
    }

    for (std::vector<int>::const_iterator i = shows.begin(); i != shows.end(); ++i)
    {
        std::cout << *i << ' ';
    }
    return 0;
    }

【问题讨论】:

  • 您可能会发现std::map&lt;int, int&gt; freq; 在此处用于跟踪计数。每次找到一个元素,freq[v[x]]++;
  • 投票结束,因为过于广泛。
  • 堆栈溢出不是让人们为您编写代码的地方。尝试编写代码以存储/返回类型,如果遇到问题,请发布包含问题的代码并说出问题所在。

标签: c++ vector element counting find-occurrences


【解决方案1】:

正如 cmets 中所建议的,std::map 将简化代码:

int main()
{
    std::vector<int> v{ 1, 2, 3, 4, 4, 3, 7, 8, 9, 10 };    
    std::map<int, int> freqOfv; 
    for(const auto& i: v)
        freqOfv[i]++;

    int N = 2; //Can be read from stdin as well...

    for(const auto& i: freqOfv)
    {
        if(N == i.second)
            std::cout << "The value " << i.first << " occurs " << N << " times." << std::endl;
    }   
}

这会产生以下输出:

The value 3 occurs 2 times.
The value 4 occurs 2 times.

当然,您需要在开始时使用#include &lt;map&gt; 才能在代码中使用地图。

【讨论】:

  • 现在正在尝试代码...是否可以将所有值一次转储到数组/向量/映射或其他任何内容中,而无需循环进入某些内容?我必须为 1000 个循环完成此操作,并且通过循环存储返回值比使用 python numpy bincount 一次将所有结果返回到数组的我要慢得多。有任何想法吗?谢谢。
  • 您的意思是要避免第二个for 循环?
  • @p-w 谢谢,是的,我想避免这个循环 for(const auto& i: freqOfv) { if(N == i.second) std::cout
  • 从向量中,创建一个包含向量唯一元素的集合。然后在 for 循环(您在问题中发布的代码)中,使用 set 元素进行迭代,如果计数等于 N 并将该对添加到 shows 向量。
  • @p-w 感谢您的帮助!我将编辑我的原始问题以包含答案。
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 2019-05-28
  • 2010-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多