2011年7月29日

greater

摘要: // greater example#include <iostream>#include <functional>#include <algorithm>using namespace std;int main () { int numbers[]={20,40,50,10,30}; sort (numbers, numbers+5, greater<int>() ); for (int i=0; i<5; i++) cout << numbers[i] << " "; cout << 阅读全文

posted @ 2011-07-29 23:31 more think, more gains 阅读(168) 评论(0) 推荐(0) 编辑

distance

摘要: // distance example#include <iostream>#include <iterator>#include <list>using namespace std;int main () { list<int> mylist; for (int i=0; i<10; i++) mylist.push_back (i*10); list<int>::iterator first = mylist.begin(); list<int>::iterator last = mylist.end(); co 阅读全文

posted @ 2011-07-29 23:29 more think, more gains 阅读(136) 评论(0) 推荐(0) 编辑

advance http://www.cplusplus.com/reference/std/iterator/advance/

摘要: advance example#include <iostream>#include <iterator>#include <list>using namespace std;int main () { list<int> mylist; for (int i=0; i<10; i++) mylist.push_back (i*10); list<int>::iterator it = mylist.begin(); advance (it,5); cout << "The sixth element in 阅读全文

posted @ 2011-07-29 23:28 more think, more gains 阅读(658) 评论(0) 推荐(0) 编辑

priority_queue

摘要: // priority_queue::push/pop#include <iostream>#include <queue>using namespace std;int main (){ priority_queue<int> mypq; mypq.push(30); mypq.push(100); mypq.push(25); mypq.push(40); cout << "Popping out elements..."; while (!mypq.empty()) { cout << " &quo 阅读全文

posted @ 2011-07-29 23:21 more think, more gains 阅读(297) 评论(0) 推荐(0) 编辑

remove

摘要: // remove algorithm example#include <iostream>#include <algorithm>using namespace std;int main () { int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^ pend = remove (pbe 阅读全文

posted @ 2011-07-29 23:18 more think, more gains 阅读(134) 评论(0) 推荐(0) 编辑

remove_if http://www.cplusplus.com/reference/algorithm/remove_if/

摘要: // remove_if example#include <iostream>#include <algorithm>using namespace std;bool IsOdd (int i) { return ((i%2)==1); }int main () { int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9 // bounds of range: int* pbegin = myints; // ^ int* pend = myints+sizeof(myints)/sizeof(int); // 阅读全文

posted @ 2011-07-29 23:17 more think, more gains 阅读(196) 评论(0) 推荐(0) 编辑

reverse http://www.cplusplus.com/reference/algorithm/reverse/

摘要: // reverse algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { vector<int> myvector; vector<int>::iterator it; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 reverse(myve 阅读全文

posted @ 2011-07-29 23:15 more think, more gains 阅读(168) 评论(0) 推荐(0) 编辑

find_if http://www.cplusplus.com/reference/algorithm/find_first_of/

摘要: // find_if example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd (int i) { return ((i%2)==1);}int main () { vector<int> myvector; vector<int>::iterator it; myvector.push_back(10); myvector.push_back(25); myvector.push_back(40); my 阅读全文

posted @ 2011-07-29 23:05 more think, more gains 阅读(130) 评论(0) 推荐(0) 编辑

stable_partition http://www.cplusplus.com/reference/algorithm/stable_partition/

摘要: //通用算法stable_partition的作用是将容器中符合表达式条件的元素全都排列到容器的前半部,并返回最后一个符合项的迭代器地址// stable_partition example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd (int i) { return (i%2)==1; }int main () { vector<int> myvector; vector<int>::iterator it 阅读全文

posted @ 2011-07-29 22:58 more think, more gains 阅读(181) 评论(0) 推荐(0) 编辑

equal http://www.cplusplus.com/reference/algorithm/equal/

摘要: // equal algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool mypredicate (int i, int j) { return (i==j);}int main () { int myints[] = {20,40,60,80,100}; // myints: 20 40 60 80 100 vector<int>myvector (myints,myints+5); // myvector 阅读全文

posted @ 2011-07-29 22:42 more think, more gains 阅读(168) 评论(0) 推荐(0) 编辑

countif http://www.cplusplus.com/reference/algorithm/count_if/

