c++筛选方法实现与性能优化实例
实现 task1() 这个函数,
它把 rows 中所有满足 b >= 10 && b < 50 并且 a == 1000 || a == 2000 || a == 3000 的行的内容都打印到终端
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct Row
{
int a;
int b;
} Row;
/*!
* Task 1.
*
* Find out all the rows that sastify below conditions:
*
* ((b >= 10 && b < 50) &&
* (a == 1000 || a == 2000 || a == 3000))
*
* Print them to the terminal, one row per line, for example:
*
* 1000,20
* 1000,23
* 2000,16
*
* @param nrows The total number of rows.
* @param rows The rows, for example rows[0] is the first row.
*/
//任务1
/*
实现 task1() 这个函数,
它把 rows 中所有满足 b >= 10 && b < 50 并且 a == 1000 || a == 2000 || a == 3000 的行的内容都打印到终端
*/
void task1(const Row *rows, int nrows)
{
if (rows == NULL)
{
return;
}
for (int i = 0; i < nrows; ++i)
{
if ((rows[i].a == 1000 || rows[i].a == 2000 || rows[i].a == 3000) &&
(rows[i].b >= 10 && rows[i].b < 50))
{
cout << rows[i].a << "," << rows[i].b << endl;
}
}
return;
}
//任务2
/*
在任务 1 的基础上, 如果输入的参数 rows 已经按照 (a,b) 进行过排序, 想办法对 task1() 的执行性能进行优化
*/
//借助二分查找来优化性能,时间复杂度从O(n)优化成O(logn)
//使用C++ STL中的lower_bound upper_bound
void task1(const Row *rows, int nrows)
{
if (rows == NULL)
{
return;
}
//a==1000 && b>=10 && b<50
Row tmp;
tmp.a = 1000;
int start = lower_bound(rows, rows + nrows, tmp, [](const Row &row1, const Row &row2)
{ return row1.a < row2.a; }) -
rows;
int end = upper_bound(rows, rows + nrows, tmp, [](const Row &row1, const Row &row2)
{ return row1.a < row2.a; }) -
rows;
tmp.b = 10;
int left = lower_bound(rows + start, rows + end, tmp, [](const Row &row1, const Row &row2)
{ return row1.b < row2.b; }) -
(rows + start);
tmp.b = 50;
int right = lower_bound(rows + start, rows + end, tmp, [](const Row &row1, const Row &row2)
{ return row1.b < row2.b; }) -
(rows + start);
for (int i = start + left; i < start + right; ++i)
{
cout << rows[i].a << "," << rows[i].b << endl;
}
//a==2000 && b>=10 && b<50
tmp.a = 2000;
start = lower_bound(rows, rows + nrows, tmp, [](const Row &row1, const Row &row2)
{ return row1.a < row2.a; }) -
rows;
end = upper_bound(rows, rows + nrows, tmp, [](const Row &row1, const Row &row2)
{ return row1.a < row2.a; }) -
rows;
tmp.b = 10;
left = lower_bound(rows + start, rows + end, tmp, [](const Row &row1, const Row &row2)
{ return row1.b < row2.b; }) -
(rows + start);
tmp.b = 50;
right = lower_bound(rows + start, rows + end, tmp, [](const Row &row1, const Row &row2)
{ return row1.b < row2.b; }) -
(rows + start);
for (int i = start + left; i < start + right; ++i)
{
cout << rows[i].a << "," << rows[i].b << endl;
}
//a==3000 && b>=10 && b<50
tmp.a = 3000;
start = lower_bound(rows, rows + nrows, tmp, [](const Row &row1, const Row &row2)
{ return row1.a < row2.a; }) -
rows;
end = upper_bound(rows, rows + nrows, tmp, [](const Row &row1, const Row &row2)
{ return row1.a < row2.a; }) -
rows;
tmp.b = 10;
left = lower_bound(rows + start, rows + end, tmp, [](const Row &row1, const Row &row2)
{ return row1.b < row2.b; }) -
(rows + start);
tmp.b = 50;
right = lower_bound(rows + start, rows + end, tmp, [](const Row &row1, const Row &row2)
{ return row1.b < row2.b; }) -
(rows + start);
for (int i = start + left; i < start + right; ++i)
{
cout << rows[i].a << "," << rows[i].b << endl;
}
return;
}
//任务3
/*
在任务 2 的基础上, 我们期望你打印出的匹配行是按照 b 列进行排序的, 请尽量优化函数的执行性能. 示例输出:
1000,10
2000,10
2000,10
1000,11
2000,12
*/
//任务4
/*
思考任务, 不需要实现. 基于任务 3, 如果 a 列上的匹配条件不只是1000,2000,3000, 而是扩充成 1000,2000,3000,...,98000,99000, 你在任务 3中进行的实现是否足够优化, 是否有更好的优化手段
*/
``