C ++比较2个不同向量的元素

时间:2017-06-18 23:55:58

标签: c++ vector operators

这是我的代码:

// vector 2, vector 3 contain each 7 integers and vector 1 contains 7 vectors of 7 integers.

std::vector < std::vector<int> > vector1;
vector1.push_back(vector2);
vector1.push_back(vector3);

if(vector1[1][0] == vector1[0][0]) {
    std::cout<<"Equal";
}

由于2个向量的比较,我的编译器崩溃了。我感觉有一种不同的比较多向量的方法。我一直在网上搜索,无法找到一些东西。

谢谢!

1 个答案:

答案 0 :(得分:0)

此代码正是您所说的不起作用。 如果你已经创建了矢量&lt;矢量&lt; int&gt; &GT;正确的方法是,这段代码没有任何问题。你只是比较两个数字。

#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::vector;

int main()
{
    vector<int> v1{1,2};
    vector<int> v2{2,3};
    vector<vector<int> > v3;
    v3.push_back(v1);
    v3.push_back(v2);
    if(v3[0][0] == v3[1][0])
    {
        cout << "The first elements of the first and second vector of v3 are equal" << std::endl;
    }
    else
       cout << "They are different" << std::endl;
    return 0;
}
相关问题