使用 "find" 访问向量中的值

Using "find" to access a value in vector

所以我复习了一下 C++,发现与我的其他语言相比我很生疏。我从 codewars.com

开始研究这个问题

Given a list lst and a number N, create a new list that contains each number of lst at most N times without reordering. For example if N = 2, and the input is [1,2,3,1,2,1,2,3], you take [1,2,3,1,2], drop the next [1,2] since this would lead to 1 and 2 being in the result 3 times, and then take 3, which leads to [1,2,3,1,2,3].

为完成此任务,我想创建一个多维向量以在第一维中保存所提供列表的唯一值,并在第二维中保存相应的出现次数。但是,我不熟悉 c++ 的语法来实现这一点,所以我只制作了 2 个单独的向量。(instances, countOfInstance)

基本上我的算法将做的是:

如果 "arr" 中的值在 "instances" 中找到,则:

但是,当我尝试使用 "instances" 的索引访问 "CountOfInstances" 的索引时,我得到一个奇怪的错误

no viable overloaded operator[] for type 'std::vector' if (countOfInstances[std::find(instances.begin(), instances.end(),arr[i])] <=2){

如果我错了请纠正我,但我的理解是 find 函数 returns 找到的元素的索引值。我想使用该索引值来访问 "countOfInstances" 向量。

谁能帮我找出我要找的东西的正确语法。将 "instances" 和 "countOfInstance" 集成为多维向量的奖励积分!!

#include <algorithm> 
            std::vector<int> deleteNth(std::vector<int> arr, int n)
            {
              std::vector<int> nFilteredVector;
              std::vector<int> instances;
              std::vector<int> countOfInstances;

              for (int i =0; i < arr.size();i++){
                if(std::find(instances.begin(), instances.end(),arr[i])==instances.end()){//value not found need to add corresponding value to instances vector then add an element of 1 to the correpeonding index of the countOfInstance vector.
                  instances.push_back(arr[i]);
                  countOfInstances.push_back(1);
                  nFilteredVector.push_back(arr[i]);
                }else{ // value is found just need to increment the value in countOfInstances
                  //find the instance of the value in arr in the instance vector, use that value to find the corresponding value in countOfInstance
                  if (countOfInstances[std::find(instances.begin(), instances.end(),arr[i])] <=n){
                    nFilteredVector.push_back(arr[i]);      
                  }
                  countOfInstances[std::find(instances.begin(), instances.end(),arr[i])]++;
              }



              return nFilteredVector;
            }

以下是 codewars 将要测试的内容的一些示例

                {
                Assert::That(deleteNth({20,37,20,21}, 1), Equals(std::vector<int>({20, 37, 21})));
                Assert::That(deleteNth({1,1,3,3,7,2,2,2,2}, 3), Equals(std::vector<int>({1, 1, 3, 3, 7, 2, 2, 2})));
              }

我相信 std::find return instances 上的迭代器。您不能将一个列表中的迭代器用于另一个列表,也不能将迭代器用作索引。

你可以做的是使用 std::find(instances.begin(), instances.end(), arr[i]) - instances.begin() 作为你的索引。这有点难看,所以您可能还想多看看迭代器以及如何使用它们。

如果您要实现的是在 std::vector 中获取找到的项目的索引,则以下使用 std::distance 完成此工作:

#include <algorithm>
#include <vector>

auto iter = std::find(instances.begin(), instances.end(),arr[i]);
if ( iter != instances.end())
{
   // get the index of the found item
   auto index = std::distance(instances.begin(), iter);
   //...
}