STL学习总结

1.string :


1.string erase函数的使用:

#include <iostream>
#include <string>
using namespace std;
int main ()
{  
    string str ("This is an example phrase.");  
    string::iterator it;  
    // 第(1)种用法  
    str.erase (10,8);  
    cout << str << endl;        // "This is an phrase."  
    // 第(2)种用法  
    it=str.begin()+9;  
    str.erase (it);  
    cout << str << endl;        // "This is a phrase."  
    // 第(3)种用法  
    str.erase (str.begin()+5, str.end()-7);  //左闭右开
    cout << str << endl;        // "This phrase." 
    // 第(4)种用法 
    str.erase(1); //只留下一个元素
    cout<<str<<endl;           //"T" 
    return 0;
}

详细讲解:http://blog.csdn.net/liang5630/article/details/7752673


2.map :

1. map一对多的使用:

#include <iostream>
#include <map>
#include <vector>
using namespace std;
map<int,vector<int> > d;
int main(int argc, char *argv[])
{
    int n,m,i,j,k,v;
    while(cin>>n>>m)
    {
        for(i=1;i<=n;i++)
        {
            cin>>v;
            if(!d.count(v)) d[v]=vector<int>();//此处的count用法 统计map中key出现的次数
            d[v].push_back(i);
        }
        while(m--)
        {
            cin>>k>>v;
            if(!d.count(v)||k>d[v].size()) cout<<0<<endl;
            else cout<<d[v][k-1]<<endl;
        }
        d.clear();  
    }
    return 0;
}

*. count( ):

count():统计key在地址区间内出现的次数 
用法:cout<<count(ivec.begin() , ivec.end() , searchValue) ;

对比:
count_if( ) : 返回满足条件的地址区间内的元素出现次数 (需定义perdicate函数)。


#include <vector>
#include <algorithm>
#include <iostream>

bool greater10(int value)//predicate函数只返回真或假 
{
    return value >10;
}

int main()
{
    using namespace std;
    vector<int> v1;
    vector<int>::iterator Iter;

    v1.push_back(10);
    v1.push_back(20);
    v1.push_back(10);
    v1.push_back(40);
    v1.push_back(10);

    cout << "v1 : ";
    for (Iter = v1.begin(); Iter != v1.end(); Iter++)
       cout << *Iter << " ";
    cout << endl;


    vector<int>::size_type  result1 = count_if(v1.begin(), v1.end(), greater10);
      //count_if算法返回使谓词函数返回条件成立的元素个数
    cout << "The number of elements in v1 greater than 10 is: "
         << result1 << "." << endl;

    return 0;
}

详细讲解:http://www.cnblogs.com/heyonggang/archive/2013/08/063240889.html

2. map按value值排序

#include<cstdio>
#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;

typedef pair<string,int> PAIR; 

bool cmp(pair<string,int>  a,pair<string,int>  b)//val值升序 
{
    return a.second<b.second;
}

bool CMP(PAIR a,PAIR b)//按key值降序 
{
    return a.firstb>b.first;
 } 

struct Node{//与<不同  val升序 
    bool operator ()(pair<string ,int > a,pair<string ,int > b)
    {
        return a.second<b.second;
    }
};

int main()
{
    int i=0,tem;
    map<string,int> MP;
    char str[100];
    for(i=1;i<=4;++i)
    {
        scanf("%s %d",str,&tem);
        MP.insert(make_pair(str,tem));  
     } 
     vector<pair<string,int> >  VEC(MP.begin(),MP.end());//不能用insert(*b,*e) 

     sort(VEC.begin(),VEC.end(),CMP);
//   sort(VEC.begin(),VEC.end(),Node());
//   sort(VEC.begin(),VEC.end(),cmp);
//   
     vector<pair<string,int> >::iterator iter;
     for(iter=VEC.begin();iter!=VEC.end();++iter)
     {
         cout<<iter->first<<'\t'<<iter->second<<endl; 
       }  
    return 0;
}

详细讲解:http://www.cnblogs.com/heyonggang/archive/2013/08/07/3243477.html


new 和delete用法:
动态申请内存(主要在题目中没有给出数据多少时用,如果用数组wa而且数组开的度掌握不好,超内存,就用这个):

#include<cstdio>
using namespace std;

int main()
{
    int *a=new int; scanf("%d",a); printf("%d\n",*a); 
    delete a; //释放的是内存,指针的指向并没有变 
    printf("%d\n",a);//可以查看地址

    a=new int(4); printf("%d\n",*a);//申请内存并赋值 

    int *b=new int[2]; scanf("%d %d",b,b+1); printf("%d %d\n",b[0],b[1]);
    delete []b;//二维及以上的还没有搞懂 

    int (*b)[2]=new int[2][2];//不能用int **b定义 行指针 
    return 0;
}

详细讲解(二维错误):http://www.cnblogs.com/jjzhou1988/archive/2008/11/30/1344314.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值