首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >避免在for循环中使用if语句?

避免在for循环中使用if语句?
EN

Stack Overflow用户
提问于 2013-06-01 18:00:14
回答 4查看 13.6K关注 0票数 124

我有一个名为Writer的类,它的writeVector函数如下所示:

void Drawer::writeVector(vector<T> vec, bool index=true)
{
    for (unsigned int i = 0; i < vec.size(); i++) {
        if (index) {
            cout << i << "\t";
        }
        cout << vec[i] << "\n";
    }
}

我正在努力避免重复的代码,同时仍然担心性能问题。在这个函数中,我对我的for-loop的每一轮执行if (index)检查,即使结果总是相同的。这与“担心性能”背道而驰。

通过将支票放在我的for-loop之外,我可以很容易地避免这种情况。但是,我会得到大量重复的代码:

void Drawer::writeVector(...)
{
    if (index) {
        for (...) {
            cout << i << "\t" << vec[i] << "\n";
        }
    }
    else {
        for (...) {
            cout << vec[i] << "\n";
        }
    }
}

所以这些对我来说都是“坏”的解决方案。我一直在想的是,有两个私有函数,其中一个输出索引,然后调用另一个。另一个只计算出值。然而,我不知道如何在我的程序中使用它,我仍然需要if检查来看看应该调用哪个……

根据这个问题,多态性似乎是一个正确的解决方案。但我不知道该如何在这里使用它。解决这类问题的首选方法是什么?

这不是一个真正的程序,我只是想知道应该如何解决这类问题。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-06-01 18:27:57

作为函数器传入循环体。它在编译时被内联,没有性能损失。

在C++标准库中,传递变量的想法无处不在。它被称为策略模式。

如果你被允许使用C++11,你可以这样做:

#include <iostream>
#include <set>
#include <vector>

template <typename Container, typename Functor, typename Index = std::size_t>
void for_each_indexed(const Container& c, Functor f, Index index = 0) {

    for (const auto& e : c)
        f(index++, e);
}

int main() {

    using namespace std;

    set<char> s{'b', 'a', 'c'};

    // indices starting at 1 instead of 0
    for_each_indexed(s, [](size_t i, char e) { cout<<i<<'\t'<<e<<'\n'; }, 1u);

    cout << "-----" << endl;

    vector<int> v{77, 88, 99};

    // without index
    for_each_indexed(v, [](size_t , int e) { cout<<e<<'\n'; });
}

这段代码并不完美,但您已经明白了。

在旧的C++98中,它看起来像这样:

#include <iostream>
#include <vector>
using namespace std;

struct with_index {
  void operator()(ostream& out, vector<int>::size_type i, int e) {
    out << i << '\t' << e << '\n';
  }
};

struct without_index {
  void operator()(ostream& out, vector<int>::size_type i, int e) {
    out << e << '\n';
  }
};


template <typename Func>
void writeVector(const vector<int>& v, Func f) {
  for (vector<int>::size_type i=0; i<v.size(); ++i) {
    f(cout, i, v[i]);
  }
}

int main() {

  vector<int> v;
  v.push_back(77);
  v.push_back(88);
  v.push_back(99);

  writeVector(v, with_index());

  cout << "-----" << endl;

  writeVector(v, without_index());

  return 0;
}

再说一次,代码远非完美,但它给了你灵感。

票数 80
EN

Stack Overflow用户

发布于 2013-06-01 18:10:45

在函数中,我对

循环的每一轮都执行if (索引)检查,即使结果总是相同的。这与“担心性能”背道而驰。

如果确实是这样的话,分支预测器在预测(常量)结果时将没有问题。因此,在最初的几次迭代中,这只会导致轻微的错误预测开销。就性能而言,没什么好担心的

在这种情况下,为了清晰起见,我主张将测试保留在循环中。

票数 40
EN

Stack Overflow用户

发布于 2013-06-01 20:17:51

为了扩展Ali的答案,它是完全正确的,但仍然重复了一些代码(循环体的一部分,不幸的是,这在使用策略模式时几乎是不可避免的)……

当然,在这种特殊情况下,代码重复并不多,但有一种方法可以进一步减少它,如果函数体比几条指令更大,那么这种方法就会派上用场。

关键是使用编译器的能力来执行常量折叠/死代码消除。我们可以通过手动将index的运行时值映射到编译时值(当只有有限数量的情况--在本例中是两种)并使用编译时已知的非类型模板参数来实现:

template<bool index = true>
//                  ^^^^^^ note: the default value is now part of the template version
//                         see below to understand why
void writeVector(const vector<int>& vec) {
    for (size_t i = 0; i < vec.size(); ++i) {
        if (index) { // compile-time constant: this test will always be eliminated
            cout << i << "\t"; // this will only be kept if "index" is true
        }
        cout << vec[i] << "\n";
    }
}

void writeVector(const vector<int>& vec, bool index)
//                                            ^^^^^ note: no more default value, otherwise
//                                            it would clash with the template overload
{
    if (index) // runtime decision
        writeVector<true>(vec);
        //          ^^^^ map it to a compile-time constant
    else
        writeVector<false>(vec);
}

这样,我们最终得到了编译后的代码,它与您的第二个代码示例(外部if /内部for)等效,但不会自己复制代码。现在我们可以让writeVector的模板版本像我们想要的那样复杂,总会有一段代码需要维护。

请注意模板版本(采用非类型模板参数形式的编译时常量)和非模板版本(采用运行时变量作为函数参数)是如何被重载的。这允许您根据自己的需要选择最相关的版本,在这两种情况下都有一个非常相似、易于记忆的语法:

writeVector<true>(vec);   // you already know at compile-time which version you want
                          // no need to go through the non-template runtime dispatching

writeVector(vec, index);  // you don't know at compile-time what "index" will be
                          // so you have to use the non-template runtime dispatching

writeVector(vec);         // you can even use your previous syntax using a default argument
                          // it will call the template overload directly
票数 35
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16871471

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档