将数组转换为矢量

converting array to vector

本文关键字:转换 数组      更新时间:2023-10-16

int 数组转换为 vector 的最简单方法是什么?并将其与 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)。

谢谢

这是将数组转换为向量作为向量定义的一部分的方式:

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

这就是将数组转换为向量而不是作为向量定义的一部分的方式:

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

或者你的意思是"将我的程序转换为使用向量而不是数组":

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