【问题标题】:C++ iterating over 2 vectors using for loopC++ 使用 for 循环迭代 2 个向量
【发布时间】:2019-07-15 23:31:25
【问题描述】:

我正在尝试使用 for 循环迭代 2 std::vectors,但出现错误。迭代 1 个向量有效,不确定当我在 for 循环中放入另一个迭代器时出了什么问题。

#include<iostream>
#include<vector>

int main()
{
    std::vector<double> x={1,2,3,4,5}, d={10,11,12,13,14},x1,x2;

    /*for (std::vector<double>::iterator it1 = x.begin(),std::vector<double>::iterator it2 = d.begin();
    it1!=x.end(),it2!=d.end(); ++it1,++it2  )
    {
        x1.push_back(*it1 + (*it2));
        x2.push_back(*it1 + (*it2));
    }*/

    for (std::vector<double>::iterator it1 = x.begin();it1!=x.end(); ++it1  )
    {
        x1.push_back(*it1 );

    }

    return 0;
}

注释掉的代码有多个错误:qualified-id in declaration before ‘it2’ for (std::vector&lt;double&gt;::iterator it1 = x.begin(),std::vector&lt;double&gt;::iterator it2 = d.begin()); 有人可以解释这里有什么问题吗?

【问题讨论】:

  • 此位 it1!=x.end(),it2!=d.end() 需要是布尔语句,而不是逗号
  • @dainsleif 这是一个布尔表达式。它评估it1!=x.end(),丢弃结果,然后评估并返回it2!=d.end()

标签: c++ c++11 vector iterator


【解决方案1】:

for 循环的第一部分是一个普通的声明,就像上面在一行中声明和定义多个向量一样。只写一次类型:

for (std::vector<double>::iterator it1 = x.begin(), it2 = d.begin();
     it1!=x.end() && it2!=d.end(); 
     ++it1, ++it2)
{
    x1.push_back(*it1 + (*it2));
    x2.push_back(*it1 + (*it2));
}

循环条件中还有一个错误:使用&amp;&amp; 而不是,,否则这是一个逗号运算符,其结果只是最后一部分。

最后,现在习惯使用auto,而不是写出那些长类型:

for (auto it1 = x.begin(), it2 = d.begin();
     it1 != x.end() && it2 != d.end(); 
     ++it1, ++it2)
{
    x1.push_back(*it1 + (*it2));
    x2.push_back(*it1 + (*it2));
}

【讨论】:

  • 你能解释更多You also have an error in the loop condition: Use &amp;&amp; instead of the ,, because otherwise this is a comma operator whose result is only the last part.吗?没听懂
  • 逗号在 C++ 中有几个不同的用途。当您在表达式中使用它时(任何返回值的东西,所以不是声明),它用作运算符。它计算两边的表达式并返回其右侧参数的结果。这不是你想要的——你想要的是逻辑与!逗号运算符仅在您明确想要丢弃结果时使用(它非常罕见,通常仅在您想要在无法使用单独行的表达式中产生副作用时使用)。
【解决方案2】:

使用逗号(,)声明多个变量时,只需指定一次类型。改变这个:

for (std::vector<double>::iterator it1 = x.begin(), std::vector<double>::iterator it2 = d.begin();

到这里:

for (std::vector<double>::iterator it1 = x.begin(), it2 = d.begin();

或更好 - 因为您使用的是c++11,请使用auto

for (auto it1 = x.begin(), it2 = d.begin();

这消除了编译器错误,但您仍然有一个逻辑错误:

it1!=x.end(),it2!=d.end(); ++it1,++it2  )

您正在丢弃it1!=x.end()。在表达式中,逗号操作符计算每个表达式,但只计算最后一个。将, 更改为&amp;&amp;,如下所示:

it1!=x.end() && it2!=d.end(); ++it1,++it2  )

你可以走了。

【讨论】:

  • 你能解释一下为什么comma是错误的,应该改用&amp;&amp;吗?
【解决方案3】:
  1. 您在 for 循环中的定义中有错误。定义变量时,不能使用逗号运算符提供多种类型。 这是错误的:

    std::vector<double>::iterator it1 = x.begin(), std::vector<double>::iterator it2 = d.begin();
    

    要纠正这个问题,请注意迭代器是相同的类型,所以你可以写:

    std::vector<double>::iterator it1 = x.begin(), it2 = d.begin();
    
  2. 测试条件不正确。在这里,逗号运算符执行第一个测试,但随后忽略了结果。 这在逻辑上是不正确的:

    it1 != x.end(), it2 != d.end();
    

    您应该使用逻辑与运算符来确保循环将在任一条件为假时立即终止:

    it1 != x.end() && it2!=d.end();
    

【讨论】:

    【解决方案4】:

    一种替代方法,而不是使用 for 循环。这样做不是很容易吗:

    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include <iostream>
    
    template<typename T>
    void printVector(const std::vector<T>& vec) {
        for (auto& v : vec)
            std::cout << v << ' ';
        std::cout << '\n';
    }
    
    int main() {
        std::vector<double> x{ 1,2,3,4,5 }, d{ 10,11,12,13,14 }, 
                            x1( x.size(), 0 ), x2( x.size(), 0 );
    
        std::transform(x.begin(), x.end(), d.begin(), x1.begin(), std::plus<double>());
        std::transform(x.begin(), x.end(), d.begin(), x2.begin(), std::plus<double>());
    
        printVector(x);
        printVector(d);
        printVector(x1);
        printVector(x2);
    
        return 0;
    }
    

    它给了我这个输出:

    1 2 3 4 5
    10 11 12 13 14
    11 13 15 17 19
    11 13 15 17 19
    

    因为这似乎是您试图通过这两个向量实现的目标。我也认为这看起来更干净,可读性更好。恕我直言,我相信这比 for 循环更具表现力。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多