c++使用vector求矩阵的转置
#include <vector>
#include <iostream>
using namespace std;
vector<vector<double>> transposeInPlace(vector<vector<double>> m) {
vector<vector<double>> array;
vector<double> temparay;
for (int i = 0; i < m[0].size(); ++i)
{
for (int j = 0; j < m.size(); ++j) {
temparay.push_back(m[j][i]);
}
array.push_back(temparay);
temparay.erase(temparay.begin(), temparay.end());
}
return array;
}
int main() {
vector<vector<double>> nums1 = {
{1, 2, 3},
{4, 15, 6}
};
vector<vector<double>> w = transposeInPlace(nums1);
for (int i = 0; i < w.size(); i++)
{
for (int j = 0; j < w[0].size(); j++)
{
cout << w[i][j] << " ";
}
cout << endl;
}
}