STL之string,vector,algorithm常用方法

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

void  testString()
{
    
string s="abc",t="cde";
    cout 
<< s+<< endl;
    s
=s+'1';
    cout 
<< s << " "<< s.size() << endl;

    cin
>>s>>t;
    
int j=s.find(t);
    
if (j!=-1
        cout 
<< j << endl;
    
else
        cout 
<< "no found ";
        
    cin
>>s>>t;
    
if (s==t)
        cout 
<< "equal " ;
    
else if (s>t)
        cout 
<< "large " ;
    
else
        cout 
<< "small " ;
    
    s
="abc defg"//getline(cin,s); 
    int k=s.find(" ");
    
if (k!=-1)
        cout 
<< s.substr(0,k) << " " << s.substr(k+1<< endl;
}


void  prtVector(vector  < int >  v)
{
    
for(int i=0; i<v.size(); i++)
        cout 
<< " "<< v[i];
    cout 
<< endl;
}


bool  cmp( int  a, int  b)
{
    
return a>b;
}


void  testVector()
{
    
int n;
    cin
>>n;
    vector 
<int> v(n);

    
int i;
    
for(i=0; i<v.size(); i++) cin>>v[i];

    sort(v.begin(),v.end());
//sort,small to large
    prtVector(v);    
    
    sort(v.begin(),v.end(),cmp);
//sort,large to small
    prtVector(v);    

    reverse(v.begin(),v.end());
//reverse
    prtVector(v);

    fill(v.begin(),v.end(),
0);//fill
    prtVector(v);

    
int x=1,y=2;
    swap(x,y);
//swap
    cout << x << " " << y << endl; 

    vector 
<vector <int> > tv;
    
int r,c;
    
    cin
>>>>c;

    tv.resize(r);
    
for(i=0;i<r;i++)
    
{
        tv[i].resize(c);
        
for(int j=0;j<tv[i].size();j++)
            cin
>>tv[i][j];
    }


    
for(i=0;i<r;i++)
    
{
        
for(int j=0;j<tv[i].size();j++)
            cout 
<< " "<< tv[i][j];
        cout 
<< endl;
    }


    tv.clear();
}


void  run() {
    cout 
<< "test string ";
    testString();

    cout 
<< " test vector and algorithm ";
    testVector();
}


int  main() {
    run();
    
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值