我正在尝试排序字符串向量的向量,但我不明白如何创建比较器函数。
我看到这个线程但无法实现我的情况: 在C ++中排序字符串向量的向量
所以我有一个字符串向量的向量看起来像这样: 你好,世界,1,3,4,7,2,1 世界,你好,1,4,8,4,2,1 手机,鼠标,2,3,5,2,1,4
我需要按照用户指定的列对矢量字符串向量进行排序。 我的用户可以指定多个列进行排序。 让我们说第3列和第5列。第3列(1,1,2)对第1行和第2行具有相同的值,然后我们必须按第5列排序。为了不使事情复杂化,这都是按升序排列的。
当你把它传递给比较器函数时,我不明白它是如何工作的概念。 我的函数如何在这些线程中的人员发布的示例中循环?
无论如何,提前谢谢!
I am trying to sort a vector of vector of strings but I don't understand how to create a comparator functions.
I saw this thread but could not implement into my situation: sorting vector of vector of strings in C++
so I have a vector of vector of strings looking like this: hello, world, 1, 3, 4, 7, 2, 1 world, hello, 1, 4, 8, 4 ,2, 1 phone, mouse, 2, 3, 5, 2, 1, 4
I need to sort this vector of vector string by columns that my user specifies. My user can specify more than one column to sort. Let say column 3 and 5. Column 3(1,1,2) has same value for row 1 and 2, then we have to sort by column 5. To not complicate things, this is all by ascending order.
I don't understand the concept of how it works when you pass it to the comparator function. How does my function gets looped inside the example posted by the people in those threads?
Anyways thanks in advance!
最满意答案
您可以使用std::sort来对矢量进行排序,并定义一个自定义比较器函数 (即具有重载operator() )。
您可以将排序列的索引存储在std::vector (它将成为自定义比较器对象的“状态”的一部分),并比较索引存储在该向量中的列的字符串。
您开始比较“排序列”向量中第一个索引中指定的列的值; 如果它们相同,则继续比较向量中下一个索引中指定的列的值,等等。这可以在比较器的operator()重载体内的for循环内完成。
请参阅以下代码作为示例(使用g ++(GCC)4.7.2编译):
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<string>> BuildTestData() { vector<string> r1 = {"hello", "world", "1", "3", "4", "7", "2", "1"}; vector<string> r2 = {"world", "hello", "1", "4", "8", "4", "2", "1"}; vector<string> r3 = {"phone", "mouse", "2", "3", "5", "2", "1", "4"}; return vector<vector<string>>{r1, r2, r3}; } void PrintData(const vector<vector<string>> & v) { for (size_t r = 0; r < v.size(); r++) { for (size_t c = 0; c < v[r].size(); c++) cout << v[r][c] << ' '; cout << '\n'; } } class StringListComparator { public: explicit StringListComparator(vector<int> sortColumns) : m_sortColumns( move(sortColumns) ) { } bool operator()(const vector<string>& lhs, const vector<string>& rhs) const { // For each sorting column: for (size_t i = 0; i < m_sortColumns.size(); i++) { // Comparison with current column const int currentColumn = m_sortColumns[i]; if (lhs[currentColumn] < rhs[currentColumn]) return true; if (lhs[currentColumn] > rhs[currentColumn]) return false; // lhs[currentColumn] == rhs[currentColumn], // so check with next sorting column } return false; } private: vector<int> m_sortColumns; }; int main() { auto v = BuildTestData(); cout << "Before sorting:\n"; PrintData(v); vector<int> sortColumns = {5, 7}; // indexes are 0-based sort(v.begin(), v.end(), StringListComparator(sortColumns)); cout << "\nAfter sort:\n"; PrintData(v); }样品运行:
Before sorting: hello world 1 3 4 7 2 1 world hello 1 4 8 4 2 1 phone mouse 2 3 5 2 1 4 After sort: phone mouse 2 3 5 2 1 4 world hello 1 4 8 4 2 1 hello world 1 3 4 7 2 1
You can just use std::sort to sort the vector, and define a custom comparator functor (i.e. class with overloaded operator()).
You can store the indexes of sorting columns in a std::vector (that will be part of the "state" of the custom comparator object), and compare strings from columns whose indexes are stored in that vector.
You start comparing values at column specified in the first index in the "sorting columns" vector; if they are the same, you continue comparing values at column specified in the next index in the vector, etc. This can be done inside a for loop inside the body of comparator's operator() overload.
See the following code as an example (compiled with g++ (GCC) 4.7.2):
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<string>> BuildTestData() { vector<string> r1 = {"hello", "world", "1", "3", "4", "7", "2", "1"}; vector<string> r2 = {"world", "hello", "1", "4", "8", "4", "2", "1"}; vector<string> r3 = {"phone", "mouse", "2", "3", "5", "2", "1", "4"}; return vector<vector<string>>{r1, r2, r3}; } void PrintData(const vector<vector<string>> & v) { for (size_t r = 0; r < v.size(); r++) { for (size_t c = 0; c < v[r].size(); c++) cout << v[r][c] << ' '; cout << '\n'; } } class StringListComparator { public: explicit StringListComparator(vector<int> sortColumns) : m_sortColumns( move(sortColumns) ) { } bool operator()(const vector<string>& lhs, const vector<string>& rhs) const { // For each sorting column: for (size_t i = 0; i < m_sortColumns.size(); i++) { // Comparison with current column const int currentColumn = m_sortColumns[i]; if (lhs[currentColumn] < rhs[currentColumn]) return true; if (lhs[currentColumn] > rhs[currentColumn]) return false; // lhs[currentColumn] == rhs[currentColumn], // so check with next sorting column } return false; } private: vector<int> m_sortColumns; }; int main() { auto v = BuildTestData(); cout << "Before sorting:\n"; PrintData(v); vector<int> sortColumns = {5, 7}; // indexes are 0-based sort(v.begin(), v.end(), StringListComparator(sortColumns)); cout << "\nAfter sort:\n"; PrintData(v); }Sample run:
Before sorting: hello world 1 3 4 7 2 1 world hello 1 4 8 4 2 1 phone mouse 2 3 5 2 1 4 After sort: phone mouse 2 3 5 2 1 4 world hello 1 4 8 4 2 1 hello world 1 3 4 7 2 1C ++用比较器对字符串向量的矢量进行排序。(C++ Sort a vector of vector of strings with comparator. Please help me understand)
我正在尝试排序字符串向量的向量,但我不明白如何创建比较器函数。
我看到这个线程但无法实现我的情况: 在C ++中排序字符串向量的向量
所以我有一个字符串向量的向量看起来像这样: 你好,世界,1,3,4,7,2,1 世界,你好,1,4,8,4,2,1 手机,鼠标,2,3,5,2,1,4
我需要按照用户指定的列对矢量字符串向量进行排序。 我的用户可以指定多个列进行排序。 让我们说第3列和第5列。第3列(1,1,2)对第1行和第2行具有相同的值,然后我们必须按第5列排序。为了不使事情复杂化,这都是按升序排列的。
当你把它传递给比较器函数时,我不明白它是如何工作的概念。 我的函数如何在这些线程中的人员发布的示例中循环?
无论如何,提前谢谢!
I am trying to sort a vector of vector of strings but I don't understand how to create a comparator functions.
I saw this thread but could not implement into my situation: sorting vector of vector of strings in C++
so I have a vector of vector of strings looking like this: hello, world, 1, 3, 4, 7, 2, 1 world, hello, 1, 4, 8, 4 ,2, 1 phone, mouse, 2, 3, 5, 2, 1, 4
I need to sort this vector of vector string by columns that my user specifies. My user can specify more than one column to sort. Let say column 3 and 5. Column 3(1,1,2) has same value for row 1 and 2, then we have to sort by column 5. To not complicate things, this is all by ascending order.
I don't understand the concept of how it works when you pass it to the comparator function. How does my function gets looped inside the example posted by the people in those threads?
Anyways thanks in advance!
最满意答案
您可以使用std::sort来对矢量进行排序,并定义一个自定义比较器函数 (即具有重载operator() )。
您可以将排序列的索引存储在std::vector (它将成为自定义比较器对象的“状态”的一部分),并比较索引存储在该向量中的列的字符串。
您开始比较“排序列”向量中第一个索引中指定的列的值; 如果它们相同,则继续比较向量中下一个索引中指定的列的值,等等。这可以在比较器的operator()重载体内的for循环内完成。
请参阅以下代码作为示例(使用g ++(GCC)4.7.2编译):
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<string>> BuildTestData() { vector<string> r1 = {"hello", "world", "1", "3", "4", "7", "2", "1"}; vector<string> r2 = {"world", "hello", "1", "4", "8", "4", "2", "1"}; vector<string> r3 = {"phone", "mouse", "2", "3", "5", "2", "1", "4"}; return vector<vector<string>>{r1, r2, r3}; } void PrintData(const vector<vector<string>> & v) { for (size_t r = 0; r < v.size(); r++) { for (size_t c = 0; c < v[r].size(); c++) cout << v[r][c] << ' '; cout << '\n'; } } class StringListComparator { public: explicit StringListComparator(vector<int> sortColumns) : m_sortColumns( move(sortColumns) ) { } bool operator()(const vector<string>& lhs, const vector<string>& rhs) const { // For each sorting column: for (size_t i = 0; i < m_sortColumns.size(); i++) { // Comparison with current column const int currentColumn = m_sortColumns[i]; if (lhs[currentColumn] < rhs[currentColumn]) return true; if (lhs[currentColumn] > rhs[currentColumn]) return false; // lhs[currentColumn] == rhs[currentColumn], // so check with next sorting column } return false; } private: vector<int> m_sortColumns; }; int main() { auto v = BuildTestData(); cout << "Before sorting:\n"; PrintData(v); vector<int> sortColumns = {5, 7}; // indexes are 0-based sort(v.begin(), v.end(), StringListComparator(sortColumns)); cout << "\nAfter sort:\n"; PrintData(v); }样品运行:
Before sorting: hello world 1 3 4 7 2 1 world hello 1 4 8 4 2 1 phone mouse 2 3 5 2 1 4 After sort: phone mouse 2 3 5 2 1 4 world hello 1 4 8 4 2 1 hello world 1 3 4 7 2 1
You can just use std::sort to sort the vector, and define a custom comparator functor (i.e. class with overloaded operator()).
You can store the indexes of sorting columns in a std::vector (that will be part of the "state" of the custom comparator object), and compare strings from columns whose indexes are stored in that vector.
You start comparing values at column specified in the first index in the "sorting columns" vector; if they are the same, you continue comparing values at column specified in the next index in the vector, etc. This can be done inside a for loop inside the body of comparator's operator() overload.
See the following code as an example (compiled with g++ (GCC) 4.7.2):
#include <algorithm> #include <iostream> #include <string> #include <vector> using namespace std; vector<vector<string>> BuildTestData() { vector<string> r1 = {"hello", "world", "1", "3", "4", "7", "2", "1"}; vector<string> r2 = {"world", "hello", "1", "4", "8", "4", "2", "1"}; vector<string> r3 = {"phone", "mouse", "2", "3", "5", "2", "1", "4"}; return vector<vector<string>>{r1, r2, r3}; } void PrintData(const vector<vector<string>> & v) { for (size_t r = 0; r < v.size(); r++) { for (size_t c = 0; c < v[r].size(); c++) cout << v[r][c] << ' '; cout << '\n'; } } class StringListComparator { public: explicit StringListComparator(vector<int> sortColumns) : m_sortColumns( move(sortColumns) ) { } bool operator()(const vector<string>& lhs, const vector<string>& rhs) const { // For each sorting column: for (size_t i = 0; i < m_sortColumns.size(); i++) { // Comparison with current column const int currentColumn = m_sortColumns[i]; if (lhs[currentColumn] < rhs[currentColumn]) return true; if (lhs[currentColumn] > rhs[currentColumn]) return false; // lhs[currentColumn] == rhs[currentColumn], // so check with next sorting column } return false; } private: vector<int> m_sortColumns; }; int main() { auto v = BuildTestData(); cout << "Before sorting:\n"; PrintData(v); vector<int> sortColumns = {5, 7}; // indexes are 0-based sort(v.begin(), v.end(), StringListComparator(sortColumns)); cout << "\nAfter sort:\n"; PrintData(v); }Sample run:
Before sorting: hello world 1 3 4 7 2 1 world hello 1 4 8 4 2 1 phone mouse 2 3 5 2 1 4 After sort: phone mouse 2 3 5 2 1 4 world hello 1 4 8 4 2 1 hello world 1 3 4 7 2 1
发布评论