Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to sort each column of a matrix with strings in C++

2 Answers

0 votes
#include <iostream>
#include <vector>
#include <algorithm>

void sortColumns(std::vector<std::vector<std::string>>& matrix) {
    int rows = matrix.size();
    int cols = matrix[0].size();

    for (int col = 0; col < cols; col++) {
        // Extract column into a vector
        std::vector<std::string> column;
        for (int row = 0; row < rows; row++) {
            column.push_back(matrix[row][col]);
        }

        // Sort the column
        std::sort(column.begin(), column.end());

        // Place sorted values back into the matrix
        for (int row = 0; row < rows; row++) {
            matrix[row][col] = column[row];
        }
    }
}

void printMatrix(const std::vector<std::vector<std::string>>& matrix) {
    for (const auto& row : matrix) {
        for (const auto& elem : row) {
            std::cout << elem << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    std::vector<std::vector<std::string>> matrix = {
        {"ccc", "zzzz", "x"},
        {"eee", "aaa", "ffff"},
        {"bbb", "gg", "yyyyyy"}
    };

    std::cout << "Original Matrix:\n";
    printMatrix(matrix);

    sortColumns(matrix);

    std::cout << "\nSorted Matrix:\n";
    printMatrix(matrix);
}



/*
run:

Original Matrix:
ccc zzzz x 
eee aaa ffff 
bbb gg yyyyyy 

Sorted Matrix:
bbb aaa ffff 
ccc gg x 
eee zzzz yyyyyy 

*/

 



answered Jun 1, 2025 by avibootz
0 votes
#include <iostream>
#include <vector>
#include <algorithm>

void sortColumns(std::vector<std::vector<std::string>>& matrix) {
    int rows = matrix.size();
    int cols = matrix[0].size();

    for (int col = 0; col < cols; col++) {
        // Extract column into a vector
        std::vector<std::string> column;
        for (int row = 0; row < rows; row++) {
            column.push_back(matrix[row][col]);
        }

        // Sort the column
        std::sort(column.begin(), column.end());

        // Place sorted values back into the matrix
        for (int row = 0; row < rows; row++) {
            matrix[row][col] = column[row];
        }
    }
}

void printMatrix(const std::vector<std::vector<std::string>>& matrix) {
    for (const auto& row : matrix) {
        for (const auto& elem : row) {
            std::cout << elem << " ";
        }
        std::cout << "\n";
    }
}

int main() {
    std::vector<std::vector<std::string>> matrix = {
        {"ccc", "zzzz", "x"},
        {"eeee", "aaa", "ffff"},
        {"uu", "hhh", "uuu"},
        {"bbb", "gg", "yyyyyy"}
    };

    std::cout << "Original Matrix:\n";
    printMatrix(matrix);

    sortColumns(matrix);

    std::cout << "\nSorted Matrix:\n";
    printMatrix(matrix);
}



/*
run:

Original Matrix:
ccc zzzz x 
eeee aaa ffff 
uu hhh uuu 
bbb gg yyyyyy 

Sorted Matrix:
bbb aaa ffff 
ccc gg uuu 
eeee hhh x 
uu zzzz yyyyyy 

*/

 



answered Jun 1, 2025 by avibootz
...