T9的空间

You will never walk alone!

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  69 随笔 :: 0 文章 :: 28 评论 :: 0 Trackbacks

#

 1/*
 2  Name: build priority_queue
 3  Copyright: 
 4  Author: Torres
 5  Date: 29-08-08 10:48
 6  Description: 建立优先队列的方法 
 7*/

 8#include<iostream>
 9#include<vector>
10#include<queue>
11using namespace std;
12template<class T>void print(T &a){while(!a.empty()){cout<<a.top()<<" ";a.pop();}cout<<endl;}
13typedef struct node{
14    int x,y;
15    bool operator<(const node &a)const{
16        return x>a.x||(x==a.x&&y>a.y);
17    }

18}
node;
19
20/*typedef struct data{
21    double x,y;
22    bool cmp(data &a){
23        return x>a.x||(x==a.x&&y>a.y);
24    }
25}data;*/

26priority_queue<int,vector<int>,less<int> >LessIntq;//虽然是less但是是最大优先
27priority_queue<int>SysIntq;//默认是less
28priority_queue<int,vector<int>,greater<int> >GreatIntq;
29priority_queue<node>Nodeq;
30//priority_queue<data,vector<data>,cmp>Cmpdata;
31
32
33int main()
34{
35    int i,temp;
36    for(i=1;i<=5;i++){
37        scanf("%d",&temp);
38        LessIntq.push(temp);
39        SysIntq.push(temp);
40        GreatIntq.push(temp);
41    }

42    cout<<"LessIntq::";
43    print(LessIntq);
44    cout<<"SysIntq::";
45    print(SysIntq);
46    cout<<"GreatIntq::";
47    print(GreatIntq);
48    cout<<"******************"<<endl;
49
50    node ntemp;
51    for(i=1;i<=5;i++){
52        scanf("%d%d",&ntemp.x,&ntemp.y);
53        Nodeq.push(ntemp);
54    }

55    cout<<"Nodeq::"<<endl;
56    for(i=0;i<5;i++){
57        cout<<Nodeq.top().x<<"::"<<Nodeq.top().y<<endl;
58        Nodeq.pop();
59    }

60
61/*    data dtemp;
62    for(i=1;i<=5;i++){
63        scanf("%lf%lf",&dtemp.x,&dtemp.y);
64        Cmpdata.push(dtemp);
65    }
66    cout<<"Cmpdata::";
67    for(i=0;i<5;i++){
68        cout<<Cmpdata.top().x<<"::"<<Cmpdata.top().y<<endl;
69        Cmpdata.pop();
70    }*/

71    return 0;
72}

73/****************************************
742 5 1 3 7
75LessIntq::7 5 3 2 1
76SysIntq::7 5 3 2 1
77GreatIntq::1 2 3 5 7
78******************
791 2
805 6
812 3
822 10
837 7
84Nodeq::
851::2
862::3
872::10
885::6
897::7
90Press any key to continue
91*************************************/
posted @ 2008-08-29 11:31 Torres 阅读(326) | 评论 (0)编辑 收藏

     摘要: 在模板参数,传递类名,在 ( 离散 ) 函数参数中,使用函数名 ( myStruct(); 或 myFunc ) 内置 <functional> : greater <T>, less<T>, greater_equal<T>, less_equal<T>, equal_to<T>, not_equal_to<T&g...  阅读全文
posted @ 2008-08-28 21:31 Torres 阅读(209) | 评论 (0)编辑 收藏

     摘要:   1#include<iostream>  2#include<string>  3#include<algorithm>  4using namespace std;  5int main()  6{ ...  阅读全文
posted @ 2008-08-19 17:16 Torres 阅读(261) | 评论 (0)编辑 收藏

C++ Queues

The C++ Queue is a container adapter that gives the programmer a FIFO (first-in, first-out) data structure.

Display all entries for C++ Queues on one page, or view entries individually:

