2017-06-18 73 views
-3

的比較元件這是我的代碼:因爲2個向量的比較C++ 2不同載體

// 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"; 
} 

我的編譯器崩潰。我有一種感覺,有一種不同的方式來比較多向量。我一直在網上搜索,找不到東西。

謝謝!

+0

歡迎來到Stack Overflow。請花些時間閱讀[The Tour](http://stackoverflow.com/tour),並參閱[幫助中心](http://stackoverflow.com/help/asking)中的資料,瞭解您可以在這裏問。 –

+5

你的**編譯器崩潰**?你什麼意思? –

+1

'vector1.push_back(10);'是無效的,不應該編譯。 'vector1'不能存儲'int'。你需要'push_back'一個'vector'這個味道雖然是[X​​-Y問題](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。你的總體目標是什麼? – user4581301

回答

0

此代碼完全符合您所說的不起作用。 如果你已經創建了矢量< vector < int >>正確的方法,這段代碼沒有任何問題。你只是比較兩個數字。

#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; 
}