简体   繁体   English

C ++列表迭代器功能

[英]C++ List Iterator to Function

I'm trying to pass the iterator to a separate function to then do something with the element at that location in the list. 我试图将迭代器传递给单独的函数,然后对列表中该位置的元素进行处理。

This is not working for me. 这对我不起作用。

#include <list>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    void doSomething(iterator::<int> *it);

    list<int> intList;

    intList.push_back(10);
    intList.push_back(20);
    intList.push_back(10);
    intList.push_back(30);

    list<int>::iterator it;



    for (it = intList.begin(); it != intList.end(); it++)
    {
        if (*it == '10')
            doSomething(*it);
    };

    void doSomething(iterator <int> *it)
    {
        (*it) = 200;
    };
}

iterator isn't a standalone type in itself. iterator本身不是独立类型。 It's just a typedef (it could be more than that. But we may safely assume that for this question) 它只是一个typedef(可能不止于此。但是对于这个问题,我们可以放心地假设是这样)

list<int>::iterator is a separate type and so is vector<int>::iterator without any common class in hierarchy. list<int>::iterator是一个单独的类型, vector<int>::iterator也是如此,层次结构中没有任何公共类。 All functions that accept an iterator are typically templatized functions where the template type having the constraint to satisfy the requirements of iterator . 接受iterator所有函数通常都是模板化函数,其中模板类型具有满足迭代器要求的约束。

For your case you will have to declare doSomething as either: 对于您的情况,您将必须将doSomething声明为:

void doSomething(list::iterator <int> it);  // will only work with std::list iterators

Or the way most STL functions/containers accept iterators 或大多数STL函数/容器接受迭代器的方式

template<typename Iterator>
void doSomething(Iterator it); // generic. Will work with any container whose iterator has an overload for * operator

In either case at the caller side you can do 无论哪种情况,您都可以在呼叫方进行

for (it = intList.begin(); it != intList.end(); it++)
{
    if (*it == 10)
       doSomething(it);
};
  • itetator won't automatically mean iterator for std::list . itetator不会自动表示std::list迭代器。
  • You cannot define functions inside functions: the GCC extension is only available for C, not for C++. 您不能在函数内部定义函数:GCC扩展仅适用于C,不适用于C ++。
  • Match the types by using unary & operator (take address) and unary * operator (dereference). 使用一元&运算符(地址)和一元*运算符(取消引用)来匹配类型。

Corrected code (at least it compiles): 更正的代码(至少可以编译):

#include <list>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    void doSomething(list<int>::iterator *it);

    list<int> intList;

    intList.push_back(10);
    intList.push_back(20);
    intList.push_back(10);
    intList.push_back(30);

    list<int>::iterator it;

    for (it = intList.begin(); it != intList.end(); it++)
    {
        if (*it == '10')
            doSomething(&it);
    }

}

void doSomething(list<int>::iterator *it)
{
    *(*it) = 200;
}

To make this code better: 为了使此代码更好:

  • Declaring functions locally is not common. 在本地声明函数并不常见。
  • Using reference might be better. 使用引用可能会更好。
  • Using multi-character character constant looks weird. 使用多字符字符常量看起来很奇怪。 You may want simple integer 10 instead of '10' , which may be 12592 (0x3130). 您可能需要简单的整数10而不是'10' ,它可能是12592(0x3130)。 Note that value of multi-character character constant is implementation-defined. 注意,多字符字符常量的值是实现定义的。

Code that seems better: 看起来更好的代码:

#include <list>
#include <iostream>

using namespace std;

 void doSomething(list<int>::iterator &it);

int main(int argc, char *argv[])
{

    list<int> intList;

    intList.push_back(10);
    intList.push_back(20);
    intList.push_back(10);
    intList.push_back(30);

    list<int>::iterator it;

    for (it = intList.begin(); it != intList.end(); it++)
    {
        if (*it == 10)
            doSomething(it);
    }

}

void doSomething(list<int>::iterator &it)
{
    (*it) = 200;
}
#include <list>
#include <iostream>

using namespace std;

void doSomething(list<int>::iterator *it)
    {
        *(*it) = 200;
    }

int main(int argc, char *argv[])
{

    list<int> intList;

    intList.push_back(10);
    intList.push_back(20);
    intList.push_back(10);
    intList.push_back(30);

    list<int>::iterator it;



    for (it = intList.begin(); it != intList.end(); it++)
    {
        if (*it == '10')
            doSomething(&it);
    }   
}

Please make the doSomething function outside of main(). 请在main()之外创建doSomething函数。 Also your argument in the function is wrong. 同样,您在函数中的参数是错误的。 iterator serves as a typedef and its kinda pointer in some way. 迭代器以某种方式用作typedef及其种类的指针。 You pass a ppointer as an argument , you need to take the address of your pointer with this symbol '&'. 您将ppointer作为参数传递,您需要使用带有符号'&'的指针地址。

Best-ish case use: 最佳用例:

#include <list>

using namespace std;

template <typename T>
void doSomething(T it); /*Pre-condition: must be passed a dereferencible object.*/

int main(){
    list<int> intList;

    intList.push_back(10);
    intList.push_back(20);
    intList.push_back(10);
    intList.push_back(30);

    list<int>::iterator it;

    for (it = intList.begin(); it != intList.end(); it++){
        if (*it == 10) doSomething(it);
    }; 
}

template <typename T>
void doSomething(T it){
    (*it) = 200;
};

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

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