C++ 为避免原始循环,请为每个

C++ 为避免原始循环,请为每个,c++,stl,C++,Stl,我试图用STL算法C++11重写我的一些代码,但我被“无原始循环”规则卡住了 我的一个函数中有一段代码: for (size_t i = 0; i < cars->size(); i++) { if (cars->at(i).getNumber() == nr) { throw RepositoryException ("Product with the same number already exists"); }

我试图用STL算法C++11重写我的一些代码,但我被“无原始循环”规则卡住了

我的一个函数中有一段代码:

for (size_t i = 0; i < cars->size(); i++) {
        if (cars->at(i).getNumber() == nr) {
               throw RepositoryException ("Product with the same number already exists");
        }
    }
汽车是矢量型的

nr是作为参数得到的int

这个for循环实际上只做了一些有效性,整个函数做了一些其他的事情,所以我的问题是,有没有一个很好的方法用一些STL算法来替换这个循环?对于_,每个函数似乎都是一种尝试,但我不知道如何使用它,因为我无法仅为这个特定的有效性创建另一个函数

我看到了在for_中使用lambda的方法,但我也不知道如何做到这一点

谢谢

在这种情况下,您可以按如下方式为每个用户使用:

std::for_each(cars.begin(), cars.end(),
  [nr](Car& c){
    if(c.getNumber() == nr)
      throw RepositoryException ("Product with the same number already exists");
  });
尽管IMO C++11的基于范围的循环更干净:

for(auto& c : cars) {
  if(c.getNumber() == nr)
    throw RepositoryException ("Product with the same number already exists");
}
在这种情况下,您可以按如下方式为每种类型使用:

std::for_each(cars.begin(), cars.end(),
  [nr](Car& c){
    if(c.getNumber() == nr)
      throw RepositoryException ("Product with the same number already exists");
  });
尽管IMO C++11的基于范围的循环更干净:

for(auto& c : cars) {
  if(c.getNumber() == nr)
    throw RepositoryException ("Product with the same number already exists");
}
但实际上,大多数情况下,每个循环都会使代码比普通循环更难阅读

在这种情况下,我也可以按find_,如果进入服务:

if (std::find_if(cars->begin(), cars->end(),
      [=](const Car& c) { return c.getNumber() == nr; })
    != cars->end()) {
  throw RepositoryException("Product with the same number already exists");
}
也不清楚这是否更容易阅读

但实际上,大多数情况下,每个循环都会使代码比普通循环更难阅读

在这种情况下,我也可以按find_,如果进入服务:

if (std::find_if(cars->begin(), cars->end(),
      [=](const Car& c) { return c.getNumber() == nr; })
    != cars->end()) {
  throw RepositoryException("Product with the same number already exists");
}
也不清楚这是否更容易阅读。

您可以这样:

if (std::find_if(begin(*cars), end(*cars),
       [&](const auto& v) {return v.getNumber() == nr;}) != end(*cars))
  throw RepositoryException ("Product with the same number already exists");
你可以这样说:

if (std::find_if(begin(*cars), end(*cars),
       [&](const auto& v) {return v.getNumber() == nr;}) != end(*cars))
  throw RepositoryException ("Product with the same number already exists");

如果您使用C++11进行编译,我建议您使用std::任意类型:


我假设cars包含Car类型的对象,如果您使用C++11进行编译,我建议使用std::任意类型的对象:

我假设汽车中包含了CARS/P>> P>的对象,但你没有说你正在为哪个C++版本编码。其他答案为您提供了C++11及更高版本的解决方案。以下是一些C++11之前的解决方案,以防其他人需要它们:

struct checkForProductNumber
{
    int nr;
    checkForProductNumber(int n) : nr(n) {}

    void operator()(const Car &c)
    {
        if (c.getNumber() == nr)
            throw RepositoryException ("Product with the same number already exists");
    }
}

void someClass::doSomething()
{
    //...
    std::for_each(cars->begin(), cars->end(), checkForProductNumber(nr));
    //...
}
<>你没有说你正在为哪个C++版本编码。其他答案为您提供了C++11及更高版本的解决方案。以下是一些C++11之前的解决方案,以防其他人需要它们:

struct checkForProductNumber
{
    int nr;
    checkForProductNumber(int n) : nr(n) {}

    void operator()(const Car &c)
    {
        if (c.getNumber() == nr)
            throw RepositoryException ("Product with the same number already exists");
    }
}

void someClass::doSomething()
{
    //...
    std::for_each(cars->begin(), cars->end(), checkForProductNumber(nr));
    //...
}

为什么你需要避免原始循环?这是一门课程要求我做的事情。在编写了两周的代码之后,我们必须在所有代码上应用无原始循环规则,但在这一部分,这让我无法接受。也许std::找到了吗?为什么你需要避免原始循环?这是一门课程要求我做的事情。在编写了两周的代码之后,我们必须在所有代码上应用无原始循环规则,但在这一部分,这让我无法接受。也许std::找到了吗?所有这些都发生在一个函数中,这个函数属于一个具有某种结构的类,我必须遵循这种结构。这就是为什么我说我真的不知道如何制作另一个函数,并且每个函数都有一个lambda,这似乎是我能找到的最可读的非原始循环解决方案。是的,c++11,我的错。我将编辑并指定以供将来参考。我希望我能选择更多的答案。感谢you@Katie44:Lambdas在C++11之前不存在。我展示的内容可以在现有的类函数中使用。但由于您使用的是C++11,您可以忽略我的答案,改用lambda。@RemyLebeau我展示的内容可以在您现有的类函数中使用,如果我回忆正确,C++11之前的本地类不能用作模板参数。只能使用具有外部链接的名称。所以必须在现有函数之外有一些东西。所有这些都发生在一个函数内部,它属于一个类,我必须遵循某种结构。这就是为什么我说我真的不知道如何制作另一个函数,并且每个函数都有一个lambda,这似乎是我能找到的最可读的非原始循环解决方案。是的,c++11,我的错。我将编辑并指定以供将来参考。我希望我能选择更多的答案。感谢you@Katie44:Lambdas在C++11之前不存在。我展示的内容可以在现有的类函数中使用。但由于您使用的是C++11,您可以忽略我的答案,改用lambda。@RemyLebeau我展示的内容可以在您现有的类函数中使用,如果我回忆正确,C++11之前的本地类不能用作模板参数。只能使用具有外部链接的名称。因此,必须有一些现有函数之外的东西。不是我有点困惑,如果c++11的for you's meanted算不算原始循环之类的东西。谢谢你的选择。我有点困惑,c++11的for you's是否意味着作为一种原始循环计数之类的东西。谢谢你的选择