使用“查找”访问向量中的值

时间:2019-05-12 00:35:08

标签: c++ algorithm vector indexof

所以我一直在学习c ++,发现与其他语言相比,我非常生锈。我一直在codewars.com上解决这个问题

  

给出一个列表lst和一个数字N,创建一个新列表,该列表最多包含N次每个lst数字,而无需重新排序。例如,如果N = 2,并且输入为[1,2,3,1,2,1,2,3],则取[1,2,3,1,2],然后删除下一个[1,2 ],因为这将导致1和2在结果中出现3次,然后取3,从而得出[1,2,3,1,2,3]。

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

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

  • 遍历提供的数组(arr)
  • 检查“实例”中是否不存在“ arr”中的值
  • 如果找不到,则将“ arr”中的值推送到“ instances”,
  • 在“ countOfInstance”中添加一个与该索引相对应的计数值
  • ,然后将“ arr”中的值添加到nFilteredVector。

如果在“实例”中找到“ arr”中的值,则:

  • 在“实例”中找到“ arr”的索引值
  • 使用该索引在“ countOfInstances”中找到其对应的计数值
  • 确定计数是否小于提供的“ N”
  • 如果少于“ N”,则添加到“ nFilteredVector”
  • 然后增加“ countOfInstances”中的值

但是,当我尝试使用“ instances”索引访问“ CountOfInstances”索引时,出现了奇怪的错误

  

'std :: vector'类型没有可行的重载运算符[]         if(countOfInstances [std :: find(instances.begin(),instance.end(),arr [i])] <= 2){

如果我错了,请纠正我,但据我了解,find函数返回被找到元素的索引值。我想使用该索引值来访问“ countOfInstances”向量。

有人可以帮助我找出我所要查找的正确语法。将“实例”和“ 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;
            }

以下是一些代码战将要测试的示例

                {
                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})));
              }

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

如果您要达到的目的是在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);
   //...
}