C++知识点

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <sstream>
 5 using namespace std;
 6 int main()
 7 {
 8     string str;
 9     while(getline(cin,str))
10     {
11         int sum=0,x;
12         stringstream ss(str);
13         while(ss>>x)
14             sum+=x;
15         cout<<"sum="<<sum<<endl;
16         
17     }    
18     return 0;
19 } 
 1 #include <cstdarg>
 2 int sum(int cnt,...)
 3 {
 4     if(cnt<=0)
 5         return 0;
 6     va_list arg_ptr;
 7     va_start(arg_ptr,cnt);
 8     int sum(0);
 9     for(int i=0;i<cnt;i++)
10         sum+=va_arg(arg_ptr,int);
11     va_end(arg_ptr);
12     return sum;
13 }

 cin慢是 有原因的,其实默认的时候,cin与stdin总是保持同步的,也就是说这两种方法可以混用,而不必担心文件指针混乱,同时cout和stdout也一 样,两者混用不会输出顺序错乱。正因为这个兼容性的特性,导致cin有许多额外的开销,如何禁用这个特性呢?只需一个语句 std::ios::sync_with_stdio(false);,这样就可以取消cin于stdin的同步了。

                                                                                         ios_base::sync_with_stdio(false);                                                                                                                    

 

#include <cassert>

    assert(express);   断言;

priority_queue<int ,vector<int>,greater<int> >  pq;

从小到大    greater<typename>;

从大到下   less<typename>;

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 intmain()
 4 {
 5 inti;
 6 int*pn=(int*)malloc(5*sizeof(int));
 7 printf("malloc%p\n",pn);
 8 for(i=0;i<5;i++)
 9 pn[i]=i;
10 pn=(int*)realloc(pn,10*sizeof(int));
11 printf("realloc%p\n",pn);
12 for(i=5;i<10;i++)
13 pn[i]=i;
14 for(i=0;i<10;i++)
15 printf("%3d",pn[i]);
16 free(pn);
17 return0;
18 }

 

 1 template < class T > T MAX(T a[], int len)  
 2    //template<class T> auto MAX(T a[],int len)->decltype(T)   decltype  :  声明类型
 3 {
 4     T res = a[0];
 5     for (int i = 1; i < len; i++)
 6         if (res < a[i])
 7             res = a[i];
 8     return res;
 9 }
10 
11 template<class T1,class T2>
12 auto prod(T1 a, T2 b)->decltype(a*b)   //拖尾返回类型
13 {
14     decltype(a*b) res = a*b;
15     return res;
16 }
17 
18 int main()
19 {
20     
21     int a = 1;
22     double d = 2.0;
23     cout << typeid(prod(a, d)).name() << endl;
24     cout << prod(a, d) << endl;  
25 }    

 

1           size_t p=s.find(',',lastpos+1);
2                 if(p==string::npos)
3                     p=s.length();
4                 db[r][c]=ID(s.substr(lastpos+1,p-lastpos-1));

 

1 string str;  
2     vector<string> words;  
3     while(cin>>str){  
4         words.push_back(str);  
5     }  
6     sort(words.begin(),words.end());  
7     vector<string>::iterator end_unique =  
8                             unique(words.begin(),words.end());  
9     words.erase(end_unique,words.end());

先排序,才能用unique,

unique将相邻的重复的元素移到最后,返回一个iterator指向最后的重复元素

再用erase删除就达到了去除重复的目的

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <stack>
12 #include <sstream>
13 #include <cctype>
14 #include <utility>
15 #include <deque>
16 using namespace std;
17 const int INF = 0x7fffffff;
18 const double EXP = 1e-8;
19 const int MAX=15;
20 struct cmp    //  set, map,若元素不是结构体,则在结构体中  重载函数调用运算符
21 {
22     bool operator ()(const int &a,const int &b)
23     {
24         return a>b;
25     }
26 };
27 
28 int main()
29 {
30     set<int,cmp> s;
31     for(int i=0;i<MAX;i++)
32     {
33         s.insert(i);
34     }
35     set<int,cmp>::iterator it;
36     cout<<endl;
37     for(it=s.begin();it!=s.end();it++)
38         cout<<*it<<" ";
39     cout<<endl<<"------------------------------"<<endl;
40     deque<int>  de;
41     de.push_back(0);
42     de.push_back(1);
43     de.push_back(2);
44     de.push_front(-1);
45     cout<<"the size of deque="<<de.size()<<endl;
46     cout<<de[0]<<" "<<de[1]<<" "<<de[2]<<endl;  //  -1 0 1   可以像数组一样使用小标
47     de.insert(de.begin()+1,100);
48     cout<<de[0]<<" "<<de[1]<<" "<<de[2]<<endl;
49     cout<<"以数组方式遍历deque"<<endl;
50     for(size_t i=0;i<de.size();i++)
51         cout<<de[i]<<"  ";
52     cout<<endl<<"----------反向遍历-------"<<endl;
53     for(deque<int>::reverse_iterator t=de.rbegin();t!=de.rend();t++)
54         cout<<*t<<"  ";
55     cout<<endl<<endl;
56     map<int,char> mp;
57     mp.insert(pair<int,char>(1,'a'));
58     mp.insert(pair<int,char>(2,'b'));
59     map<int,char>::iterator itt;
60     itt=mp.find(1);
61     if(itt!=mp.end())
62     {
63 
64         cout<<(*itt).first<<"  "<<(*itt).second<<endl;
65     }
66 
67     return 0;
68 }

 

posted @ 2015-01-23 23:36  daydaycode  阅读(112)  评论(0编辑  收藏  举报