English 中文(简体)
C++ STL: 如何在需要获得元素及其指数时使病媒变质?
原标题:C++ STL: How to iterate vector while requiring access to element and its index?

我经常发现,我要求转播STLmoss。 虽然我这样做,但我需要接触到elementindex

我这样做是为了:

typedef std::vector<Foo> FooVec;
typedef FooVec::iterator FooVecIter;

FooVec fooVec;
int index = 0;
for (FooVecIter i = fooVec.begin(); i != fooVec.end(); ++i, ++index)
{
    Foo& foo = *i;
    if (foo.somethingIsTrue()) // True for most elements
        std::cout << index << ": " << foo << std::endl;
}

在发现<BOOST_FOREACH后,我将其缩短如下:

typedef std::vector<Foo> FooVec;

FooVec fooVec;
int index = -1;
BOOST_FOREACH( Foo& foo, fooVec )
{
    ++index;
    if (foo.somethingIsTrue()) // True for most elements
        std::cout << index << ": " << foo << std::endl;
}

在需要提及病媒要素及其指数时,对STL矢量相比,是否有更好或更可取的方法?

我知道另一种选择:for (int i = 0; i < fooVec.size(); ++i 但是,我仍然想到的是,在类似STL集装箱的运输上,这不是一种良好做法。

问题回答
for (size_t i = 0; i < vec.size(); i++)
    elem = vec[i];

病媒比水泥阵列的包装薄;无论是使用电梯还是指数,其速度也一样快。 其他数据结构并非如此令人担心,例如:清单。

你们总是能够把指数归为:

std::size_t index = std::distance(fooVec.begin(), i);

就病媒而言,这很有可能作为单一点子减员操作加以实施,因此其成本并不特别高。

产权属于持有人的范畴,然而,确实记得点人/点人对称:

for (FooVecIter i = fooVec.begin(); i != fooVec.end(); ++i)
{
    Foo& foo = *i;
    if (foo.somethingIsTrue()) // True for most elements
        std::cout << i - fooVec.begin() << ": " << foo << std::endl;
}

与距离方法相比,越权越权越权越权越权越权越权越权越权越权越权越权越权越权越权越权越权越权越权越高。

关于具体问题:

Is there a better or more elegant way to iterate over STL vectors
when both reference to the vector element and its index is required?

IMHO,

for (size_t i = 0; i < fooVec.size(); ++i) {
    Foo & foo = fooVec[i];        // if  foo  is to be modified
    Foo const& foo = fooVec[i];   // if  foo  is to be NOT modified
}

这是最简单和最棘手的解决办法。 不需要使用自动处理器的问题。





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

热门标签