摘要: // count_if example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool IsOdd (int i) { return ((i%2)==1); }int main () { int mycount; vector<int> myvector; for (int i=1; i<10; i++) myvector.push_back(i); // myvector: 1 2 3 4 5 6 7 8 9 mycount = 阅读全文

posted @ 2011-07-29 22:40 more think, more gains 阅读(152) 评论(0) 推荐(0) 编辑

merge http://www.cplusplus.com/reference/algorithm/merge/

摘要: // merge algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int first[] = {5,10,15,20,25}; int second[] = {50,40,30,20,10}; vector<int> v(10); vector<int>::iterator it; sort (first,first+5); sort (second,second+5); 阅读全文

posted @ 2011-07-29 22:31 more think, more gains 阅读(179) 评论(0) 推荐(0) 编辑

replace http://www.cplusplus.com/reference/algorithm/replace/

摘要: // replace algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 }; vector<int> myvector (myints, myints+8); // 10 20 30 30 20 10 10 20 replace (myvector.begin(), myvector.end 阅读全文

posted @ 2011-07-29 22:29 more think, more gains 阅读(293) 评论(0) 推荐(0) 编辑

for_each stl http://www.cplusplus.com/reference/algorithm/for_each/(转载)

摘要: // for_each example#include <iostream>#include <algorithm>#include <vector>using namespace std;void myfunction (int i) { cout << " " << i;}struct myclass { void operator() (int i) {cout << " " << i;}} myobject;int main () { vector<int& 阅读全文

posted @ 2011-07-29 22:25 more think, more gains 阅读(115) 评论(0) 推荐(0) 编辑

count http://www.cplusplus.com/reference/algorithm/count/

摘要: // count algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;//count 他查找一个元素出现的次数int main () { int mycount; // counting elements in array: int myints[] = {10,20,30,30,20,10,10,20}; // 8 elements mycount = (int) count (myints, myints+8, 10); 阅读全文

posted @ 2011-07-29 22:19 more think, more gains 阅读(176) 评论(0) 推荐(0) 编辑

线性筛素数

摘要: #include <stdio.h>#include <string.h>#include <stdlib.h>#include <math.h>const int N = 10000000;bool a[N];int p[N];using namespace std;void debug( ){ #ifdef P freopen("out","w",stdout); #endif } void prime( ){ debug( ); memset(a, 0, N*sizeof(a[0])); int nu 阅读全文

posted @ 2011-07-29 20:58 more think, more gains 阅读(143) 评论(0) 推荐(0) 编辑

copy

摘要: #include <stdio.h>#include <iostream>#include <algorithm>#include <vector>using namespace std;int main( ){ int my[]={10,32,324,43,43,2332,3223}; vector<int>myvector; vector<int>::iterator it; myvector.resize(7); copy(my, my + 7, myvector.begin()); for( it = myvect 阅读全文

posted @ 2011-07-29 20:21 more think, more gains 阅读(90) 评论(0) 推荐(0) 编辑

min heap

摘要: // range heap example#include <iostream>#include <algorithm>#include <vector>#include <functional>using namespace std;bool cmp( int x, int y){ return x > y;}int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+5); vector<int>::iterator it; 阅读全文

posted @ 2011-07-29 16:29 more think, more gains 阅读(194) 评论(0) 推荐(0) 编辑

heap stl 各类操作 http://www.cplusplus.com/reference/algorithm/push_heap/

摘要: // range heap example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int myints[] = {10,20,30,5,15}; vector<int> v(myints,myints+5); vector<int>::iterator it; make_heap (v.begin(),v.end()); cout << "initial max heap : 阅读全文

posted @ 2011-07-29 16:14 more think, more gains 阅读(203) 评论(0) 推荐(0) 编辑

heap 堆的各类操作

摘要: #include <stdio.h>#include <string.h>#include <stdlib.h>int N;//最小堆int heap[20];//交换两个元素的值void swap( int &x, int &y){ int temp; temp = x; x = y; y = temp;}// 非递归写法void down ( int x ){ int i, j, flag = 1; if ( 2 * x <= N) { while (flag && (i = 2 * x) <= N ) { i 阅读全文

posted @ 2011-07-29 15:56 more think, more gains 阅读(231) 评论(0) 推荐(0) 编辑

导航