STD::C++中的分区

C++在STL算法库中有一个类,它允许我们使用某些内置函数容易地分区算法。分割是指根据给定条件分割容器元素的行为。 分区操作 : 1.分区(beg、end、condition) :-此函数用于 划分元素 在…上 条件基础 在其论点中提到。 2.是否已分区(beg、end、condition) :-此函数返回布尔值 如果容器已分区,则为true 否则返回false。

null

CPP

// C++ code to demonstrate the working of
// partition() and is_partitioned()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
using namespace std;
int main()
{
// Initializing vector
vector< int > vect = { 2, 1, 5, 6, 8, 7 };
// Checking if vector is partitioned
// using is_partitioned()
is_partitioned(vect.begin(), vect.end(), []( int x)
{
return x%2==0;
})?
cout << "Vector is partitioned" :
cout << "Vector is not partitioned" ;
cout << endl;
// partitioning vector using partition()
partition(vect.begin(), vect.end(), []( int x)
{
return x%2==0;
});
// Checking if vector is partitioned
// using is_partitioned()
is_partitioned(vect.begin(), vect.end(), []( int x)
{
return x%2==0;
})?
cout << "Now, vector is partitioned after partition operation" :
cout << "Vector is still not partitioned after partition operation" ;
cout << endl;
// Displaying partitioned Vector
cout << "The partitioned vector is : " ;
for ( int &x : vect) cout << x << " " ;
return 0;
}


输出:

Vector is not partitionedNow, vector is partitioned after partition operationThe partitioned vector is : 2 8 6 5 1 7

在上面的代码中,分区函数根据元素是偶数还是奇数对向量进行分区,偶数元素与奇数元素不按特定顺序进行分区。 3.稳定分区(beg、end、condition) :-此函数用于 划分元素 在…上 条件基础 在其论点中提到 这样可以保持元素的相对顺序。 . 4.分割点(beg、end、condition) :-这个功能 返回指向分区点的迭代器 容器的,即分区范围[beg,end]中条件为非真的第一个元素。容器应该已经分区,此函数才能工作。

CPP

// C++ code to demonstrate the working of
// stable_partition() and partition_point()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
using namespace std;
int main()
{
// Initializing vector
vector< int > vect = { 2, 1, 5, 6, 8, 7 };
// partitioning vector using stable_partition()
// in sorted order
stable_partition(vect.begin(), vect.end(), []( int x)
{
return x%2 == 0;
});
// Displaying partitioned Vector
cout << "The partitioned vector is : " ;
for ( int &x : vect) cout << x << " " ;
cout << endl;
// Declaring iterator
vector< int >::iterator it1;
// using partition_point() to get ending position of partition
auto it = partition_point(vect.begin(), vect.end(), []( int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout << "The vector elements returning true for condition are : " ;
for ( it1= vect.begin(); it1!=it; it1++)
cout << *it1 << " " ;
cout << endl;
return 0;
}


输出:

The partitioned vector is : 2 6 8 1 5 7 The vector elements returning true for condition are : 2 6 8

在上面的代码中,偶数和奇数元素是按递增顺序(排序)进行分区的。虽然不总是以递增的顺序出现,但在这里,元素(偶数和奇数)以递增的顺序出现,分区后的结果也是如此。如果向量在稳定的_划分()之后是{2,1,7,8,6,5},那么它就是{2,8,6,1,7,5}。仪容整洁。 5.分区拷贝(beg、end、beg1、beg2、条件) :-这个功能 复制分区的元素 在其参数中提到的不同容器中。它需要5个参数。 容器的开始和结束位置、必须复制元素的新容器的开始位置(条件为返回true的元素)、必须复制其他元素的新容器的开始位置(条件为返回false的元素)和条件 . 调整大小 新容器 这是必要的 用于此功能。

CPP

// C++ code to demonstrate the working of
// partition_copy()
#include<iostream>
#include<algorithm> // for partition algorithm
#include<vector> // for vector
using namespace std;
int main()
{
// Initializing vector
vector< int > vect = { 2, 1, 5, 6, 8, 7 };
// Declaring vector1
vector< int > vect1;
// Declaring vector1
vector< int > vect2;
// Resizing vectors to suitable size using count_if() and resize()
int n = count_if (vect.begin(), vect.end(), []( int x)
{
return x%2==0;
} );
vect1.resize(n);
vect2.resize(vect.size()-n);
// Using partition_copy() to copy partitions
partition_copy(vect.begin(), vect.end(), vect1.begin(),
vect2.begin(), []( int x)
{
return x%2==0;
});
// Displaying partitioned Vector
cout << "The elements that return true for condition are : " ;
for ( int &x : vect1)
cout << x << " " ;
cout << endl;
// Displaying partitioned Vector
cout << "The elements that return false for condition are : " ;
for ( int &x : vect2)
cout << x << " " ;
cout << endl;
return 0;
}


输出:

The elements that return true for condition are : 2 6 8 The elements that return false for condition are : 1 5 7

本文由 曼吉星 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享