Queue constructor construct a new queue
back returns a reference to last element of a queue
empty true if the queue has no elements
front returns a reference to the first element of a queue
pop removes the first element of a queue
push adds an element to the end of the queue
size returns the number of items in the queue
 1#include<iostream>
 2#include<queue>
 3#include<algorithm>
 4using namespace std;
 5int main()
 6{
 7    //队列,first_in ,first_out,不提供迭代器,不提供走访功能
 8    int i;
 9    queue<int>q;
10    for(i=0;i<5;i++)q.push(i);
11    while(q.size()){
12        cout<<q.front();
13        q.pop();
14    }

15    return 0;
16}

17
posted @ 2008-08-18 11:36 Torres 阅读(12085) | 评论 (0)编辑 收藏

C++ Double-ended Queues

Double-ended queues are like vectors, except that they allow fast insertions and deletions at the beginning (as well as the end) of the container.

Display all entries for C++ Double-ended Queues on one page, or view entries individually:

Container constructors create dequeues and initialize them with some data
Container operators compare, assign, and access elements of a dequeue
assign assign elements to a dequeue
at returns an element at a specific location
back returns a reference to last element of a dequeue
begin returns an iterator to the beginning of the dequeue
clear removes all elements from the dequeue
empty true if the dequeue has no elements
end returns an iterator just past the last element of a dequeue
erase removes elements from a dequeue
front returns a reference to the first element of a dequeue
insert inserts elements into the dequeue
max_size returns the maximum number of elements that the dequeue can hold
pop_back removes the last element of a dequeue
pop_front removes the first element of the dequeue
push_back add an element to the end of the dequeue
push_front add an element to the front of the dequeue
rbegin returns a reverse_iterator to the end of the dequeue
rend returns a reverse_iterator to the beginning of the dequeue
resize change the size of the dequeue
size returns the number of items in the dequeue
swap swap the contents of this dequeue with another

在我看来,deque就是一个双端的array,和vector是一摸一样的,虽然内部机制不一样
 1/*
 2  Name: C++ DEQUE   
 3  Copyright: 
 4  Author: Torres
 5  Date: 18-08-08 10:55
 6  Description: STL DEQUE 学习笔记 
 7*/

 8#include<deque>
 9#include<iostream>
10#include<algorithm>
11using namespace std;
12#define showpass cout<<"pass"<<endl
13typedef struct point{
14    double x,y;
15    point(double a=0,double b=0){
16        x=a;
17        y=b;
18    }

19}
point;
20ostream operator<<(ostream out,point a)
21{
22    return out<<a.x<<" "<<a.y<<endl;
23}

24
25void print(point a)
26{
27    cout<<a;
28}

29int main()
30{
31    //deque与vector很相似只不过是两端都可以存取的vector
32    deque<point>cp,cp1(10);
33    int i;
34    for(i=0;i<5;i++){
35        point temp(i,i+1);
36        cp.push_back (temp);
37    }

38    
39    for(i=0;i<5;i++){
40        cp1[i].x=i;
41        cp1[i].y=i+1;
42    }

43    //取地址符号[]必须在容器已经开辟空间后才能用,push函数开辟了新空间
44    for_each(cp.begin(),cp.end(),print);
45    cout<<endl;
46    for_each(cp1.begin(),cp1.end()-5,print);
47    cout<<endl;
48    
49    //erase删除函数
50    cp.erase(cp.begin()+1);
51    for_each(cp.begin(),cp.end(),print);
52    cout<<endl;
53    cp1.erase(cp1.begin(),cp1.begin()+3);
54    for_each(cp1.begin(),cp1.end()-5,print);
55    cout<<endl;
56
57    cout<<cp.front()<<cp.back()<<endl; 
58
59
60    return 0;
61}

posted @ 2008-08-18 11:02 Torres 阅读(650) | 评论 (0)编辑 收藏

先看函数:
C++ Lists

Lists are sequences of elements stored in a linked list. Compared to vectors, they allow fast insertions and deletions, but slower random access.

Display all entries for C++ Lists on one page, or view entries individually:

