English 中文(简体)
C++ 回收机
原标题:function returning iterator in C++
  • 时间:2012-01-11 16:10:29
  •  标签:
  • c++
  • iterator

下面是一条 Java法,将一台电梯归来。

vector<string> types;

// some code here

Iterator Union::types() 
{
    return types.iterator();
}

我想将这一法典翻译成C++。 如何从这种方法中恢复病媒的传导器?

问题回答

(a) 《公约》第22条:

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++.

您可修改<条码>: 类型(),以回归范围:

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++)
{
}

页: 1 方法:

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

std::vector<string>::const_iterator Union::begin()const
{
  return types.begin();
}

std::vector<string>::const_iterator Union::end()const
{
  return types.end();
}

假设这种类型是阶级联盟的属性,即符合《普通刑法》的规定,处理方式是:

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(); }
};

工会是其成员的一个集装箱。 我将使用<条码>begin和end,分别向第一和后继成员提供背机。

类型清单不属于海事组织,而是工会的主要不动产。 因此,我自己将使用以下文字,并为成员数据本身保留“密码”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();
}

遣返一个炉子是容易的。 例如,您可以返回病媒类型中的第一个探测器:

std::vector<std::string> types;

// some code here

std::vector<std::string>::iterator Union::returnTheBeginIterator() 
{
    return types.begin();
}

Java vs. C++

但C++的炉:不是 Java的炉:: 他们没有同样使用。

在 Java(IIRC),你更喜欢一个统计员,即你使用从一个项目到下一个项目的“延伸”方法。 因此,从一开始至尾,将 Java调器退回就足够了。

在C++,电梯设计成像超级点。 因此,通常使用操作人++的“点”——等等(取决于变压器的确切类型),你可以把自动取款人移至集装箱的“点”到下一个点、以前的点等值。

Let s iterate!

通常,你希望从一开始就结束。

因此,如果你希望只读一次,那么,你们要么要归还全部收藏品(作为“const”),让用户 it想他/她想要的方式。

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();
}

并且,从一开始至尾,在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 << ".
" ;
   }
}

C++11 version

如果你在C++11中提供完整的集装箱,而不是只提供集装箱,那么就更容易了,因为你可以使用距离(如 Java和C#的海滩):

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  << ".
" ;
   }
}

P.S.:Johannes Schaub - litb尽可能使用“const” 限定词。 我并不是因为我想避免淡化法典,而归根结底,“const”是你的朋友。

您可以做如下工作:

std::vector<std::string> types

std::vector<std::string>::iterator Union::types(){
    return types.begin();
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签