C++ Online Compiler
Example: Print Unique Rows using Set of Strings in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Print Unique Rows using Set of Strings #include <iostream> #include <vector> #include <string> #include <set> using namespace std; // Function to print unique rows void printUniqueRows(const vector<vector<int>>& matrix) { int R = matrix.size(); if (R == 0) return; // Handle empty matrix int C = matrix[0].size(); set<string> uniqueRowsSet; for (int i = 0; i < R; ++i) { string currentRowStr = ""; for (int j = 0; j < C; ++j) { currentRowStr += to_string(matrix[i][j]); } // Attempt to insert the string representation of the current row // If insertion is successful, it means the row is unique if (uniqueRowsSet.find(currentRowStr) == uniqueRowsSet.end()) { uniqueRowsSet.insert(currentRowStr); // Print the unique row for (int j = 0; j < C; ++j) { cout << matrix[i][j] << " "; } cout << endl; } } } int main() { // Step 1: Define the boolean matrix vector<vector<int>> matrix = { {0, 1, 0, 0}, {1, 0, 1, 1}, {0, 1, 0, 0}, {1, 1, 0, 0} }; cout << "Unique rows in the matrix:" << endl; // Step 2: Call the function to print unique rows printUniqueRows(matrix); // Another example matrix cout << "\nUnique rows for another matrix:" << endl; vector<vector<int>> matrix2 = { {1, 1}, {0, 1}, {1, 1}, {0, 0} }; printUniqueRows(matrix2); return 0; }
Output
Clear
ADVERTISEMENTS