C++中的函数返回迭代器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8822576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
function returning iterator in C++
提问by sufyan siddique
Following is a Java method that returns an iterator
以下是返回迭代器的 Java 方法
vector<string> types;
// some code here
Iterator Union::types()
{
return types.iterator();
}
I want to translate this code to C++. How can i return an iterator of vector from this method?
我想将此代码转换为 C++。如何从此方法返回向量的迭代器?
回答by hmjd
This will return an iterator to the beginning of types
:
这将返回一个迭代器到开头types
:
std::vector<string>::iterator Union::types()
{
return types.begin();
}
However, the caller needs to know the end()
of vector types
as well.
Java's Iterator
has a method hasNext()
: this does not exist in C++.
但是,调用者也需要知道end()
向量的types
。JavaIterator
有一个方法hasNext()
:这在 C++ 中不存在。
You could change Union::types()
to return a range:
您可以更改Union::types()
以返回一个范围:
std::pair<std::vector<std::string>::iterator,
std::vector<std::string>::iterator> Union::types()
{
return std::make_pair(types.begin(), types.end());
}
std::pair<std::vector<std::string>::iterator,
std::vector<std::string>::iterator> p = Union::types();
for (; p.first != p.second; p.first++)
{
}
回答by Sean
You'll want to have a begin
and end
method:
你会想要一个begin
andend
方法:
std::vector<string>::iterator Union::begin()
{
return types.begin();
}
std::vector<string>::iterator Union::end()
{
return types.end();
}
For completeness you might also want to have const
versions
为了完整起见,您可能还需要const
版本
std::vector<string>::const_iterator Union::begin()const
{
return types.begin();
}
std::vector<string>::const_iterator Union::end()const
{
return types.end();
}
回答by ravenspoint
Assuming that types is an attribute of the class Union, a nice, STL compliant, way to handle this is:
假设 types 是类 Union 的一个属性,一个很好的、符合 STL 的处理方法是:
class Union
{
std::vector< std::string > types
public:
typedef std::vector< std::string >::iterator iterator;
iterator begin() { return types.begin(); }
iterator end() { return types.end(); }
};
回答by Johannes Schaub - litb
An union is a container of its members. I would use begin
and end
to give back iterators to the first and after-the-last members, respectively.
工会是其成员的容器。我将使用begin
和end
分别将迭代器返回给第一个和最后一个成员。
The list of types is not IMO the primary iterable property of an union. So I would myself use the following, and reserve the plain begin
and end
for the member data itself.
类型列表不是 IMO 联合的主要可迭代属性。所以我会自己使用以下内容,并保留普通的begin
和end
成员数据本身。
std::vector<string>::const_iterator Union::types_begin() const {
return types.begin();
}
std::vector<string>::const_iterator Union::types_end() const {
return types.end();
}
回答by paercebal
Returning an iterator is easy. For example, you can return the first iterator in the vector types:
返回迭代器很容易。例如,您可以返回向量类型中的第一个迭代器:
std::vector<std::string> types;
// some code here
std::vector<std::string>::iterator Union::returnTheBeginIterator()
{
return types.begin();
}
Java vs. C++
Java 与 C++
But C++ iterators are not Java iterators: They are not used the same way.
但是 C++ 迭代器不是 Java 迭代器:它们的使用方式不同。
In Java (IIRC), you have more like an enumerator, that is, you use the method "next" to iterate from one item to the next. Thus, returning the Java iterator is enough to iterate from the begining to the end.
在 Java (IIRC) 中,您更像是一个枚举器,也就是说,您使用“next”方法从一个项目迭代到下一个项目。因此,返回 Java 迭代器就足以从头到尾进行迭代。
In C++, the iterator is designed to behave like a super-pointer. Thus, it usually "points" to the value, and using the operator ++, --, etc. (depending on the exact type of the iterator), you can move the iterator to "point" to the next, previous, etc. value in the container.
在 C++ 中,迭代器被设计为表现得像一个超级指针。因此,它通常“指向”该值,并使用运算符 ++、-- 等(取决于迭代器的确切类型),您可以将迭代器移动到“指向”下一个、上一个等. 容器中的值。
Let's iterate!
让我们迭代!
Usually, you want to iterate from the beginning to the end.
通常,您希望从头到尾进行迭代。
This, you need to return either the whole collection (as "const", if you want it to be readonly), and let the user iterate the way he/she wants.
这,您需要返回整个集合(作为“const”,如果您希望它是只读的),并让用户按照他/她想要的方式进行迭代。
Or you can return two iterators, one for the beginning, and one for the end. So you could have:
或者你可以返回两个迭代器,一个用于开始,一个用于结束。所以你可以有:
std::vector<std::string>::iterator Union::typesBegin()
{
return types.begin();
}
std::vector<std::string>::iterator Union::typesEnd()
{
return types.end();
}
And the, you can iterate from the beginning to the end, in C++03:
并且,您可以在 C++03 中从头到尾进行迭代:
// alias, because the full declaration is too long
typedef std::vector<std::string> VecStr ;
void foo(Union & p_union)
{
VecStr::iterator it = p_union.typesBegin() ;
VecStr::iterator itEnd = p_union.typesEnd() ;
for(; it != itEnd; ++it)
{
// here, "*it" is the current string item
std::cout << "The current value is " << *it << ".\n" ;
}
}
C++11 version
C++11版本
If you provide the full container instead of only its iterators, in C++11, it becomes easier, as you can use the range-for loop (as the foreach in Java and C#):
如果您提供完整的容器,而不仅仅是它的迭代器,在 C++11 中,它会变得更容易,因为您可以使用 range-for 循环(作为 Java 和 C# 中的 foreach):
void foo(std::vector<std::string> & p_types)
{
for(std::string & item : p_types)
{
// here, "item " is the current string item
std::cout << "The current value is " << item << ".\n" ;
}
}
P.S.: Johannes Schaub - litbis right in using the "const" qualifier whenever possible. I did not because I wanted to avoid to dilute the code, but in the end, "const" is your friend.
PS:Johannes Schaub - litb在可能的情况下使用“const”限定符是正确的。我没有因为我想避免稀释代码,但最终,“const”是你的朋友。
回答by Chethan Ravindranath
You can do it as below
你可以做如下
std::vector<std::string> types
std::vector<std::string>::iterator Union::types(){
return types.begin();
}