[英]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
迭代器。 &
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: 为了使此代码更好:
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.