code 流 http://hi.baidu.com/my_acm_room/blog/item/684fcc171057d210972b43c1.html
- 自己敲了一下:
- #include <iostream>
- #include <map>
- using namespace std;
- int main(int argc, char *argv[])
- {
- map<int, int> xiaoqiang;
- xiaoqiang.insert(make_pair(1, 1));
- xiaoqiang.insert(make_pair(1, 1));
- map<int, int>::iterator iter;
- for( iter=xiaoqiang.begin(); iter!=xiaoqiang.end(); iter++)
- {
- cout<<iter->first<<" : " <<iter->second<<endl;
- }
- return 0;
- }
#include<cstdio> #include<algorithm> #include<map> #include<iostream> #include<String> #include<Queue> using namespace std; struct node { int key; int val; node() {} node(int a, int b){ key = a, val = b; } bool operator < ( node nd) const{//+ const return key < nd.key;//´ÓСµ½´ó //return key > nd.key;//·´Ö® } bool operator > ( node nd) const{//+ const return key > nd.key;//´ÓСµ½´ó //return key > nd.key;//·´Ö® } }; //±È½Ïº¯Êýд·¨Ò» struct cmp { bool operator () ( node nd1, node nd2 ){ return nd1.key > nd2.key; } }; int main() { //char str[] = "hello world!"; ////////////map////////////////////////////// //¶¨Òå map<string, int> mp; string str = "Lili"; //²åÈë //mp[str] = 13865235; mp.insert(pair<string,int>(str, 123)); mp["a"]=1; mp["b"]=2; mp["c"]=3; mp["d"]=4; mp["e"]=5; //²éÕÒ map<string, int>::iterator it; it = mp.find("hi"); if(it == mp.end()){ printf("no command!\n"); } else{ cout<<it->second<<endl; } for(it = mp.begin(); it != mp.end(); it++){ printf("%s %d\n", it->first.c_str(), it->second); } //¸üРmp[str]=123456; it = mp.find(str); if(it != mp.end()){ it->second = 321; } cout<<it->second<<endl; //////////////////queue////////////////////// //¶¨Òå queue<int>que; //²åÈë que.push(4); que.push(2); que.push(3); //·ÃÎÊ if(!que.empty()){ printf("front: %d\n", que.front()); printf("back: %d\n", que.back()); } //¸üРque.pop(); if(!que.empty()){ printf("update:\n"); printf("front: %d\n", que.front()); printf("back: %d\n", que.back()); } /////////////////priority_queue/////////////////// //define priority_queue<int>pri_que; //insert pri_que.push(3); pri_que.push(10); pri_que.push(1); //visit if(!pri_que.empty()){ printf("priority_queue example:\n"); printf("top: %d\n", pri_que.top()); printf("size: %d\n", pri_que.size()); } //update if(!pri_que.empty()){ pri_que.pop(); } if(!pri_que.empty()){ printf("after pop:\n" ); printf("top : %d\n", pri_que.top()); } /////pri_que1 priority_queue<int, vector<int>, less<int> >pri_que1; pri_que1.push(11); pri_que1.push(10); pri_que1.push(12); //visit if(!pri_que1.empty()){ printf("priority_queue 1 example:\n"); printf("top: %d\n", pri_que1.top()); printf("size: %d\n", pri_que1.size()); } //update if(!pri_que1.empty()){ pri_que1.pop(); } if(!pri_que1.empty()){ printf("after pop:\n" ); printf("top : %d\n", pri_que1.top()); } //pri_que2 priority_queue<int, vector<int>, greater<int> >pri_que2; pri_que2.push(103); pri_que2.push(10); pri_que2.push(123); //visit if(!pri_que2.empty()){ printf("priority_queue 2 example:\n"); printf("top: %d\n", pri_que2.top()); printf("size: %d\n", pri_que2.size()); } //update if(!pri_que2.empty()){ pri_que2.pop(); } if(!pri_que2.empty()){ printf("after pop:\n" ); printf("top : %d\n", pri_que2.top()); } //struct example with priority_queue printf("struct example with priority_queue\n"); priority_queue<node, vector<node>, greater<node> > pri_node; pri_node.push( node(1, 10) ); pri_node.push( node(-1, 10)); pri_node.push( node(100, 10)); printf("top : %d %d\n", pri_node.top().key, pri_node.top().val ); //sort printf("sort\n" ); int array[]={1,2,3,4,5,6,7,8}; sort(array, array + 8, greater<int>());//´Ó´óµ½Ð¡ÅÅÁÐ for(int i = 0; i < 8; i++ ){ printf("%d ", array[i] ); } printf("\n"); //binary_search printf("binary_search\n" ); int myints[] = {1,2,3,4,5,4,3,2,1}; vector<int> v(myints,myints+9); // 1 2 3 4 5 4 3 2 1 // using default comparison: sort (v.begin(), v.end()); cout << "looking for a 3... "; if (binary_search (v.begin(), v.end(), 3))//binary_search(myints, myints + 9, 3) cout << "found!\n"; else cout << "not found.\n"; //string //strcmp(), strcpy, strcat, strstr(s1, s2) printf("strstr : hello world!=>world %s\n", strstr("hello world!","world") ); //val = strcmp(s1, s2);<1>val = 0 : equal <2>val=1 : s1 > s2 <3>val = -1 : s1 < s2 //strcpy(s1, s2); s1 = s2; return 0; }