English 中文(简体)
C++中的 std: list <T>:: begin () 返回类型是什么 C++
原标题:What s the return type of std::list<T>::begin() in C++

我想通过深入查看 lt; list> 执行过程,可以得出这一信息,这就是我为最后一天所做的。

我的问题是我想用我的 template< class T> (有私家 std:: list<T> 成员) 来包装方法, 但我无法找到返回值给这个方法的值 。

我的代码看起来是这样的:

template &lt;class T &gt;
    class MyTemplate {
       std::list&lt;T&gt; myList;
       ...

       somereturnvaluehere myMethod(){
              return myList.begin();       
       }
       ...

}
最佳回答

列表 s begin 将返回 std:: list<T>::terator

如果您是依附类型,那么 some backvalue here 应该是

typename std::list<T>::iterator
问题回答

要么是,要么是

  • typename std::list<T>::iterator, (non-const version)
  • or typename std::list<T>::const_iterator (const version)

取决于 myList const 对象还是不是 const 对象,尽管在您的情况中,它似乎不是 const 对象,这意味着您应该使用第一个版本,即非condt版本。

在 C++11 中, 您不需要担心它, 似乎您想要, 您可以使用尾随返回类型 :

auto myMethod() -> decltype(myList.begin())
{
    return myList.begin();       
}

但我认为,对于你来说,这太过分了,而且事实上,使事情变得不必要地比要求复杂。所以我建议你不要使用它;但只要知道在C++11中有一种叫做“它们”-返回型 的东西,在“它们”的某些案例中,使用这种东西是有道理的。

要么是,要么是

std::list<T>::iterator;

std::list<T>::const_iterator;

depending on whether your list is const 或not. You can use the type in your class like this:

struct Foo {
    typedef typename std::list<SomeType>::iterat或iterator;
    typedef typename std::list<SomeType>::const_iterat或const_iterator;

    iterat或foo() { return m_list.begin();}
    const_iterat或foo() const { return m_list.begin(); }

    std::list<SomeType> m_list;
};

std:: list<T> : begin () 会像其他人所说的那样, 返回一个 std:: list<T> :: list<T> :: const_titor

更重要的是 你试图从 myMethod () () 函数中提炼 什么?

如果您在 std:: list 周围只写一个薄包装纸,那么我不确定我是否看到这个值。 理想的情况是, 您会将列表嵌入您的类中, 以便不暴露其执行 。

正如其他人所说,它将是 < code>std:: list<T>: 编辑 。 但是,为了展示一个更清洁的 API, 并允许自己在将来有一定的灵活性来改变这一点, 您可能想要定义自己的类型def (该地图是 std:: list<T>:: 编辑 ), 并使用它作为返回类型 。 I. e. :

template <class T >
    class MyTemplate {
       std::list<T> myList;
       typedef std::list<T>::iterator iterator;
       ...

       iterator myMethod(){
              return myList.begin();       
       }
       ...

}

这里,我已经命名了类型 def just terator , 这意味着您的类中的用户会把它称为 MyTemplate<T>:: -- 但取决于 myMethod () () 和您类中的细节, 您可能想要给它命名更具体的东西 。

通过使用 typedef ,如果您后来改变心意并决定要使用 std:: victor 而不是 std:: list ,那么您的用户代码就不必更改(当然需要重新编译)。





相关问题
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?

热门标签