C++11中,STL的使用方法之2

1)unordered_set(无序集合)与 set(有序集合) 的使用方法,代码如下:

#include<iostream>
#include<unordered_set>
#include<set>
using namespace std;
int main(void)
{
    //需要包含unordered_set头文件
    std::unordered_set<int> set1; //unordered 无序的
    set1.insert(12);
    set1.insert(14);
    set1.insert(11);
    set1.insert(17);
    set1.insert(15);
    for(auto e : set1)
    {
        cout<<e<<" ";
    }
    cout<<endl;
    //需要包含set头文件
    std::set<int> set2;
    set2.insert(12);
    set2.insert(12); //只能包含一次,再次插入没有意义
    set2.insert(14);
    set2.insert(11);
    set2.insert(17);
    set2.insert(15);
    for(auto e : set2)
    {
        cout<<e<<" ";
    }
    return 0;
}

输出结果:

15 17 11 12 14
11 12 14 15 17

2)unordered_map(映射)的使用方法1,代码如下:

#include<unordered_map>
#include<iostream>
using namespace std;
int main(void)
{
    std::unordered_map<int,string> un_map;
    un_map.insert(std::pair<int,string>(20,"jiang"));
    un_map.insert(std::pair<int,string>(30,"zhang"));
    un_map.insert(std::pair<int,string>(40,"lu"));
    un_map.insert(std::pair<int,string>(50,"wangxingli"));

    std::unordered_map<int,string>::iterator it;
    for(it = un_map.begin(); it != un_map.end(); it++)
    {
        cout<<it->first<<" "<<it->second<<endl; //这个输入it.的时候,不智能提示,一直以为自己写错了
    }
    return 0;
}

测试结果:

50 wangxingli
40 lu
20 jiang
30 zhang
  1. 根据老师学习的unordered_map,代码如下:
#include<iostream>
#include<unordered_map>
using namespace std;
typedef unordered_map<int,string> myUnMap; //这个dypedef重命名需要学习
myUnMap merge(myUnMap str1, myUnMap str2) //这个字符串合并函数需要学习
{
    myUnMap temp(str1);
    temp.insert(str2.begin(),str2.end());
    return temp;
}
int main(void)
{
    myUnMap map1; //直接定义变量
    myUnMap map2{{23,"jiang"},{44,"luwen"}}; //数组初始化
    myUnMap map3{{56,"jia6666g"},{46,"5555555"}};//数组初始化
    myUnMap map4(map2); // 复制初始化
    myUnMap map5(merge(map4,map3)); //移动初始化
    myUnMap map6(map5.begin(),map5.end()); //范围初始化操作
    for(auto& e : map6)
    {
        cout<<e.first<<" "<<e.second;
    }
    return 0;
}

测试结果如下:

44 luwen23 jiang46 555555556 jia6666g

4)函数对象的使用案例,代码如下:

#include<iostream>
#include <set>
using namespace std;

class A
{
public:
    A(string tmp1,string tmp2)
    {
        cout<<"A constructor..."<<endl;
        this->name1 = tmp1;
        this->name2 = tmp2;
    }

    string getName1(void) const
    {
        return name1;
    }

    string getName2(void) const
    {
        return name2;
    }

private:
    string name1;
    string name2;
};

class B
{
public:
    bool operator()(const A& a1,const A& a2) const
    {
        return (a1.getName2() < a2.getName2()) || (a1.getName2() == a2.getName2() &&
            (a1.getName1() < a2.getName1()));
    }
};

int main(void)
{
    set<A, B> sett; //可以定义一个类型,也可以定义两个类型

    A t1("liu", "san");
    A t2("xiao", "ming");
    A t3("zhang", "san");
    A t4("wang", "xiao");

    sett.insert(t1);
    sett.insert(t2);
    sett.insert(t3);
    sett.insert(t4);

    for (auto i : sett)
    {
        cout << i.getName2() << "," << i.getName1() << endl;
    }

    cout << endl;

    return 0;
}

输出结果:

A constructor...
A constructor...
A constructor...
A constructor...
ming,xiao
san,liu
san,zhang
xiao,wang

