使用 "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)
- 检查 "arr" 中的值是否在 "instances"
中不存在
- 如果找不到则将"arr"中的值推送到"instances",
- 在"countOfInstance"
中添加对应该索引的计数值1
- 然后将"arr"中的值添加到nFilteredVector。
如果 "arr" 中的值在 "instances" 中找到,则:
- 在"instances"
中找到"arr"的索引值
- 利用这个索引在"countOfInstances"
中找到它对应的计数值
- 判断计数是否小于提供的"N"
- 如果小于 "N" 添加到 "nFilteredVector"
- 然后增加 "countOfInstances"
中的值
但是,当我尝试使用 "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);
//...
}
所以我复习了一下 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)
- 检查 "arr" 中的值是否在 "instances" 中不存在
- 如果找不到则将"arr"中的值推送到"instances",
- 在"countOfInstance" 中添加对应该索引的计数值1
- 然后将"arr"中的值添加到nFilteredVector。
如果 "arr" 中的值在 "instances" 中找到,则:
- 在"instances" 中找到"arr"的索引值
- 利用这个索引在"countOfInstances" 中找到它对应的计数值
- 判断计数是否小于提供的"N"
- 如果小于 "N" 添加到 "nFilteredVector"
- 然后增加 "countOfInstances" 中的值
但是,当我尝试使用 "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);
//...
}