[英]Rotate M*N matrix 90 Degrees Clockwise ,C++
我正在嘗試旋轉chars的向量。
我進行了二維向量矩陣設置。 現在,矩陣從文件中獲取輸入,我使用vector.push_back(c)
將字符添加到vvc中; vvc數組的一個例子是這樣的
aaaaa
azzza
azaza
azzza
azaaa
azaaa
azaaa
aaaaa
我有vvc設置,但是我試圖將其旋轉90度。 我將其逆時針旋轉90度,但我需要將其順時針旋轉90度。
截至目前,我的代碼執行此操作
90 counter clock
aaaaaaaa
azzzzzza
azazaaaa
azzzaaaa
aaaaaaaa
它通過這個循環來完成;
cout <<"\n90 counter clock"<<endl;
for (size_t colNo = 0; colNo < kvsize2; colNo++)
{
for (const auto &row : twovector)
{
char colVal = row.at(colNo);
cout << colVal;
}
cout << endl;
}
我只是在學習向量及其范圍。 嘗試執行遞減循環幾乎可以工作,但總是讓我陷入段錯狀態。
我正在使用“已解決”
twovector.push_back(temp);
運用
twovector.insert(twovector.begin(),temp);
給我
90 counter clock aaaaaaaa azzzzzza aaaazaza aaaazzza aaaaaaaa
如果我做對了,而您想要做的就是順時針打印90度矩陣,請嘗試以下代碼:
for (int colNo = 0; colNo < vec[0].size(); colNo++)
{
for (int i = vec.size() - 1; i >= 0; i--)
{
const auto& row = vec[i];
int colVal = row.at(colNo);
cout << colVal;
}
cout << endl;
}
解決問題的特定部分:
如果有人對如何旋轉M * N 2d向量數組有任何提示或建議
C ++擅長將算法與數據分離。
請注意,答案有點冗長,目的是為了教程。
讓我們開始 !!
我們需要rotate_2d_matrix_clockwise
算法中的3個功能:
int
, char
, double
或任何用戶定義的類型。 std::array
和std::vector
rotate_2d_matrix_clockwise
返回的結果調用rotate_2d_matrix_clockwise
,以實現兩次旋轉。 一旦明確了需求,就可以為算法起草一些用例。
std::vector<std::vector<char>> data = { {'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'i', 'j', 'k', 'l'} };
rotate_2d_matrix_clockwise(data); // rotating 2d-matrix of vector<char>
std::array<std::array<int, 4>, 3> data2 = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
// rotating 2d-matrix of array<int>, twice
rotate_2d_matrix_clockwise(rotate_2d_matrix_clockwise(data2)));
因此,讓我們使用一些模板來創建通用的2d順時針旋轉功能。
我們的rotate_2d_matrix_clockwise
將:
original_matrix
並返回一個新的rotated_matrix
。 rotated_matrix
並將它傳遞給一個輔助函數rotate_2d_matrix_clockwise_impl
的實際工作將完成。 那么std::array
的rotate_2d_matrix_clockwise
實現的外觀如何?
template<typename T, size_t M, size_t N>
auto rotate_2d_matrix_clockwise(std::array<std::array<T, M>, N> const & original_matrix)
-> std::array<std::array<T, N>, M>
{
std::array<std::array<T, N>, M> rotated_matrix;
rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N); // rotate
return rotated_matrix;
}
整潔而精確。
rotate_2d_matrix_clockwise
, std::vector
的rotate_2d_matrix_clockwise
的實現有點混亂。
template<typename Matrix2D>
auto rotate_2d_matrix_clockwise(Matrix2D const & original_matrix) -> Matrix2D
{
int const M = original_matrix[0].size(); // deduce M and N
int const N = original_matrix.size();
Matrix2D rotated_matrix; // vector has no form, hence we have to resize it for `N x M`
rotated_matrix.resize(M);
for (auto x = 0; x < M; ++x) {
rotated_matrix[x].resize(N);
}
rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N); // rotate
return rotated_matrix;
}
現在讓我們看一下實際的旋轉算法rotate_2d_matrix_clockwise_impl
外觀。
應該注意的是,該算法與容器和/或所包含的數據無關。 它只是專注於旋轉。
template<typename OriginalMatrix2D, typename RotatedMatrix2D>
void rotate_2d_matrix_clockwise_impl(OriginalMatrix2D const & original_matrix,
RotatedMatrix2D & rotated_matrix,
int const M,
int const N)
{
for (auto x = 0; x < N; ++x) {
for (auto y = 0; y < M; ++y) {
// Source : https://stackoverflow.com/questions/4780119/2d-euclidean-vector-rotations
rotated_matrix[y][-x -1 +N] = original_matrix[x][y];
}
}
}
這是用C ++ 11編譯的完整示例。
#include <iostream>
#include <vector>
#include <array>
template<typename Matrix2D>
void print_matrix(Matrix2D const & vec)
{
std::cout << "size of matrix is [" << vec[0].size() << " x " << vec.size() << "]\n";
for (auto const & inner_vec : vec) {
for (auto const & item : inner_vec) {
std::cout << item << ", ";
}
std::cout << std::endl;
}
}
template<typename OriginalMatrix2D, typename RotatedMatrix2D>
void rotate_2d_matrix_clockwise_impl(OriginalMatrix2D const & matrix,
RotatedMatrix2D & rotated_matrix,
int const M,
int const N)
{
for (auto x = 0; x < N; ++x) {
for (auto y = 0; y < M; ++y) {
// Source : https://stackoverflow.com/questions/4780119/2d-euclidean-vector-rotations
rotated_matrix[y][-x -1 +N] = matrix[x][y];
}
}
}
template<typename T, size_t M, size_t N>
auto rotate_2d_matrix_clockwise(std::array<std::array<T, M>, N> const & original_matrix)
-> std::array<std::array<T, N>, M>
{
std::array<std::array<T, N>, M> rotated_matrix;
rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N);
return rotated_matrix;
}
template<typename Matrix2D>
auto rotate_2d_matrix_clockwise(Matrix2D const & original_matrix) -> Matrix2D
{
int const M = original_matrix[0].size();
int const N = original_matrix.size();
Matrix2D rotated_matrix;
rotated_matrix.resize(M);
for (auto x = 0; x < M; ++x) {
rotated_matrix[x].resize(N);
}
rotate_2d_matrix_clockwise_impl(original_matrix, rotated_matrix, M, N);
return rotated_matrix;
}
int main()
{
std::array<std::array<int, 4>, 3> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
std::cout << "\nBefore Rotation :\n";
print_matrix(data);
std::cout << "\nAfter 2nd Clockwise Rotation :\n";
print_matrix(rotate_2d_matrix_clockwise(rotate_2d_matrix_clockwise(data)));
std::vector<std::vector<char>> data2 = { {'a', 'b', 'c', 'd'}, {'e', 'f', 'g', 'h'}, {'i', 'j', 'k', 'l'}};
std::cout << "Before Rotation :\n";
print_matrix(data2);
std::cout << "\nAfter Clockwise Rotation :\n";
print_matrix(rotate_2d_matrix_clockwise(data2));
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.