List constructors create lists and initialize them with some data
List operators assign and compare lists
assign assign elements to a list
back returns a reference to last element of a list
begin returns an iterator to the beginning of the list
clear removes all elements from the list
empty true if the list has no elements
end returns an iterator just past the last element of a list
erase removes elements from a list
front returns a reference to the first element of a list
insert inserts elements into the list
max_size returns the maximum number of elements that the list can hold
merge merge two lists
pop_back removes the last element of a list
pop_front removes the first element of the list
push_back add an element to the end of the list
push_front add an element to the front of the list
rbegin returns a reverse_iterator to the end of the list
remove removes elements from a list
remove_if removes elements conditionally
rend returns a reverse_iterator to the beginning of the list
resize change the size of the list
reverse reverse the list
size returns the number of items in the list
sort sorts a list into ascending order
splice merge two lists in constant time
swap swap the contents of this list with another
unique removes consecutive duplicate elements

要注意的是list不提随机的迭代器,迭代器是个双向的,非连续存储,而且是个环状链表在尾端是个未初始化节点
空判断:begin==end;
clear  remove  unique(把相同元素移出,留一个先sort)  insert(插入两种方式)
只能使用成员函数 sort,不能使用 算法 sort(他只接受random iterator)

 1/*
 2  Name: C++ STL LIST    
 3  Copyright: 
 4  Author: Torres
 5  Date: 17-08-08 23:28
 6  Description: LIST 学习笔记 
 7*/

 8#include<iostream>
 9#include<list>
10#include<vector>
11#include<algorithm>
12using namespace std;
13typedef struct node{
14    double x,y;
15    node (double a=0,double b=0){
16        x=a;
17        y=b;
18    }

19}
node;
20void print(int a){printf("%d ",a);return;}
21
22ostream operator <<(ostream out,node a)
23{
24    return out<<a.x<<" "<<a.y<<endl;
25}

26int main()
27{
28    int i,j;
29
30    list<node>nlist;
31    list<int>intlist,intlist1;
32    
33    vector<int>intv1,intv2;
34    intv1.assign(5,7);
35    intv2.assign(intv1.begin()+1,intv1.end()-1);
36    for_each(intv2.begin()+1,intv2.end(),print);
37    cout<<endl;
38
39    //assign的用法,有两种,发现list的iterator不能够相加而vector可以
40    intlist.assign(5,7);
41    intlist1.assign(intlist.begin(),intlist.end());
42    for_each(intlist.begin(),intlist.end(),print);
43    printf("\n");
44    for_each(intlist1.begin(),intlist1.end(),print);
45    printf("\n");
46    
47    //size的用法,表示实际数据的大小
48    cout<<intlist.size()<<endl;
49    //list没有容量capacity函数
50
51    //因为list是一个双端链表,不支持随机存取,他的存取必须用iterator来进行
52    //而vector,deque,等容器是支持的。
53    list<int>::iterator it1;
54    for(it1=intlist1.begin();it1!=intlist1.end();it1++)
55        cout<<*it1<<" ";
56    cout<<endl;
57
58    const node cnode(1.0,2.0);
59    cout<<cnode<<endl;
60
61    nlist.assign(5,cnode);
62    list<node>::iterator it2;
63    for(it2=nlist.begin();it2!=nlist.end();it2++)
64        cout<<*it2;
65    return 0;
66}

67

posted @ 2008-08-17 23:30 Torres 阅读(661) | 评论 (0)编辑 收藏

菜菜的学习了一下STL的vector,做一下笔记感觉这个网站讲得通俗易懂http://www.cppreference.com/cppvector/index.html 写了一下测试code(待续)

C++ Vectors

Vectors contain contiguous elements stored as an array. Accessing members of a vector or appending elements can be done in constant time, whereas locating a specific value or inserting elements into the vector takes linear time.

Display all entries for C++ Vectors on one page, or view entries individually:

