我有一个STL向量的列表,我想按照每个向量的第一个元素对它们进行排序
I have a list of STL vectors and I want to sort them by the first element of each vector
我有一个来自STL的向量列表。它们有不同的长度,我想根据每个向量的第一个元素对它们进行排序。例如,我有向量:445 6 10、18、223和317 9。第一个数字是每个向量的大小,后面跟着向量的元素。我想在文件中显示矢量,按第一个元素排序。在本例中,我的矢量应按以下顺序显示:317 9、223、445 6 10和18。
这是我的代码:
ofstream fout ("retele.out");
fout << T << 'n';
for (i=1; i<=T; i++)
{
fout << sol[i].size() << ' ';
sort(sol[i].begin(),sol[i].end());
for (j=0; j<sol[i].size(); j++)
fout << sol[i][j] << ' ';
fout << 'n';
}
fout.close();
"T"是矢量的数量。"排序"函数用于对每个向量进行排序,我还需要另一个排序来对向量进行排序。
其他答案的一个小说明:std::vector<T>
已经有了与您描述的行为完全相同的operator<
(cplusplus描述(。
template < class T, class Alloc > bool operator < (const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs);
描述:
小于比较(运算符<以对等的方式(即检查a<b和b<a(并且在第一次出现时停止。
所以,你可以写
sort(sol.begin(), sol.end());
UP:注释中写道,您有一个std::vector
数组,而不是std::list<std::vector>
数组。该数组没有方法begin()
和end()
,因此这个确切的代码将不起作用。解决方案是
sort(sol, sol + SIZE);
其中SIZE
是要排序的数组部分的大小。
基于此链接的答案:
你可以创建这样的比较方法:
bool compare_vec (const std::vector<int>& first, const std::vector<int>& second)
{
return first[1]<second[1];
}
并呼叫:
sol.sort(compare_vec);
查看这篇文章。
bool myfunction (vector<int> i,vector<int> j) { return (i[0]<j[0]);}
struct myclass {
bool operator() (vector<int> i,vector<int> j) { return (i[0]<j[0]);}
} myobject;
...
ofstream fout ("retele.out");
fout << T << 'n';
for (i=1; i<=T; i++)
{
fout << sol[i].size() << ' ';
sort(sol[i].begin(),sol[i].end(), myfunction);
for (j=0; j<sol[i].size(); j++)
fout << sol[i][j] << ' ';
fout << 'n';
}
fout.close();
您必须实现自己的比较器,稍后将使用它对vectors
列表进行排序。
您可以遵循以下算法:
// sort algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::sort
#include <vector> // std::vector
bool myfunction (int i,int j) { return (i<j); }
struct myclass {
bool operator() (int i,int j) { return (i<j);}
} myobject;
int main () {
int myints[] = {32,71,12,45,26,80,53,33};
std::vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33
// using default comparison (operator <):
std::sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33
// using function as comp
std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
// using object as comp
std::sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80)
// print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << 'n';
return 0;
}
std::sort(MyVector.begin(), MyVector.end(), [](auto a, auto b) { return a.front() < b.front(); } );
我希望这能对你的向量进行排序
std::sort
包含一个可选谓词。在这种情况下,我想你会想写你的排序:
std::sort(sol.begin(), sol.end(), [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); });
请注意,如果列表中包含空向量,则此代码将崩溃。
如果sol不是std::list
,尽管注释中指出了类似于c样式的数组std::vector<int> v[50001]
。应该重写为:
std::sort(sol[0], sol[nofElementsFilledIn], [](const std::vector<int> &lhs, const std::vector<int> &rhs) { return lhs.front() < rhs.front(); });
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 在 C++ 中输出枚举类类型的向量元素
- 用向量的向量元素初始化向量的空向量
- 为什么不允许使用可变长度数组作为向量元素?
- 在函数中传递向量元素
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 返回向量元素的 l 值的正确方法是什么?
- 当向量增长时,指向向量元素的C++指针是否无效
- 如何在C++中从一个向量元素减去std::unorderede_map向量元素并更新它
- C++:向量元素赋值导致访问冲突
- 将向量元素与字符串元素进行比较,而不初始化向量
- 从2D矢量中找出最小尺寸的向量元素的更好方法
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 使用递归C++向量元素的总和
- 如何查找为数组中的最低值编制索引的向量元素
- 限制指针访问STL ::向量元素
- 访问索引指向向量元素时的seg故障 - 做错了什么
- 如何使用指针访问向量元素的各个元素
- 向量元素数据损坏了Find()操作
- 将字符值分配给向量元素.C