[英]Nested range-based for-loops
I have the following code using range-based for
-loops (C++11): 我有使用范围基于下面的代码for
-loops(C ++ 11):
vector<atom> protein;
...
for(atom &atom1 : protein) {
...
for(atom &atom2 : protein) {
if(&atom1 != &atom2) {
...
}
}
}
Is there a better/cleaner/faster way to write this nested loops? 是否有更好/更清洁/更快的方式来编写这个嵌套循环? Isn't there a way to include the if
condition in the second loop? 是不是有办法在第二个循环中包含if
条件?
Similar to ronag's answer is a more generic version: 与ronag相似的答案是更通用的版本:
template<typename C, typename Op>
void each_unique_pair(C& container, Op fun)
{
for(auto it = container.begin(); it != container.end() - 1; ++it)
{
for(auto it2 = std::next(it); it2 != container.end(); ++it2)
{
fun(*it, *it2);
fun(*it2, *it);
}
}
}
UPDATE UPDATE
template<typename C, typename O1, typename O2>
void each_value_and_pair(C& container, O1 val_fun, O2 pair_fun)
{
auto it = std::begin(container);
auto end = std::end(container);
if(it == end)
return;
for(; it != std::prev(end); ++it)
{
val_fun(*it);
for(auto it2 = std::next(it); it2 != end; ++it2)
{
pair_fun(*it2, *it);
pair_fun(*it, *it2);
}
}
}
Which is used like this: 使用方式如下:
main()
{
std::vector<char> values;
// populate values
// ....
each_value_and_pair(values,
[](char c1) { std::cout << "value: " << c1 << std::endl;},
[](char c1, char c2){std::cout << "pair: " << c1 << "-" << c2 << std::endl;});
}
Sad but true. 伤心但真实。 How about normal loops with iterators and auto keyword? 如何使用迭代器和auto关键字进行正常循环?
I think this might be what you are looking for: 我想这可能就是你要找的东西:
for(auto it1 = std::begin(protein1); it1 != std::end(protein); ++it1)
{
for(auto it2 = std::next(it1); it2 != std::end(protein); ++it2)
{
auto& atom1 = *it1;
auto& atom2 = *it2;
// ...
}
}
you're method is just fine. 你的方法很好。 if you want to save the if statement you can 如果你想保存if语句,你可以
vector<atom> protein;
int i, j;
...
for(i = 0; i < protein.size() : i++) {
atom &atom1 = protein.at(i);
for(j = i+1; j < protein.size() ; j++) {
atom &atom2 = protein.at(j);
// Do something
// Swap the atom2 and atom1
// Do something again
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.