Vector constructors create vectors and initialize them with some data
Vector operators compare, assign, and access elements of a vector
assign assign elements to a vector
at returns an element at a specific location
back returns a reference to last element of a vector
begin returns an iterator to the beginning of the vector
capacity returns the number of elements that the vector can hold
clear removes all elements from the vector
empty true if the vector has no elements
end returns an iterator just past the last element of a vector
erase removes elements from a vector
front returns a reference to the first element of a vector
insert inserts elements into the vector
max_size returns the maximum number of elements that the vector can hold
pop_back removes the last element of a vector
push_back add an element to the end of the vector
rbegin returns a reverse_iterator to the end of the vector
rend returns a reverse_iterator to the beginning of the vector
reserve sets the minimum capacity of the vector
resize change the size of the vector
size returns the number of items in the vector
swap swap the contents of this vector with another

 1#include<vector>
 2#include<iostream>
 3#include<algorithm>
 4using namespace std;
 5void print(int a){cout<<a<<" ";}
 6int main()
 7{
 8    vector<int>v,v1;
 9    vector<int>::iterator iv1,iv2;
10    vector<int>v2;
11    v2.reserve(10);    
12    v.assign(10,2);
13    cout<<v2.capacity()<<endl;//capacity容量输出为10
14    cout<<v2.size()<<endl;//大小输出为1
15    int i;
16    for(i=0;i<7;i++)v2.push_back(i);
17    for_each(v2.begin(),v2.end(),print);
18    cout<<endl;
19    for(iv1=v2.begin();iv1!=v2.end();iv1++)
20        if(*iv1==3)break;
21    if(iv1!=v2.end())v2.insert(iv1,99);
22    for_each(v2.begin(),v2.end(),print);
23    cout<<endl;
24    if(iv1!=v2.end())v2.insert(iv1,5,99);
25    for_each(v2.begin(),v2.end(),print);
26    cout<<endl;
27    if(iv1!=v2.end())v2.insert(iv1,v.begin(),v.end());
28    for_each(v2.begin(),v2.end(),print);
29    cout<<endl;
30    cout<<v2.size()<<endl;
31    v2.erase(v2.begin());
32    cout<<v2.size()<<endl;
33    v2.erase(v2.begin(),v2.end());
34    for_each(v2.begin(),v2.end(),print);
35    iv1=v.begin();iv2=v.end();
36    cout<<v.begin()<<endl;
37    cout<<v.end()<<endl;
38    cout<<iv1<<endl;
39    cout<<iv2<<endl;
40//    for(i=0;i<15;i++)cout<<v[i]<<" ";
41//    cout<<endl;
42//    for(i=0;i<15;i++)cout<<v.at(i)<<" ";//at()函数更加安全;
43//    cout<<endl;
44    for_each(v.begin(),v.end(),print);
45    cout<<endl;
46//    v1.assign(v.begin(),v.end());
47//    v1.assign(iv1+2,iv2-2);
48//    for(i=0;i<10;i++)cout<<v1[i]<<' ';
49//    cout<<endl;
50    for(i=0;i<10;i++)v1.push_back(i);
51    for_each(v1.begin()+1,v1.end()-2,print);//输出begin开始指向的数据,直到end指向的前一个数据
52    for_each(v1.begin(),v1.end(),print);
53    cout<<endl;
54    cout<<v1.back()<<endl;
55    cout<<v1.front()<<endl;
56    cout<<v1.size()<<endl;
57    v.clear();
58    cout<<v.size()<<endl;
59    for_each(v.begin(),v.end(),print);//已经clear,begin==end,不会有任何结果。
60    return 0;
61}

62
63
64
65//试了一下结构体,感觉自己简直太水了,努力学习中!
66
67#include<iostream>
68#include<vector>
69using namespace std;
70typedef struct node{
71    int x;
72    int y;
73}
node;
74ostream operator<<(ostream in,node a){
75    in<<a.x<<a.y<<endl;
76    return in;
77}

78int main()
79{
80    vector<node>v(10);
81    v[0].x=1;v[0].y=1;
82    vector<node>::iterator iv(v.begin());
83    cout<<v[0].x<<v[0].y<<endl;
84    cout<<*iv<<endl;
85    cout<<iv->x<<iv->y<<endl;
86    return 0;
87}
posted @ 2008-08-13 00:54 Torres 阅读(159) | 评论 (0)编辑 收藏

一直想有一个blog,做做笔记,整理整理思路,基于这个思想,这个blog诞生了!以后就靠这个来祭奠生活了。
posted @ 2008-08-11 12:30 Torres 阅读(176) | 评论 (1)编辑 收藏

仅列出标题
共7页: 1 2 3 4 5 6 7