C++ 的常见算法 之二

划分序列

partition

重新排列 [first,last) 范围内的元素,使得 pred 返回 true 的所有元素先于所有返回 false 的元素。迭代器返回指向第二组的第一个元素的点。

每个组内的相对顺序不一定与调用之前相同。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool greater50 (int i) { return (i > 50); }
void print_val(int val) { cout << val << ' '; }
int main () {
  vector<int> myvector = {30, 40, 80, 12, 19, 20, 55, 72};

  vector<int>::iterator bound;
  bound = partition (myvector.begin(), myvector.end(), greater50);

  // print out content:
  cout << "greater than 50:";
  for (vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
    cout << ' ' << *it;
  cout << '\n';

  cout << "less than 50:";
  for (vector<int>::iterator it=bound; it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  for_each (myvector.begin(), myvector.end(), print_val);
  cout << endl;
  
  return 0;
}

结果屏幕输出

greater than 50: 72 55 80
less than 50: 12 19 20 40 30
72 55 80 12 19 20 40 30

stable_partition

重新排列 [first,last) 范围内的元素,pred 返回 true 的所有元素排在返回 false 的所有元素之前, 但与partition函数不同,这个函数保留每个组内元素的相对顺序。

这通常使用内部临时缓冲区来实现。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool greater30 (int i) { return (i > 30); }
void print_element(int val) { cout << val << " ";}

int main () {
  vector<int> myvector = {4, 80, 5, 30, 6, 9, 50, 60, 1, 15};

  vector<int>::iterator bound;
  bound = stable_partition (myvector.begin(), myvector.end(), greater30);

  // print out content:
  cout << "greater than 30 elements:";
  for (vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
    cout << ' ' << *it;
  cout << '\n';

  cout << "greater than or equal 30 elements: ";
  for (vector<int>::iterator it=bound; it!=myvector.end(); ++it)
    cout << ' ' << *it;
  cout << '\n';

  cout << "all elements: ";
  for_each (myvector.begin(), myvector.end(), print_element);
  cout << '\n';

  return 0;
}

结果屏幕输出

greater than 30 elements: 80 50 60
greater than or equal 30 elements:  4 5 30 6 9 1 15
all elements: 80 50 60 4 5 30 6 9 1 15

排序

sort

将范围 [first,last) 中的元素按升序排序。
第一个版本使用operator< 来比较元素,第二个版本使用comp 来比较元素。

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

using namespace std;

struct personnel {
	string  name;
	int     age;
};

bool myfunction (int i,int j) { return (i<j); }
void print_element(int val) { cout << val << " " ; }
void print_personnel(personnel person) { cout << person.name << " " << person.age << endl; }

struct sort_class {
   bool operator() (personnel lf, personnel lr) { return (lf.age < lr.age);}
} sort_object;

int main () {
  vector<int> myvector = {32,71,12,45,26,80,53,33}; 
  vector<personnel> persons = {
	  {"John", 30},
	  {"Allison", 25},		  
	  {"Sam", 29},		  
	  {"Alice", 39},		  
  };
  
  // using default comparison (operator <):
  sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33
  sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

  // print out content:
  cout << "myvector contains:" << endl;
  for_each (myvector.begin(), myvector.end(), print_element);
  cout << '\n';

  // using object as comp
  sort (persons.begin(), persons.end(), sort_object); 
  // print out content:
  cout << "sorted personnel:" << endl;
  for_each (persons.begin(), persons.end(), print_personnel);
  cout << '\n';
  return 0;
}

结果屏幕·输出

myvector contains:
12 32 45 71 26 33 53 80
sorted personnel:
Allison 25
Sam 29
John 30
Alice 39

nth_element

重新排序 [first,last) 范围内的元素,得到的序列是,第 n 个位置的元素位置是,完全排序后,该元素所在的位置。

在结果序列中,其他元素没有任何特定的顺序,第 n 个元素前面的所有元素,都小于或对于该元素,它后面的元素都大于或等于它。

该算法有两个版本,第一个版本使用operator< 来比较元素,第二个版本使用comp来比较元素。

#include <iostream>  
#include <algorithm> 
#include <vector> 

using namespace std;

bool myfunction (int i,int j) { return (i<j); }
void print_element(int val)  { cout << val << " "; }

struct personnel {
	string name;
	int  age;
};

struct personnel_comp {
	bool operator()(personnel lf, personnel lr) {return (lf.age < lr.age);};
} person_comp;

int main () {
  vector<int> myvector = {1, 9, 5, 8, 90, 30, 11};
  vector<int> v2 = {11, 19, 15, 18, 100, 40, 21, 130, 210};
  vector<personnel> v_persons = {
	  {"John", 30},
	  {"Allison", 25},		  
	  {"Sam", 29},		  
	  {"Alice", 39},
  };
  
  // using default comparison (operator <):
  nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
  cout << "before sort:";
  for_each (myvector.begin(), myvector.end(), [](int val) ->void {cout << val << " ";});
  cout << endl;

  // using default comp
  nth_element (myvector.begin(), myvector.begin()+5, myvector.end());
 
  // print out content:
  cout << " after sort:";
  for_each (myvector.begin(), myvector.end(),  [](int val) ->void {cout << val << " ";});
  cout << '\n';

  // using function as comp
  cout << "before sort:" << endl;
  for_each (v2.begin(), v2.end(), [](int val) ->void {cout << val << " ";});
  cout << endl;
  nth_element (v2.begin(), v2.begin()+6, v2.end(), myfunction);
  cout << " after sort:";
  for_each (v2.begin(), v2.end(),  [](int val) ->void {cout << val << " ";});
  cout << '\n';

  // using function as comp
  cout << "before sort:" << endl;
  for_each (v_persons.begin(), v_persons.end(), [](personnel p) ->void {cout << p.name << " " << p.age << endl;});
  cout << endl;
  nth_element (v_persons.begin(), v_persons.begin()+3, v_persons.end(), person_comp);
  cout << " after sort:" << endl;
  for_each (v_persons.begin(), v_persons.end(),  [](personnel p) ->void {cout << p.name << " " << p.age << endl;});
  cout << '\n';

  return 0;
}

程序运行结果屏幕输出

before sort:9 1 5 8 11 30 90
 after sort:8 1 5 9 11 30 90
before sort:
11 19 15 18 100 40 21 130 210
 after sort:19 11 15 18 21 40 100 130 210
before sort:
John 30
Allison 25
Sam 29
Alice 39

 after sort:
Sam 29
Allison 25
John 30
Alice 39

二分查找

binary_search

如果范围 [first,last) 中的任何元素值等于 val,则返回 true,否则返回 false。

该算法支持两种版本。

  • 第一个版本,使用operator< 来比较元素,
  • 第二个版本,使用comp 来比较元素。如果 (!(a<b) && !(b<a)) 或 if (!comp(a,b) && !comp(b,a)),则 a 和 b 两个元素被视为相同。

范围中的元素应已根据相同的标准(operator< 或 comp)进行排序,或者至少根据 val 进行分区。

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct personnel {
	string name;
	int  age;
};

struct personnel_comp {
	bool operator()(personnel lf, personnel lr) {return (lf.name < lr.name);};
} person_comp;

int main () {
  std::vector<int> v = {1,2,40,30,3,4,5,20,10}; 
  vector<personnel> v_persons = {
	  {"John", 30},
	  {"Allison", 25},		  
	  {"Sam", 29},		  
	  {"Alice", 39},
  };

  // using default comparison:
  sort (v.begin(), v.end());
  cout << " After sort: " << endl;
  for_each (v.begin(), v.end(), [](int v)->void{cout << v << " ";});
  cout << endl;
  
  cout << "looking for a 3... ";
  if (binary_search (v.begin(), v.end(), 3))
    cout << "found!\n"; 
  else cout << "not found.\n";

  // using myfunction as comp:
  sort (v.begin(), v.end(), myfunction);
  cout << "looking for a 15... ";
  if (binary_search (v.begin(), v.end(), 15, myfunction))
    cout << "found!\n"; 
  else cout << "not found.\n";

  // using personnel_comp as comp:
  cout << " Before sort: " << endl;
  for_each (v_persons.begin(), v_persons.end(), [](personnel v)->void{cout << v.name << " " << v.age << endl;});
  
  sort (v_persons.begin(), v_persons.end(), person_comp);
  cout << " After sort: " << endl;
  for_each (v_persons.begin(), v_persons.end(), [](personnel v)->void{cout << v.name << " " << v.age << endl;});
  cout << endl;

  cout << "looking for John... ";
  personnel person = {"John", 30}; 
  if (binary_search (v_persons.begin(), v_persons.end(), person, person_comp))
    cout << "found!\n"; 
  else cout << "not found.\n";

  return 0;
}

上述代码运行后的屏幕输出

 After sort:
1 2 3 4 5 10 20 30 40
looking for a 3... found!
looking for a 15... not found.
 Before sort:
John 30
Allison 25
Sam 29
Alice 39
 After sort:
Alice 39
Allison 25
John 30
Sam 29

looking for John... found!
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值