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

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 ++向量库

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543804.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

监听文本框数据修改,特别是微信等客户端直接选择粘贴修改

2019独角兽企业重金招聘Python工程师标准>>> // 手机号码信息加载验证 $(input).bind(input propertychange, function() { initPage.checkName(); }); 转载于:https://my.oschina.net/u/1579617/blog/550488

SSIA的完整形式是什么?

SSIA&#xff1a;主题说明一切 (SSIA: Subject Says It All) SSIA is an abbreviation of "Subject Says It All". SSIA是“主题说明一切”的缩写。 It is an expression, which is commonly used in the Gmail platform. It is written in the subject of the mail…

服务器控件转换成HTML

服务器控件转换成HTML<asp:Label ID"Label1" runat"server" Text"I am label"><asp:Literal ID"Literal1" runat"server" Text"I am a literal"><asp:Panel ID"Panel1" runat"serv…

Eratosthenes筛

什么是Eratosthenes筛&#xff1f; (What is Sieve of Eratosthenes?) Sieve of Eratosthenes is an ancient algorithm of finding prime numbers for any given range. Its actually about maintaining a Boolean table to check for corresponding prime no. Eratosthenes的…

微信iOS多设备多字体适配方案总结

一、背景 2014下半年&#xff0c;微信iOS版先后适配iPad, iPhone6/6plus。随着这些大屏设备的登场&#xff0c;部分用户觉得微信的字体太小&#xff0c;但也有很多用户不喜欢太大的字体。为了满足不同用户的需求&#xff0c;我们做了全局字体设置功能&#xff0c;在【设置-通用…

python矩阵中插入矩阵_Python | 矩阵的痕迹

python矩阵中插入矩阵The sum of diagonal elements of a matrix is commonly known as the trace of the matrix. It is mainly used in eigenvalues and other matrix applications. In this article, we are going to find the trace of a matrix using inbuilt function nu…

TOP命令监视系统任务及掩码umask的作用

top 命令使用方法及參数。 top 选择參数 參数&#xff1a; -b 以批量模式执行。但不能接受命令行输入&#xff1b;-c 显示命令行&#xff0c;而不不过命令名。-d N 显示两次刷新时间的间隔&#xff0c;比方 -d 5&#xff0c;表示两次刷新间隔为5秒&#xff1b;-i 禁止显示空暇…

python点线图_Python | 点线图

python点线图A mixture of dot and line plot is called a Dot-Line plot. Each dot is connected through a line and it is the next version of the line plot. It maintains the discrete property of the points and also represents the correlation between consecutive…

Android Studio导入工程的正确姿势

为什么80%的码农都做不了架构师&#xff1f;>>> 如果你有很好的网络环境 好的网络环境&#xff0c;这里不是指&#xff1a;我家网速带宽100M&#xff0c;电信的光纤接入。 而是&#xff1a;能翻墙。因为如果本机的gradle和将要导入的工程版本不匹配&#xff0c;Stu…

BBIAF的完整形式是什么?

BBIAF&#xff1a;回来几场 (BBIAF: Be Back In A Few) BBIAF is an abbreviation of "Be Back In A Few". BBIAF是“几回去”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, …

为什么年轻人挣得很多还是穷?北上广深挑战指数报告~

又是年底&#xff0c;又到了做选择的时候。从“激情燃烧的岁月”到“何处安放的青春”&#xff0c;逃离北上广深的口号从未停止过&#xff0c;回到北上广深的呼喊更是一浪接着一浪。应届生们奔波忙碌&#xff0c;想有一份承载自己梦想的工作&#xff0c;想在异乡有一处安身之所…

apple组织名称是什么_什么是Apple Macintosh?

apple组织名称是什么苹果Macintosh (Apple Macintosh) Steve Jobs and Steve Wozniak has founded the line of computers in the year 1984, on the date 24th January, named it Apple Macintosh. Macintosh is shortly abbreviated as Mac. In this, various versions of co…

什么是Apple Desktop Bus? 亚行代表什么?

亚行&#xff1a;Apple桌面总线 (ADB: Apple Desktop Bus) ADB is an abbreviation of "Apple Desktop Bus". ADB是“ Apple Desktop Bus”的缩写 。 It is a low-speed proprietary bit-serial peripheral bus connecting devices to computers. In 1986, it was l…

CentOS 创建SVN 服务器,并且自动同步到WEB 目录

CentOS 创建SVN 服务器&#xff0c;并且自动同步到WEB 目录 标签&#xff1a; centossvnsubversion服务器2013-12-06 10:09 5492人阅读 评论(0) 收藏 举报分类&#xff1a;linux&#xff08;5&#xff09; 一、安装Subversion #yum install subversion二&#xff0c;基本的SVN服…

TTYL的完整形式是什么?

TTYL&#xff1a;稍后再与您交谈 (TTYL: Talk To You Later) TTYL is an abbreviation of Talk To You Later. It is an internet slang that is most generally used in text messaging, instant messaging, and chatting on Facebook, Twitter, WhatsApp, etc. The acronym i…

zhilizhili-ui 2016始动 开始做个样例站吧 (一)

对 我又挖坑了 不过其实也不算挖坑 因为ui构建中就会有填坑的文章 之前一直在写《编写大型web页面 结合现有前端形势思考未来前端》这是一篇巨难写的文章 估计要到年中才能写好了 写作的过程中 发生了国内前端大撕逼 2015的尾声大战 是否可以宣告前端是否开始新的时代 2016年 国…

python 网格_Python | 网格到情节

python 网格Most of the time, we need good accuracy in data visualization and a normal plot can be ambiguous. So, it is better to use a grid that allows us to locate the approximate value near the points in the plot. It helps in reducing the ambiguity and t…

2016年1月计划

开始试着每月做计划和总结&#xff0c;有节奏的规划自己的时间&#xff0c;一月计划&#xff1a; 1、hive那本书拖了很久了&#xff0c;一月一定会看完。 2、因为跟着阚爷的风准备试着做一下讲师&#xff0c;分配给我的是推荐这块&#xff0c;所以网上多找找做推荐的资源&#…

slr1文法_SLR的完整形式是什么?

slr1文法单反&#xff1a;单镜头反光 (SLR: Single Lens Reflex) SLR is an abbreviation of Single Lens Reflex. It is used in high standard cameras. SLR makes use of an automatic moving mirror arrangement that makes it possible for photographers to perceive pre…

vim快捷键2

一、移动光标 1、左移h、右移l、下移j、上移k 2、向下翻页ctrl f&#xff0c;向上翻页ctrl b 3、向下翻半页ctrl d&#xff0c;向上翻半页ctrl u 4、移动到行尾$&#xff0c;移动到行首0&#xff08;数字&#xff09;&#xff0c;移动到行首第一个字符处^ 5、移动光标到下一…