简体   繁体   English

将向量的迭代器发送到函数中。 (C ++)

[英]To send in iterators of vectors into a function. (C++)

This is more like a follow-up question to Is it possible to send part of vector as a vector to a function? 这更像是一个后续问题, 是否可以将向量的一部分作为向量发送给函数? but it's different and I couldn't ask it there. 但这是不同的,我不能在那里问。

I include the specific answer that is interesting in my case: 我提供了针对我的情况的有趣的具体答案:


Use iterators as the parameters to the range-based function, and pass in the required range. 将迭代器用作基于范围的函数的参数,并传递所需的范围。 Your code in the function becomes 您在函数中的代码变为

funcWithRange(v.cbegin()+10, v.cbegin()+50); funcWithRange(v.cbegin()+ 10,v.cbegin()+ 50); with function signature 具有功能签名

void funcWithRange(std::vector::const_iterator first, std::vector::const_iterator last) This could be generalized by making this a function template with the vector member type as its template parameter, or still further to any container supporting this type of range iteration. void funcWithRange(std :: vector :: const_iterator首先,std :: vector :: const_iterator最后)可以通过将其设为具有矢量成员类型作为其模板参数的函数模板,或者进一步扩展到支持该类型的任何容器,来推广这一点。范围迭代。 As noted in the comments, has many examples of this pattern. 如评论中所述,有很多这种模式的例子。

std::distance(first, last); std :: distance(first,last); will return the desired altered size. 将返回所需的更改大小。 I don't think you can get closer to meeting your requirements without making a physical copy. 我认为您不进行物理复制就无法满足您的要求。


This is all fine but I wonder about how to "use" the vector (my vector is called "numbers" in the new function. As my program look now I just use numbers[i], numbers[i + 1] and numbers.size(). I don't know how to do when I have two in parameters. I guess I can use first + ? or something but since it is a recursive function I would like to be able to treat is just as a vector. 一切都很好,但我想知道如何“使用”向量(在新函数中,我的向量称为“数字”。从我的程序看,现在我只使用数字[i],数字[i + 1]和数字。 size()。当我有两个in参数时,我不知道该怎么做,我想我可以使用first +?之类的东西,但是由于它是一个递归函数,我希望能够将其视为向量。

My second question is how do I write the function signature when I have using namespace std;? 我的第二个问题是使用命名空间std;时如何编写函数签名? Normally I just cut off everything before the :: and the :: but this time I see them twice in both first and last. 通常,我只截断::和::之前的所有内容,但是这次我在第一和最后两次都看到了它们。


I'm sorry about asking this here when it's so similar to other questions already answered. 我很抱歉在与已回答的其他问题如此相似时在这里提出此问题。 I first tried to send an e-mail to the person posting the answer and he recommended me to post it here. 我首先尝试向发帖人发送电子邮件,他建议我在此处发帖。

Update: 更新:

Thank you for answers still I can't really see how I can use this. 仍然感谢您的回答,我仍然看不到如何使用它。 Not because your answers weren't good but because I didn't describe it good enough. 不是因为您的回答不好,而是因为我对它的描述不够好。 Ishould also attach the code with the function: 我还应该在代码中附加以下功能:


bool deeperLook (vector<int> numbers, int target)
{
    for (int i = 0 ; i < numbers.size() ; i++)
    {
        if (numbers[i] == target)
        {
            return true;
        }
        if (numbers[i] < target)
        {
            deeperLook({numbers.cbegin() + i, numbers.cbegin() + numbers.size()} , target - numbers[i]);
        }
    while(numbers[i] == numbers[i+1])
    {
        i++;
    }
    }
    return false;
}

This function will get a sorted vector of int (biggest first) and then try to create a combination of the numbers that added together reach a specific number (target). 此函数将获取有序的int向量(首先是最大的),然后尝试创建相加的数字的组合以达到特定的数字(目标)。 bool deeperLook (vector numbers, int target) As you see, the function wants a vector as indata. bool deeperLook(向量数字,int目标)如您所见,该函数希望将向量作为indata。 That vector is sent to another part of the program and also, it's recursive so I need to send it a vector as indata. 该向量被发送到程序的另一部分,并且它是递归的,因此我需要将其作为数据输入。 How do I do this with iterators? 我该如何使用迭代器? The code works fine but it's memory inefficient to create all thoose since it's really just all part of the same vector. 该代码可以正常工作,但是创建所有thoose的内存效率很低,因为它实际上只是同一向量的所有部分。

someone also said that I should avoid "using namespace std;". 有人还说我应该避免“使用命名空间std;”。 Why? 为什么? It seems a lot easier to write code with it. 用它编写代码似乎容易得多。

You can use first[i] , operator[] works on random access iterators, and you can use last - first to get the 'size'. 您可以使用first[i]operator[]在随机访问迭代器上工作,也可以使用last - first获得“大小”。 Remember that iterator operations are based on pointers, so if you can do it with a pointer you can do it with a random access iterator. 请记住,迭代器操作是基于指针的,因此,如果可以使用指针进行操作,则可以使用随机访问迭代器进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM