c ++向量库

As per as a 2D vector is concerned it's a vector of a 1D vector. But what we do in sorting a 1D vector like,

就2D向量而言,它是1D向量的向量 。 但是我们在对一维向量进行排序时所做的工作

sort(vector.begin(),vector.end());

We can't do the same for a 2D vector without any user-defined comparator function, as it will merely sort based on the first element of each column.

没有任何用户定义的比较器函数,我们无法对2D向量执行相同的操作,因为它只会根据每列的第一个元素进行排序。

But we can sort 2D vector based on use cases:

但是我们可以根据用例对2D向量进行排序:

1)根据特定的行排序 (1) Sort based on a particular row)

The below example sorts a particular row in the 2D vector.

下面的示例对2D向量中的特定行进行排序。

Say for example, the 2D matrix is:

例如,二维矩阵为:

[[3, 5, 4],
[6, 4, 2],
[1, 7, 3]]

So if we sort the first row in ascending order the output will be:

因此,如果我们以升序对第一行进行排序,则输出将是:

[[3, 4, 5],
[6, 4, 2],
[1, 7, 3]]

#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the first row of the 2D vector
//so, basically we sort the 1D array(the first row)
sort(two_D_vector[0].begin(), two_D_vector[0].end());
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}

Output:

输出:

printing the 2D vector before sorting
3 5 4
6 4 2
1 7 3
printing the 2D vector after sorting
3 4 5
6 4 2
1 7 3

So if we sort the first row in descending order the output will be:

因此,如果我们以降序对第一行进行排序,则输出将是:

[[3, 5, 4],
[6, 4, 2],
[7, 3, 1]]

#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector[2].begin(), two_D_vector[2].end(), greater<int>());
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}

Output:

输出:

printing the 2D vector before sorting
3 5 4
6 4 2
1 7 3
printing the 2D vector after sorting
3 5 4
6 4 2
7 3 1

2)根据特定的列排序 (2) Sort based on a particular column)

The below example sorts a particular column in the 2D vector.

下面的示例对2D向量中的特定列进行排序。

Say for example, the 2D matrix is:

例如,二维矩阵为:

[[3, 5, 4],
[6, 4, 2],
[1, 7, 3]]

So if we sort the first column in ascending order the output will be:

因此,如果我们以升序对第一列进行排序,则输出将是:

[[1, 4, 5],
[3, 4, 2],
[6, 7, 3]]

Here we need to define our user-defined comparator function to do the above thing. Like we will take each element of the 2D vector (which is a 1D vector, to be specific each row) and compare based on the first element (or any particular element) only. That's why we need a user-defined comparator.

在这里,我们需要定义用户定义的比较器函数来完成上述操作。 就像我们将采用2D向量的每个元素(这是1D向量,每一行都是特定的)并仅基于第一个元素(或任何特定元素)进行比较。 这就是为什么我们需要一个用户定义的比较器 。

#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first element of first
//row<first element of second row
if (A[0] < B[0])
return true; //no swap
//other wise swap the rows
return false;
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}

Output:

输出:

printing the 2D vector before sorting
3 5 4
6 4 2
1 7 3
printing the 2D vector after sorting
1 7 3
3 5 4
6 4 2

To sort in descending order, we need to just change the comparator function.

要按降序排序,我们只需要更改比较器功能即可。

