避免在for循环中使用if语句
Avoiding if statement inside a for loop?
我有一个名为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
循环的每一轮进行if (index)
检查,即使结果总是相同的。这是反对"担心性能"的。
我可以很容易地避免这种情况,通过将检查放在for
-循环之外。但是,我将得到大量重复的代码:
void Drawer::writeVector(...)
{
if (index) {
for (...) {
cout << i << "t" << vec[i] << "n";
}
}
else {
for (...) {
cout << vec[i] << "n";
}
}
}
所以这些对我来说都是"坏"的解决方案。我一直在想,是两个私有函数,其中一个输出索引,然后调用另一个。另一个只输出值。然而,我不知道如何使用它与我的程序,我仍然需要if
检查,看看哪一个调用…
根据问题,多态性似乎是一个正确的解决方案。但我不知道在这里该怎么用。解决这类问题的首选方法是什么?
这不是一个真正的程序,我只是对如何解决这类问题感兴趣
作为函子传入循环体。它在编译时内联,没有性能损失。
传递变量的想法在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;
}
同样,代码远非完美,但它给了你一个想法。
在函数中,我对每一轮for循环进行if (index)检查,即使结果总是相同的。这是反对"担心性能"的。
如果确实是这种情况,那么分支预测器在预测(常量)结果时将没有问题。因此,这只会在最初的几个迭代中造成轻微的错误预测开销。在性能方面没有什么可担心的
在这种情况下,为了清晰,我建议将测试保持在循环内。
对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
在大多数情况下,您的代码在性能和可读性方面已经很好了。好的编译器能够检测循环不变量并进行适当的优化。考虑下面的示例,它与您的代码非常接近:
#include <cstdio>
#include <iterator>
void write_vector(int* begin, int* end, bool print_index = false) {
unsigned index = 0;
for(int* it = begin; it != end; ++it) {
if (print_index) {
std::printf("%d: %dn", index, *it);
} else {
std::printf("%dn", *it);
}
++index;
}
}
int my_vector[] = {
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
};
int main(int argc, char** argv) {
write_vector(std::begin(my_vector), std::end(my_vector));
}
我使用下面的命令行来编译它:
g++ --version
g++ (GCC) 4.9.1
Copyright (C) 2014 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ -O3 -std=c++11 main.cpp
然后,让我们转储汇编:
objdump -d a.out | c++filt > main.s
write_vector
的结果集为:
00000000004005c0 <write_vector(int*, int*, bool)>:
4005c0: 48 39 f7 cmp %rsi,%rdi
4005c3: 41 54 push %r12
4005c5: 49 89 f4 mov %rsi,%r12
4005c8: 55 push %rbp
4005c9: 53 push %rbx
4005ca: 48 89 fb mov %rdi,%rbx
4005cd: 74 25 je 4005f4 <write_vector(int*, int*, bool)+0x34>
4005cf: 84 d2 test %dl,%dl
4005d1: 74 2d je 400600 <write_vector(int*, int*, bool)+0x40>
4005d3: 31 ed xor %ebp,%ebp
4005d5: 0f 1f 00 nopl (%rax)
4005d8: 8b 13 mov (%rbx),%edx
4005da: 89 ee mov %ebp,%esi
4005dc: 31 c0 xor %eax,%eax
4005de: bf a4 06 40 00 mov $0x4006a4,%edi
4005e3: 48 83 c3 04 add $0x4,%rbx
4005e7: 83 c5 01 add $0x1,%ebp
4005ea: e8 81 fe ff ff callq 400470 <printf@plt>
4005ef: 49 39 dc cmp %rbx,%r12
4005f2: 75 e4 jne 4005d8 <write_vector(int*, int*, bool)+0x18>
4005f4: 5b pop %rbx
4005f5: 5d pop %rbp
4005f6: 41 5c pop %r12
4005f8: c3 retq
4005f9: 0f 1f 80 00 00 00 00 nopl 0x0(%rax)
400600: 8b 33 mov (%rbx),%esi
400602: 31 c0 xor %eax,%eax
400604: bf a8 06 40 00 mov $0x4006a8,%edi
400609: 48 83 c3 04 add $0x4,%rbx
40060d: e8 5e fe ff ff callq 400470 <printf@plt>
400612: 49 39 dc cmp %rbx,%r12
400615: 75 e9 jne 400600 <write_vector(int*, int*, bool)+0x40>
400617: eb db jmp 4005f4 <write_vector(int*, int*, bool)+0x34>
400619: 0f 1f 80 00 00 00 00 nopl 0x0(%rax)
我们可以看到,在函数开始时,我们检查值并跳转到两个可能的循环之一:
4005cf: 84 d2 test %dl,%dl
4005d1: 74 2d je 400600 <write_vector(int*, int*, bool)+0x40>
当然,这只有在编译器能够检测到条件是实际不变的情况下才有效。通常,它完全适用于标志和简单的内联函数。但如果条件是"复杂的",可以考虑使用其他答案中的方法。
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复