2013-10-19 104 views
-1

將int數組轉換爲向量最簡單的方法是什麼?並在if語句中使用它。將數組轉換爲向量

int num1[4] = {2, 4, 1, 8}; 
int num2[4] = {2, 4, 6, 8}; 

if (testNUM(num1, num2, 4)) 
    cout << "ERROR: num1 and num2 are reported to be the same.\n"; 
    else 
    cout << "SUCCESS: num1 and num2 are correctly identified " 
    << "as different.\n"; 

testNUM被聲明爲函數原型(BOOL)。

感謝,

+0

請定義'testNUM' –

+0

'num1'和'num2'都是數組,爲什麼你需要將它們轉換爲向量? – azz

回答

6

這是怎麼陣列以一個矢量轉換爲矢量定義的一部分:

std::vector<int> v(num1, num1+4); 

這是怎麼陣列轉換爲矢量不作爲載體的一部分定義:

std::vector<int> v; 
v.assign(num1, num1+4); 

或者,也許你的意思是「我的轉換程序使用的載體,而不是數組」:

std::vector<int> num1 = { 2, 4, 1, 8}; 
+0

'bool testNUM(int set1 [],int set2 [],int size);' – mani

+0

@mani,使它成爲'(const)std :: vector &'s。 – chris