4) atomic_flag的使用案例,代码如下:
atomic_flag 只支持两种操作:test-and-set 和 clear。

#include<iostream>
#include<atomic>
#include<vector>
#include<thread>

using namespace std;

std::atomic_flag lock = ATOMIC_FLAG_INIT; //定义原子布尔类型

void funAt(int args)
{
    for(int i = 0; i < 5; i++)
    {
        while (lock.test_and_set(std::memory_order_acquire)); //得到锁,说白了就是让锁的值为真
        std::cout<<"out threads: "<<i<<endl;
        lock.clear(std::memory_order_release); //释放锁,让锁值为假
    }
}

int main(void)
{
    std::vector<std::thread> vct;
    for(int i = 0; i < 2; i++)
    {
        vct.emplace_back(funAt,i);
    }
    for(auto& t : vct)
    {
        t.join();
    }
    return 0;
}

输出结果:

out threads: 0
out threads: 0
out threads: 1
out threads: 1
out threads: 2
out threads: 2
out threads: 3
out threads: 3
out threads: 4
out threads: 4

5)条件变量condition_variable的使用,代码如下:
条件变量用法:条件变量一般都和互斥锁一起用的;

#include<iostream>
#include<thread>
#include<condition_variable>
#include<mutex>

using namespace std;

std::mutex mx;
std::condition_variable scv;
bool ready = false;

void printID(int Id)
{
    std::unique_lock<std::mutex> lock(mx); //注意:还有个unique_ptr,这个是独占指针,不要弄错了
    while (!ready)
    {
        scv.wait(lock);
    }
    std::cout<<"thread id is : "<<Id<<endl;
}

void run(void)
{
    std::unique_lock<std::mutex> lock(mx);
    ready = true;
    scv.notify_all();
}

int main(void)
{

    std::thread ths[5];
    for(int i = 0; i < 5; i++)
    {
        ths[i] = std::thread(printID,i); //创建线程的方法要知道
    }
    std::cout<<"5 threads ready running......"<<std::endl;
    run();
    for(auto& t : ths)
        t.join();
    return 0;
}

输出结果:

5 threads ready running......
thread id is : 1
thread id is : 4
thread id is : 0
thread id is : 3
thread id is : 2

6)stdexcept头文件的bad_cast的使用,代码如下:
//dynamic_cast < type-id > ( expression)
//作用:该运算符把expression转换成type-id类型的对象;

#include<iostream>
#include<stdexcept> //这个头文件里面包含exception

using namespace std;

class A
{
public:
    virtual void running(void)
    {
        cout<<"A class running...."<<endl;
    }
};

class B : public A
{
public:
    virtual void running(void)
    {
        cout<<"B class running....********"<<endl;
    }
};
int main(void)
{
    A a;
    try //注意try没有小括号
    {    //a->b不安全转换(因为不能全部包含); b->a安全转换(b可以全部包含a)
         B& b = dynamic_cast<B&>(a);  //相当于把a转换为b,就这么简单,真的没办法!
         cout<<"hello world"<<endl;   //执行不到这句话,因为上面抛出异常了
    }
    catch(bad_cast& e)
    {
        cerr<<e.what()<<endl;
    }
    return 0;
}

测试效果:

std::bad_cast

7)C++11的简单线程案例,代码如下:

#include <iostream>
#include <thread>
using namespace std;

void ThreadFunc1()
{
    cout << "ThreadFunc1()--A\n" << endl;
    this_thread::sleep_for(std::chrono::seconds(2));
    cout << "ThreadFunc1()--B" << endl;
}
void ThreadFunc2(int args,string sp)
{
    cout << "ThreadFunc2()--A" << endl;
    this_thread::sleep_for(std::chrono::seconds(7));
    cout << "ThreadFunc2()--B" << endl; //这条语句不会打印,因为线程分离了
}
int main()
{
    thread ths1(ThreadFunc1); //线程构造函数
    thread ths2(ThreadFunc2,10,"LS"); //线程构造

    ths1.join();
    cout << "join" << endl;
    ths2.detach();
    cout << "detach" << endl;
    return 0;
}

输出结果:

ThreadFunc1()--A

ThreadFunc2()--A
ThreadFunc1()--B
join
detach
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值