紫书 总结 5.4

swap

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void swap1(int& a,int& b)
{
    int t=a;a=b;b=t;
}
int main()
{
    int a,b;
    cin>>a>>b;
    swap1(a,b);
    cout<<a << " "<<b<<endl;
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
void swap1(int *a,int *b)
{
    int t=*a;
    *a = *b;
    *b = t;
}
int main()
{
    int a,b;
    scanf("%d%d",&a,&b);
    swap1(&a,&b);
    printf("%d %d\n",a,b);
    return 0;
}

string流

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    string line;
    while(getline(cin,line))
    {
        int sum=0,x;
        stringstream ss(line);
        while(ss>>x) sum+=x;
        cout<<sum<<endl;
    }
    return 0;
}

结构体初始化和定义运算

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Point
{
    int x,y;
    Point(int x=0,int y=0):x(x),y(y){} //对结构体赋值
};
Point operator + (const Point& A,const Point& B) // 计算方式
{
    return Point(A.x+B.x,A.y+B.y);
}
ostream& operator << (ostream &out, const Point& p){ //输出方式
    out<<"("<<p.x<<","<<p.y<<")";
}
int main()
{
    Point a,b(2,22);
//    cout<<a.x<<" "<<a.y<<endl;
//    cout<<b.x<<" "<<b.y<<endl;
    a.x=3;
    cout<<a+b<<endl;
    return 0;
}

vector

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector <int> a;//a[]
    a.push_back(5); //在结尾插入
    a.push_back(4);
    cout<<a[0]<<" "<<a[1]<<endl;
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    a.pop_back(); //删除结尾
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    a.resize(10); //改变大小
    for(int i=0;i<a.size();i++)
    {
        cout<<a[i]<<" ";
    }
    cout<<endl;
    cout<<a.size()<<endl; //长度
    cout<<a.at(0)<<endl;//输出a[0]
    cout<<a.front()<<endl;//输出开头
    cout<<a.back()<<endl;
    if(!a.empty()) cout<<"yes"<<endl;
    else cout<<"no"<<endl;
    a.clear();
    cout<<a.size()<<endl;
    if(!a.empty()) cout<<"yes"<<endl;  //判空
    else cout<<"no"<<endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值