C++ 向量的排序向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14419520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sort vector of vectors
提问by pawel
I have
我有
vector<vector<int>> vec
in my c++ app.
在我的 C++ 应用程序中。
Every vector of integers as an element of "big" vector has 4 INT values. I want to sort vec basing on third value of it's content vectors of ints (I mean every "inside" vector third element) - is it possible?
作为“大”向量元素的每个整数向量都有 4 个 INT 值。我想根据它的整数内容向量的第三个值(我的意思是每个“内部”向量第三个元素)对 vec 进行排序 - 可能吗?
EDIT
编辑
Let's say I've got a function
假设我有一个功能
COST(vector<int>)
which calculates me some value based on my vector values - can I use it in comparation parameter too? It'd help me a lot more.
它根据我的向量值计算出一些值 - 我也可以在比较参数中使用它吗?它会帮助我更多。
回答by Joseph Mansfield
Sure it is. std::sort
can take a third parameter which is the comparison function to use when sorting. For example, you could use a lambda function:
就是这样。std::sort
可以采用第三个参数,它是排序时使用的比较函数。例如,您可以使用 lambda 函数:
std::vector<std::vector<int>> vec;
// Fill it
std::sort(vec.begin(), vec.end(),
[](const std::vector<int>& a, const std::vector<int>& b) {
return a[2] < b[2];
});
Alternatively, you can pass anything else callable with signature bool(const std::vector<int>&, const std::vector<int>&)
, such as a functor or function pointer.
或者,您可以传递任何其他可使用签名调用的东西bool(const std::vector<int>&, const std::vector<int>&)
,例如函子或函数指针。
Response to edit: Simply apply your COST
function to a
and b
:
对编辑的回应:只需将您的COST
功能应用于a
和b
:
std::sort(vec.begin(), vec.end(),
[](const std::vector<int>& a, const std::vector<int>& b) {
return COST(a) < COST(b);
});
回答by Ulrich Eckhardt
If you want to compare the two vectors by cost, try this:
如果要按成本比较两个向量,请尝试以下操作:
bool predicate(const std::vector<int>& a, const std::vector<int>& b)
{
return COST(a) < COST(b);
}
Notes:
笔记:
- The above works with C++98, too, I'm not sure about how widespread the use of C++11 is and whether you have a compliant compiler. Otherwise, you can of course use a lambda expression, too, as sftrabbit suggested.
- You don't say what COST returns, I simply assumed some sortable value like float or long.
- I hope you don't copy the vector when passing it to COST(), that would be horribly inefficient.
- COST suggests a macro, like all UPPERCASE_NAMES. Don't use macros. Don't use macro names for functions.
- 以上也适用于 C++98,我不确定 C++11 的使用有多广泛,以及您是否有兼容的编译器。否则,您当然也可以按照 sftrabbit 的建议使用 lambda 表达式。
- 你没有说 COST 返回什么,我只是假设了一些可排序的值,比如 float 或 long。
- 我希望您在将向量传递给 COST() 时不要复制它,这会非常低效。
- COST 建议一个宏,就像所有 UPPERCASE_NAMES 一样。不要使用宏。不要对函数使用宏名称。
回答by borgomeister
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
using namespace std;
// This makes the sort be according to column 2 and ascending
bool sortFunc( const vector<int>& p1,
const vector<int>& p2 ) {
return p1[1] < p2[1];
}
int main() {
srand(time(NULL));
// Creates and initializes 10 x 4 vector
vector< vector<int> > vec;
for( int i=0; i<10; i++ ) {
vector<int> tmpVec;
for( int j=0; j<2; j++ ) {
tmpVec.push_back( rand()%10 );
}
vec.push_back( tmpVec );
}
// Print out the pre-sorted vector
cout << "Pre-sorting state:" << endl;
for( int i=0; i<vec.size(); i++ ) {
for( int j=0; j<vec[i].size(); j++ ) {
cout << vec[i][j] << " ";
}
cout << endl;
}
cout << endl;
// Do the sorting according to column 2
sort(vec.begin(), vec.end(), sortFunc);
// Print out the post-sorted vector
cout << "Post-sorting state:" << endl;
for( int i=0; i<vec.size(); i++ ) {
for( int j=0; j<vec[i].size(); j++ ) {
cout << vec[i][j] << " ";
}
cout << endl;
}
return 0;
}
source: https://shihho.wordpress.com/2012/11/28/sort_with_vectors/
来源:https: //shihho.wordpress.com/2012/11/28/sort_with_vectors/