1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <set> #include <queue> #include <map> #include <cmath> #include <vector> using namespace std; #define N 500010 #define pi acos(-1.0) #define inf 0x3f3f3f3f #define pb(x) push_back((x)) typedef long long ll; typedef unsigned long long ull; struct node{ int a,b; friend bool operator < (node aa, node bb){ return aa.a>bb.a; } node(int aa,int bb):a(aa),b(bb){} }; void print(vector<int> &L){ for (vector<int>::iterator it=L.begin(); it!=L.end(); it++) cout << *it << " "; cout << endl; } int main(){ //priority_queue priority_queue<node> q; int tmp,n; q.push(node(1,4)); q.push(node(5,4)); q.push(node(77,7)); q.push(node(2,4)); printf("priority\n"); while(!q.empty()){ printf("%d %d\n",q.top().a,q.top().b); q.pop(); } //vector vector<int> v; vector<int>::iterator it,first,last; for (int i=0; i<10; i++) v.push_back(i); vector<int>::iterator min_it = min_element(v.begin(), v.end()); vector<int>::iterator max_it = max_element(v.begin(), v.end()); cout << "Min is " << *min_it << endl; cout << "Max is " << *max_it << endl; sort(v.begin(), v.end()); print(v); sort(v.begin(), v.end(), greater<int>()); // 按降序排序 print(v); v.push_back(2); sort(v.begin(), v.end()); cout<<"lower_bound 2 "<<lower_bound(v.begin(),v.end(),2)-v.begin()<<endl;//O(logn) 查找第一个不小于k的元素 cout<<"upper_bound 2 "<<upper_bound(v.begin(),v.end(),2)-v.begin()<<endl;// O(logn) 查找第一个大于k的元素 v.front();//返回首元素。 v.back(); //返回尾元素。 v.push_back(tmp); //向表尾插入元素x。 v.size(); //返回表长。 v.empty(); //当表空时,返回真,否则返回假。 v.pop_back(); //删除表尾元素。 v.begin(); //返回指向首元素的随机存取迭代器。 v.end(); //返回指向尾元素的下一个位置的随机存取迭代器。 /* v.insert(it, tmp);// 向迭代器it指向的元素前插入新元素val。 v.insert(it, n, tmp);// 向迭代器it指向的元素前插入n个x。 v.insert(it, first, last);// 将由迭代器first和last所指定的序列[first, last)插入到迭代器it指向的元素前面。 v.erase(it); //删除由迭代器it所指向的元素。 v.erase(first, last); //删除由迭代器first和last所指定的序列[first, last)。 v.reserve(n); //预分配缓冲空间,使存储空间至少可容纳n个元素。 v.resize(n); //改变序列的长度,超出的元素将会被删除,如果序列需要扩展(原空间小于n), 元素默认值将填满扩展出的空间。 v.resize(n, tmp);// 改变序列的长度,超出的元素将会被删除,如果序列需要扩展(原空间小于n), 将用val填满扩展出的空间。 v.clear();// 删除容器中的所有的元素。 s.swap(v) 将s与另一个vector对象v进行交换。 v.assign(first, last);// 将序列替换成由迭代器first和last所指定的序列[first, last)。 [first, last)不能是原序列中的一部分。 //要注意的是,resize操作和clear操作都是对表的有效元素进行的操作,但 并不一定会改变缓冲空间的大小。 */ string str; /*getline(cin,str);// 读取字符到遇到换行,空格可读入,知道‘\n’结束(练习在下一个代码中), getline(cin,str,'a');// 一个直到‘a’结束,其中任何字符包括'\n'都能够读入 -------------------------插入函数----------------------------------包括迭代器操作和下标操作,下标操作更灵活 s.insert( it , p ); 把字符串p插入到it的位置 s.insert(p,n,t); 迭代器p元素之前插入n个t的副本 s.insert(p,b,e); 迭代器p元素之前插入迭代器b到e之间的所有元素 s.insert(p,s2,poe2,len); 在下标p之前插入s2下标从poe2开始长度为len的元素 s.insert(pos,cp,len); 下标pos之前插入cp数组的前len个元素。 -----------------------替换函数------------------------------- s.assign(b,e); 用迭代器b到e范围内的元素替换s s.assign(n,t); 用n个t的副本替换s a.assign(s1,pos2,len);从s1的下标pos2开始连续替换len个。 s.replace ( 3 , 3 , " good " ) ; 从第三个起连续三个替换为good s.substr(i,j) 截取s串中从i到j的子串 //string::npos 判断字符串是否结束 -----------------------删除函数----------------------------- s.erase( 3 )||s.erase ( 0 , 4 ) ; 删除第四个元素或第一到第五个元素 ----------------------其他函数----------------------------- s.find ( " cat " ) ; 超找第一个出现的字符串”cat“,返回其下标值,查不到返回 4294967295,也可查找字符; s.append(args); 将args接到s的后面 s.compare ( " good " ) ; s与”good“比较相等返回0,比"good"大返回1,小则返回-1; reverse ( s.begin(), s.end () ); 反向排序函数,即字符串反转函数*/ map<int,string> mapp; map<int,string> ::iterator mit; mapp[1]="a"; mapp[2]="b"; mapp[5]="hello"; mapp[3]="word"; mapp.insert(make_pair(33,"boring")); for(mit=mapp.begin();mit!=mapp.end();mit++){ cout<<"map "<<mit->second<<endl; } int key=33; mit=mapp.find(key); if (mit==mapp.end()){ cout << "No such key!" << endl; } else{ cout << key << " is " << mit->second << endl; cout << "Erased " << mapp.erase(key) << endl; } char string[10]; char str1[] = "abcdefghi"; strcpy(string, str1); cout<<string<<endl; char destination[25]; char *blank = " ",*c = "C++", *Borland = "Borland"; strcpy(destination, Borland); strcat(destination, blank); strcat(destination, c); printf("%s\n", destination); char *ptr, ch = 'r'; strcpy(string, "This is a string"); ptr = strchr(string, ch); //在一个串中查找给定字符的第一个匹配之处 if (ptr) printf("The character %c is at position: %d\n", ch, ptr-string); else printf("The character was not found\n"); strncpy(string, str1, 3); //串拷贝 string[3] = '\0'; printf("%s\n", string); char *str2 = "gh"; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); return 0; } |