C++刷题小知识

对容器vector,set,map这些遍历的时候都是用迭代器访问
可以将定义迭代器
vector::itetator 等价于 auto

vector<int >  v(10,2);
for(auto *it = v.begin();it != v.emd();it++)
{
	cout<<*it<<" ";
}

C++STL之集合set的使用:
set是一个集合,一个set里面的各个元素是各不相同的,而且set会按照元素进行从小到大的排序

/****************
* set自动排序
* set自动清除重复值
*****************/
#include <iostream>
#include <set>
using namespace std;
int main()
{
    set<int>  s;
    s.insert(1);
    cout<<*(s.begin())<<endl;

    for(int i=6;i>=0;i--)
    {
        s.insert(i);
    }

    for(auto it=s.begin();it != s.end();it++)
        cout<<*it<<" ";//输出 0 1 2 3 4 5 6
    cout<<endl;

    //能找到2,所以返回 1
    cout<<(s.find(2)!= s.end())<<endl;
    //不能找到10,所以返回 0
    cout<<(s.find(10)!= s.end())<<endl;

    s.erase(1);
    //不能找到1,所以返回 0
    cout<<(s.find(1) != s.end())  <<endl;
    return 0;
}

C++STL之映射map的使用:

/****************
* map<key,value>
* key: 键
* value: 值
* 会根据"值"自动排序
*想输出 key  用m->first
*想输出 value 用m->value
*****************/
#include <iostream>
#include <map>
using namespace std;
int main()
{
    map<string , int> m;
    m["hello"] = 2;
    cout<<m["hello"] << endl; //输出 2

    cout<<m["world"] << endl; //print 0
    m["world"] = 3;

    m[","] = 1;

    //traverse
    /*
    输出:
            , 1
        hello 2
        world 3
    */
    for(auto it = m.begin();it != m.end();it++)
        cout<<it->first<<" "<<it->second<<endl;

        
        
    /*
        print the first elem.  it's key and value
    */
    cout<<m.begin()->first<<" "<<m.begin()->second<<endl;

    
    
    
     /*
        print the last elem.  it's key and value
    */
     cout<<m.rbegin()->first<<" "<<m.rbegin()->second<<endl;

    cout<<m.size()<<endl;
    

C++STL 之队列:

#include <iostream>
#include <queue>
using namespace std;
int main()
{
    queue<int>  q;
    for(int i = 0; i< 6;i++)
    {
        q.push(i); //enqueue 0 1 2 3 4 5
    }

    cout<< q.front() << " "<<q.back()<<endl; //print : 0  5
    cout<< q.size() <<endl;  //print : 6

    q.pop(); //dequeue : 0

    cout<< q.front(); //print :1
    return 0;
}

C++STL之bitset

#include <iostream>
#include <bitset>
using namespace std;
int main()
{
    unsigned int u =1000;
    bitset<5> b(u);
    //初始化方式
    //bitset<5> b; 都为0
    //bitset<5> b(u); u为unsigned int 如果 u=1,则初始化为10000
    //bitset<5> b(s); s为字符串,如“1101” -> "10110"
    //bitset<5> b(s.pos,n);从字符串的s[pos]开始,n位长度

    for(int i=0;i<5;i++)
        cout<<b[i];

    cout<<endl<<b.any();//b中是否存在1的二进制位为1
    cout<<endl<<b.none();//b中不存在1?
    cout<<endl<<b.count();//b中1的二进制位的个数
    cout<<endl<<b.size();//b中二进制位的个数
    cout<<endl<<b.test(1);//测试下标为2是否为1

    b.set(4);//把小标为4的置为1   b:11001

    b.reset();//所有位归零       b:00000

    b.flip();//所有位取反        b:11111

    b.reset(3);  //b的下标3处归零b:11101

    return 0;
}

cctype的使用
中已经定义了字符所属的范围
isalpha(大写+小写字母),islower(小写字母),isupper(大写字母),isalnum(字母大写和数字),isblank(space和\t)。isspace(space,\t,\r,\n)
C++11新特性:
①for循环与auto

forint i-0;i<v.size();i++)
	cout<<v[i];

等价于

forauto i :v)
	cout<<i;

②to_string
to_string();把int型转化为字符串

③stoi,stod
字符串转整型和double

发布了110 篇原创文章 · 获赞 14 · 访问量 6004

猜你喜欢

转载自blog.csdn.net/qq_38173631/article/details/104780177