#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
//to sort based on column in descending order
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first element of first
//row<first element of second row
if (A[0] < B[0])
return false; //swap the rows
//other wise  no swap
return true;
}
int main()
{
//2D vector initialized with
//user-defined elements only
vector<vector<int> > two_D_vector{
{ 3, 5, 4 },
{ 6, 4, 2 },
{ 1, 7, 3 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}

Output:

输出:

printing the 2D vector before sorting
3 5 4
6 4 2
1 7 3
printing the 2D vector after sorting
6 4 2
3 5 4
1 7 3

There can be various use cases to sort a 2D vector and we need to write our comparator functions.

可以使用各种用例对2D向量进行排序,我们需要编写比较器函数。

Exercises

练习题

(a) Sort based on row sizes in ascending order

(a)根据行大小升序排序

Say the 2D vector is
{
{2, 3, 4, 5},
{3, 4, 1},
{1}}
After sorting the 2D vector based on
row size in ascending order:
{
{1},
{3, 4, 1},
{2, 3, 4, 5}
}

Here we need to use our user define a function which will swap the rows as per their sizes.

在这里,我们需要使用用户定义的函数,该函数将根据行的大小交换行。

#include <bits/stdc++.h>
using namespace std;
void print(vector<vector<int> > two_D_vector)
{
for (auto it : two_D_vector) {
//it is now an 1D vector
for (auto ij : it) {
cout << ij << " ";
}
cout << endl;
}
}
//to sort based on column in descending order
bool mycomp(vector<int>& A, vector<int>& B)
{
//if first row size>second row size
if (A.size() > B.size())
return false; //swap the rows
//other wise  no swap
return true;
}
int main()
{
//2D vector initialized with
//use- defined elements only
vector<vector<int> > two_D_vector{
{ 2, 3, 4, 5 },
{ 3, 4, 1 },
{ 1 }
};
//printing the 2D vector
cout << "printing the 2D vector before sorting\n";
print(two_D_vector);
//sorting the 2D array based on a particular row
//here we sort the last row of the 2D vector
//in descending order
//so, basically we sort the 1D array in
//descending order(the last row)
sort(two_D_vector.begin(), two_D_vector.end(), mycomp);
//print the 2D vector
cout << "printing the 2D vector after sorting\n";
print(two_D_vector);
return 0;
}

Output:

输出:

printing the 2D vector before sorting
2 3 4 5
3 4 1
1
printing the 2D vector after sorting
1
3 4 1
2 3 4 5

(b) Can you sort based on column size anyway?

(b)仍然可以根据列大小进行排序吗?

If you can't sort in that way, then do comment why you cant?

如果您无法通过这种方式进行排序,那么请评论一下您为什么不能这样做?

翻译自: https://www.includehelp.com/stl/sort-a-2d-vector-in-cpp.aspx

c ++向量库

c ++向量库_在C ++中对2D向量进行排序相关推荐

  1. c ++向量库_C ++中的2D向量–实用指南2D向量

    c ++向量库 Also referred to as vector of vectors, 2D vectors in C++ form the basis of creating matrices ...

  2. c ++向量库_将向量复制到C ++中的另一个向量

    c ++向量库 The ways that we are using to copy vectors in C++, are: 我们用于在C ++中复制向量的方法是: Copy one vector' ...

  3. stl向量_用户定义大小的C ++ STL中的2D向量

    stl向量 C ++ STL中的2D矢量 (2D Vector in C++ STL) In C++ STL, a 2D vector is a vector of vector. 在C ++ STL ...

  4. c++中int向量初始化_以不同的方式在C ++中初始化2D向量

    c++中int向量初始化 Prerequisite: Initialize 1D vector 先决条件: 初始化一维向量 Before discussing about the initializa ...

  5. python中对列表排序_在Python中对嵌套列表进行排序和分组

    在Python中对嵌套列表进行排序和分组 我具有以下数据结构(列表列表) [ ['4', '21', '1', '14', '2008-10-24 15:42:58'], ['3', '22', '4 ...

  6. 词向量与词向量拼接_中文字词向量和方面词向量联合嵌入情感分析方法与流程...

    本发明涉及一种中文字词向量和方面词向量联合嵌入CNN-LSTM情感分析方法. 背景技术: 近年来,越来越多的用户习惯在网络上发自己对某一事物的看法与评论.如何快速,准确地从互联网海量评论信息中分析所包 ...

  7. python二维向量运算模拟_python二维向量运算_[VB.NET][C#]二维向量的基本运算

    前言 在数学中,几何向量是指具有大小和方向的几何对象. 在编程中,向量有着广泛的应用,其作用在图形编程和游戏物理引擎方面尤为突出. 第一节 构造函数 通过创建一个二维向量的类(或结构体),实现向量的表 ...

  8. dx绘制2d图像_在DirectX 中进行2D渲染

    http://flcstudio.blog.163.com/blog/static/756035392008115111123672/ 最近,我看到很多关于DirectX8在最新的API中摒弃Dire ...

  9. python3库_对python3中pathlib库的Path类的使用详解

    用了很久的os.path,今天发现竟然还有这么好用的库,记录下来以便使用. 1.调用库 from pathlib import 2.创建Path对象 p = Path('D:/python/1.py' ...

最新文章

  1. git切换ssh和http协议
  2. 字符设备驱动笔记(一)
  3. 为什么batchnormalize 有效
  4. 安装慢_Origin平台安装更新慢的解决办法
  5. 数据oem 操作手册_海口电销外呼系统oem-河南爱聊科技
  6. 多线程的创建方式---继承Thread和实现Runnable
  7. UMEditor调整文本编辑器的组件位置的方法
  8. Abp连接多个数据源
  9. 斐波那契数列(复习)
  10. Linux系统瓶颈分析(经典)
  11. android 查看某一个apk签名,APK提取获取签名
  12. IGBT的绘制与逆变器的绘制-Visio制图总结【电控类】(三)
  13. 微信小程序中使用tabBar
  14. 网吧带宽很大,为什么三层更新下载游戏很慢?
  15. 如果生活将我们拆散了
  16. STM32F4驱动LTC2664-16驱动程序
  17. 【UEFI实战】Intel开源固件项目
  18. NanoHttp的使用入门
  19. javaweb网页上传图片并显示在页面上,并在服务端存到磁盘(base64编码解码)
  20. OSP 使用异步发送

热门文章

  1. kali linux 截图位置,Kali Linux中使用shutter截图工具 | CN-SEC 中文网
  2. 缓存雪崩、击穿、穿透解决方案
  3. HttpServletResponse应用 的 简单介绍
  4. UVA1225 ​​​​​​​Digit Counting
  5. 重新学习Ubuntu -- 截图软件的选择和安装
  6. 2017软件工程实践
  7. LCD显示屏原理与应用
  8. TYVJ P1030 乳草的入侵 Label:跳马问题
  9. 网管必须了解的理光复印机相关故障现相之一
  10. python解释器环境中用于表示上一次运算结果的特殊变量_判断正误 PUSH CL_学小易找答案...