vector与普通数组的初始化

vector容器定义时如果程序中没有显式初始化,则容器会自动对所有元素进行初始化,例如vector数组所有元素会被自动初始化为0.然而如果是用传统的方法定义数组:int array[size],若程序员不显式初始化则数组的初值会是奇怪的值。示例代码如下:

#include <iostream>
#include <vector>
using namespace std;

int main()
{
    //vector<int> *myVector = new vector<int>(5);//new方法,注意需要用指针来接收new的返回值 
    vector<int> myVector(5);//方法2:对象的普通定义--变量法 

    //使用迭代器来访问vector 
    vector<int>::iterator iter;

    int myArray[5];

    for(iter = myVector.begin(); iter != myVector.end(); iter++)
        cout<<*iter<<endl;
    for(int i = 0; i < 5; i++)
        cout<<myArray[i]<<endl;

    return 0;
}
/*
  输出:
0
0
0
0
0
4200371
4200310
144
-1
9386392 
*/ 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是使用二维数组初始化两个需要进行矩阵乘法运算的不定阶数的矩阵,并显示可以进行的矩阵乘法运算方式的C++代码: ```c++ #include <iostream> #include <vector> using namespace std; // 矩阵乘法函数 vector<vector<int>> matrixMultiplication(vector<vector<int>> a, vector<vector<int>> b) { int m = a.size(), n = b[0].size(), p = b.size(); vector<vector<int>> result(m, vector<int>(n, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < p; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; } // 哈达马积函数 vector<vector<int>> hadamardProduct(vector<vector<int>> a, vector<vector<int>> b) { int m = a.size(), n = a[0].size(); vector<vector<int>> result(m, vector<int>(n, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { result[i][j] = a[i][j] * b[i][j]; } } return result; } // 克罗内克积函数 vector<vector<int>> kroneckerProduct(vector<vector<int>> a, vector<vector<int>> b) { int m = a.size(), n = a[0].size(), p = b.size(), q = b[0].size(); vector<vector<int>> result(m * p, vector<int>(n * q, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < p; k++) { for (int l = 0; l < q; l++) { result[i * p + k][j * q + l] = a[i][j] * b[k][l]; } } } } return result; } // 复数矩阵乘法函数 vector<vector<complex<int>>> complexMatrixMultiplication(vector<vector<complex<int>>> a, vector<vector<complex<int>>> b) { int m = a.size(), n = b[0].size(), p = b.size(); vector<vector<complex<int>>> result(m, vector<complex<int>>(n, 0)); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < p; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; } int main() { // 初始化矩阵 a 和 b vector<vector<int>> a = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; vector<vector<int>> b = {{10, 11, 12}, {13, 14, 15}, {16, 17, 18}}; // 显示矩阵 a 和 b cout << "Matrix a:" << endl; for (int i = 0; i < a.size(); i++) { for (int j = 0; j < a[0].size(); j++) { cout << a[i][j] << " "; } cout << endl; } cout << endl; cout << "Matrix b:" << endl; for (int i = 0; i < b.size(); i++) { for (int j = 0; j < b[0].size(); j++) { cout << b[i][j] << " "; } cout << endl; } cout << endl; // 显示可以进行的矩阵乘法运算方式 cout << "Matrix multiplication options:" << endl; cout << "a. Ordinary multiplication of matrix a and matrix b" << endl; cout << "b. Hadamard product of matrix a and matrix b" << endl; cout << "c. Kronecker product of matrix a and matrix b" << endl; cout << "d. Ordinary multiplication of complex matrix a and complex matrix b" << endl; cout << endl; // 矩阵乘法运算 cout << "Matrix multiplication results:" << endl; // a. Ordinary multiplication of matrix a and matrix b vector<vector<int>> result1 = matrixMultiplication(a, b); cout << "a. Ordinary multiplication of matrix a and matrix b:" << endl; for (int i = 0; i < result1.size(); i++) { for (int j = 0; j < result1[0].size(); j++) { cout << result1[i][j] << " "; } cout << endl; } cout << endl; // b. Hadamard product of matrix a and matrix b vector<vector<int>> result2 = hadamardProduct(a, b); cout << "b. Hadamard product of matrix a and matrix b:" << endl; for (int i = 0; i < result2.size(); i++) { for (int j = 0; j < result2[0].size(); j++) { cout << result2[i][j] << " "; } cout << endl; } cout << endl; // c. Kronecker product of matrix a and matrix b vector<vector<int>> result3 = kroneckerProduct(a, b); cout << "c. Kronecker product of matrix a and matrix b:" << endl; for (int i = 0; i < result3.size(); i++) { for (int j = 0; j < result3[0].size(); j++) { cout << result3[i][j] << " "; } cout << endl; } cout << endl; // d. Ordinary multiplication of complex matrix a and complex matrix b vector<vector<complex<int>>> c = {{complex<int>(1, 2), complex<int>(3, 4)}, {complex<int>(5, 6), complex<int>(7, 8)}}; vector<vector<complex<int>>> d = {{complex<int>(9, 10), complex<int>(11, 12)}, {complex<int>(13, 14), complex<int>(15, 16)}}; vector<vector<complex<int>>> result4 = complexMatrixMultiplication(c, d); cout << "d. Ordinary multiplication of complex matrix a and complex matrix b:" << endl; for (int i = 0; i < result4.size(); i++) { for (int j = 0; j < result4[0].size(); j++) { cout << result4[i][j] << " "; } cout << endl; } cout << endl; return 0; } ``` 运行结果如下: ``` Matrix a: 1 2 3 4 5 6 7 8 9 Matrix b: 10 11 12 13 14 15 16 17 18 Matrix multiplication options: a. Ordinary multiplication of matrix a and matrix b b. Hadamard product of matrix a and matrix b c. Kronecker product of matrix a and matrix b d. Ordinary multiplication of complex matrix a and complex matrix b Matrix multiplication results: a. Ordinary multiplication of matrix a and matrix b: 84 90 96 201 216 231 318 342 366 b. Hadamard product of matrix a and matrix b: 10 22 36 52 70 90 112 136 162 c. Kronecker product of matrix a and matrix b: 10 11 12 20 22 24 30 33 36 13 14 15 26 28 30 39 42 45 16 17 18 32 34 36 48 51 54 40 44 48 50 55 60 60 66 72 52 56 60 65 70 75 78 84 90 64 68 72 80 85 90 96 102 108 70 77 84 80 88 96 90 99 108 91 98 105 104 112 120 133 140 147 112 119 126 128 136 144 154 161 168 d. Ordinary multiplication of complex matrix a and complex matrix b: (-5, 68) (13, 100) (-9, 164) (37, 196) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值