初学C++

来源:互联网 发布:如何举报淘宝卖家 编辑:程序博客网 时间:2024/06/05 19:46
#include <iostream>using namespace std;int main(){    cout << "Hello Word!" << endl ;    int a ,b ;    cin >> a >> b ;    cout << a+b << endl ;    return 0;}
类似于动态数组,删除操作很方便
#include <iostream>#include <vector>using namespace std;vector <int> p ;int main(){    int n , x ;    p.clear();    cin >> n ;    for(int i = 0 ; i < n ; i++)    {        cin >> x ;        p.push_back(x) ;    }    for(int i = 0 ; i < n ; i++)        cout << p[i] << " " ;    cout << endl ;    cout << "***" << endl ;    vector <int>::iterator it ;    for(it = p.begin() ; it != p.end() ; it++)        cout << (*it) << endl ;    cout << "***" << endl ;    it = p.begin();    p.erase(it) ;    // erase 删除其中一个    cout << (*it) << endl ;    cout << p.size() << endl ;    cout << "***" << endl ;    it = p.begin();    p.erase(it, it+3) ;    // erase 由it开始一直删除到it+3;    cout << (*it) << endl ;    cout << p.size() << endl ;    cout << "***" << endl ;    vector <int>::iterator itt ;    for(itt = p.begin() ; itt != p.end() ; itt++)        cout << (*itt) << endl ;    return 0;}


队列  先进先出
#include <iostream>#include <queue>using namespace std ;int main(){    queue <int> p ;    int n , x ;    cin >> n ;    for(int i = 0 ; i < n ; i++){        cin >> x ;        p.push(x) ;    }    while( !p.empty() ){        int u = p.front() ;        cout << u << " " << endl ;        p.pop();    }    cout << endl ;    return 0;}

优先队列   在结构体中定义

#include <iostream>#include <queue>#include <utility>using namespace std ;struct node{    int x , y ;    bool operator < (const node &tmp) const {        return x > tmp.x ;    }};priority_queue <node> p ;int main(){    int n ;    node tmp ;    while(!p.empty())        p.pop() ;    cin >> n ;    for(int i = 0 ; i < n ; i++){        cin >> tmp.x >> tmp.y ;        p.push(tmp) ;    }    cout << endl ;    while( !p.empty()){        tmp = p.top();        cout << tmp.x << " " << tmp.y << endl ;        p.pop();    }    return 0;}
去掉重复输入 判断有没有输入
#include <iostream>#include <set>using namespace std ;//去掉重复输入set <int> st ;int main(){    int n , x ;    cin >> n ;    for(int i = 0 ; i < n ; i++){        cin >> x ;        st.insert(x) ;    }    set<int> :: iterator it ;    for(it = st.begin() ; it != st.end() ; it++){        cout << (*it) << endl ;    }    //查找 是否存在3    if(st.find(3) != st.end())        cout << "YES" << endl ;    else        cout << "NO" << endl ;    return 0;}

#include <iostream>#include <string>#include <map>#include <utility>using namespace std ;//map 建立映射map <string,int> f ;int main(){    int n , ct = 0 ;    string str ;    cin >> n ;    for(int i = 0 ; i < n ; i++){        cin >> str ;        f[str] = ++ct ;    }    cout << "--- INPUT SUCCEED ---" << endl ;    for(int i = 0 ; i < 3 ; i++){        cin >> str ;        cout << f[str] << endl ;    }//直接添加映射    f.insert(make_pair("abc",123));    cout << f["abc"] << endl ;    return 0;}
栈的实现
#include <iostream>#include <stack>using namespace std ;//у╩ ох╫Ь╨СЁЖstack <int> st ;int main(){    int n , x ;    cin >> n ;    for(int i = 0 ; i < n ; i++){        cin >> x ;        st.push(x) ;    }    cout << endl ;    while(!st.empty()){        x = st.top() ;        cout << x << endl ;        st.pop() ;    }    return 0;}
整形转化为二进制
#include <iostream>#include <bitset>using namespace std;//整数转化为二进制int main(){    int x ;    cin >> x ;    bitset<16> bit (x) ; // bitset<长度> 变量名 (转化量)   转化得到的变量类似数组操作    cout << bit << endl ;    return 0;}




0 0
原创粉丝点击