English 中文(简体)
C++中的每一算法
原标题:foreach algorithm in C++
  • 时间:2009-11-25 20:28:24
  •  标签:
  • c++

是否有办法从我所穿行的职能中获得回报价值。

For ex: I have,

void myfunction (int i) 
{
        cout << " " << i;
}

vector<int> myvector;
myvector.push_back(10);
for_each (myvector.begin(), myvector.end(), myfunction);

让我说,我想用某种规则来计算病媒中的内容数目,我希望从我的职能中得到回报,这是可能的吗?

最佳回答

页: 1 但您可以制定<条码>。 a functor, 将其带往某些记忆中,并通过该点人储存你的回报价值。

struct MyFunctor {
    int *count;
    MyFunctor(int *count_) : count(count_) { }
    void operator()(int n) {
        if (n > 5) (*count)++;
    }
};

int main() {
    vector<int> vec;
    for (int i=0; i<10; i++) vec.push_back(i);
    int count = 0;
    for_each(vec.begin(), vec.end(), Myfunctor(&count));
    printf("%d
", count);
    return 0;
}

Edit: 正如评论所指出的,我的第一个例子将失败为for_each。 本来会提供<代码>MyFunctor的复印件,这样我们就能够从原物体中收回收益价值。 我沿用了最初做法的思路,但你确实应当看看全球机制的解决办法,这种解决办法更具有代表性。 我并不肯定可运输性,但它确实在我的<代码>gcc(4.4.2)上工作。 正如其他人提到的那样,尽可能使用<代码>和>;algorithm>。

问题回答

有一种特殊用途<代码>std:count (单位:千美元) (当假定回报真实时计算) Don t abuses std:for_each for what it was not used for.

for_each,将交送你通过的一名校长的复印件。 这意味着你可以这样做:

template <typename T>
class has_value
{
    has_value(const T& pValue) : mValue(pValue), mFlag(false) {}

    void operator()(const T& pX)
    {
        if (pX == mValue)
            mFlag = true;
    }

    operator bool(void) const { return mFlag; }
private:
    T mValue;
    bool mFlag;
};

bool has_seven = std::for_each(myvector.begin(), myvector.end(), has_value<int>(7));

例如。 但是,在计票和计票方面,请登录algorithm,并看您的职能是否已经建立。 (Like count

如果你想要强食,则有BOOST_FOREACH makro。 大部分的助推器是图书馆的主人,因此,你只能把“快乐”提升到你的项目中。 例:

BOOST_FOREACH( int & i , my_vector )
{
     i = 0;
}

我的导师可使用<条码>查询和查询;int>或int[]或任何其他种类的探测器。

Isn t this what functors ?

<代码>std:for_each 用于此目的。 使用<代码>std:count,以计算相当于一定价值的元素数目,或std:count_if,以计算具有某些前提的要素:

std::vector<SomeType> vec;
std::count(vec.begin(), vec.end(), SomeType(9));
/*or */
bool myfunc(const SomeType& v)
{
    return v == 9;
}
std::count_if(vec.begin(), vec.end(), f);

如果你只想将集装箱内容复制到像<编码>这样的 o子:cout,则使用:

std::vector<SomeType> vec;
...
std::copy(vec.begin(), vec.end(), 
    std::ostream_iterator<SomeType>(std::cout," "));

如果您需要每项援引职能的回报价值,则使用<代码>std:transform:

std::vector<SomeType> src;
std::vector<SomeType> result;
int myfunc(int val)
{
    ...
}
std::transform(src.begin(), src.end() 
    result.begin(), myfunc);

<代码>std:transform也超负荷运行,用于双功能和零功能。

奥凯,我担心,在你处理计票问题时,你选择过错的榜样。

问题在于for_each是极为笼统的,而且对于具体实施而言,已经有了更为具体的算法(count/code>,accumulate,transform,......)

因此,请举另一个例子:for_each通常用于对其所处理物体实施突变操作。 这并不妨碍你收集统计数字。

虽然for_each 确实交还了Predicate的标的,但没有任何保证在标的每个项目上都使用该标的。 执行可以免费复制前后的定点,并部分使用封面复印件......这样,你最终返回的复印件就可以在批号上。

class Predicate
{
public:
  Predicate(size_t& errors) : m_errors(errors) {}
  void operator()(MyObject& o)
  {
    try { /* complicated */ } catch(unfit&) { ++m_errors; }
  }
private:
  size_t& m_errors;
};

std::vector<MyObject> myVec;
// fill myVec

size_t errors = 0;
std::for_each(myVec.begin(), myVec.end(), Predicate(errors));

这里的陷阱是,所有原始基流的复印件都将标明相同的<代码>size_t变量,从而对这一变量进行了正确的更新。

http://www.ohchr.org。

但更好的解决办法是使用正确的算法。

You should be able to use std::count or std::count_if, or perhaps std::accumulate. These allow you to return one result for processing the entire sequence.

另选std:transform 允许您按顺序交回每个要素的结果,从而形成包含结果的新产出顺序。





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

热门标签