将向量传递给C ++中的构造函数
这是一个简单的C++程序,用于将向量传递给构造函数。
算法
Begin
Declare a class named as vector.
Declare vec of vector type.
Declare a constructor of vector class.
Pass a vector object v as a parameter to the constructor.
Initialize vec = v.
Declare a function show() to display the values of vector.
for (int i = 0; i < vec.size(); i++)
print the all values of variable i.
Declare v of vector type.
Initialize some values into v in array pattern.
Declare ob as an object against the vector class.
Pass values of v vector via ob vector object to class vector.
Call show() function using vector object to show the all values of
vector v.
End.范例程式码
#include <iostream>
#include <vector>
using namespace std;
class Vector {
vector<int> vec;
public:
Vector(vector<int> v) {
vec = v;
}
void show() {
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
}
};
int main() {
vector<int> v = {7,6,5,4};
Vector ob(v);
ob.show();
return 0;
}输出结果
7 6 5 4
热门推荐
2 红丝绒戚风蛋糕
3 夹心小甜点
4 南瓜甜点
6 粘米粉蒸蛋糕
7 酸奶华夫饼
9 红柚鸡蛋糕
10 葡萄干蛋糕
11 紫薯蛋糕卷
13 纸杯裱花小蛋糕
14 果酱奶酪蛋糕
15 日式樱花蔓越莓大福
16 草莓蛋糕卷
17 卡仕达长崎杯子蛋糕