STL --C++语言新特性2
unordered_set
数据存储机构也是采用哈希表的方式结构操作,此外,std::unordered_set在插入时不会自动排序。
//unordered_set
#include <iostream>
#include <string>
#include <set>
#include <unordered_set>
using namespace std;
int main()
{
std::unordered_set<int> un_set;
un_set.insert(23);
un_set.insert(33);
un_set.insert(12);
un_set.insert(78);
un_set.insert(99);
cout << "\nunordered_set:" << endl;
for (auto it : un_set)
{
cout << it << endl;
}
cout << endl;
std::set<int> st;
st.insert(23);
st.insert(33);
st.insert(12);
st.insert(78);
st.insert(99);
cout << "\n" << endl;
for (auto it : st)
{
cout << it << endl;
}
cout << endl;
return 0;
}
关联容器:unordered_map
内部采用hash表结构,具备快速检索的功能。特性:关联性,无序性,map,唯一性
//unordered_map
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
typedef unordered_map<string, string> strmap;
strmap merge(strmap str1, strmap str2)
{
strmap temp(str1);
temp.insert(str2.begin(),str2.end());
return temp;
}
int main()
{
strmap s1;
strmap s2({ {"apple","red"},{"lemon","yellow"} });//使用数组初始化
strmap s3({ {"orange","orange"},{"strawberry","red"} });//使用数组初始化
strmap s4(s2);//复制初始化
strmap s5(merge(s3, s4));//移动初始化操作
strmap s6(s5.begin(), s5.end());//范围初始化
cout << "\n输出s6容器:\n";
for (auto& x : s6)
cout << " " << x.first << ":" << x.second;
cout << endl;
return 0;
}
function 函数对象
//function 函数对象
//函数对象指定义operator()的对象。优势比普通函数要灵活(可以拥有状态),执行速率比函数指针要快
#include <iostream>
#include <set>
using namespace std;
class TestA
{
public:
TestA(string lname, string fname) :_fname(fname), _lname(lname)
{
cout << "执行TestA类的构造函数\n";
}
string firstname()const
{
return _fname;
}
string lastname()const
{
return _lname;
}
private:
string _fname = nullptr;
string _lname = nullptr;
};
class TestB
{
public:
bool operator()(const TestA& t1, const TestA& t2)const
{
return t1.lastname() < t2.lastname() || t1.lastname() == t2.lastname() &&
t1.firstname() < t2.firstname();
}
};
int main()
{
set<TestA, TestB> sett;
TestA t1("liu", "san");
TestA t2("xiao", "ming");
TestA t3("zhang", "san");
TestA t4("wang", "xiao");
sett.insert(t1);
sett.insert(t2);
sett.insert(t3);
sett.insert(t4);
for (auto i : sett)
{
cout << i.lastname() << "," << i.firstname() << endl;
}
cout << endl;
return 0;
}
automic_flag
//automic_flag:原子布尔型,不提供加载或存储操作
#include <iostream>
#include <atomic>
#include <vector>
#include <thread>
using namespace std;
//ATOMIC_FLAG_INIT-->定义能以语句用于初始化操作,清楚状态的初始化器
atomic_flag lock = ATOMIC_FLAG_INIT;
void FuncAt(int args)
{
for (int i = 0; i < 10; i++)
{
while (lock.test_and_set(memory_order_acquire));//获得锁
cout << "Output Threads:" << i << endl;
lock.clear(memory_order_release);//释放锁
}
}
int main()
{
vector<thread> vct;
for (int i = 0; i < 2; i++)
{
vct.emplace_back(FuncAt, i);
}
for (auto& t : vct)
{
t.join();
}
return 0;
}
条件变量:condition_variable
使用条件变量:condition_variable实现多线程间同步操作,当条件不满足时,相关线程被一直阻塞,直到某种条件出现,这些线程才会被唤醒
//条件变量:condition_variable
#include <iostream>
#include <thread>
#include <condition_variable>
#include <mutex>
using namespace std;
mutex mx;
condition_variable scv;
bool ready = false;
void PrintID(int id)
{
unique_lock<mutex> lock(mx);
while (!ready)
{
scv.wait(lock);//当前线程被阻塞,当全局标志位变为true之后,才唤醒
}
cout << "Threads:" << id << endl;
}
void RunFunc()
{
unique_lock<std::mutex> lock(mx);
ready = true;//设置全局标志位为true
scv.notify_all();//唤醒所有线程
}
int main()
{
thread thrs[5];
for (int i = 0; i < 5; i++)
thrs[i] = thread(PrintID, i);
cout << "5 thread ready to race......\n";
RunFunc();
for (auto& t : thrs)
{
t.join();
}
return 0;
}
//请注意join函数和detch函数
异常处理:exception
//exception 头文件<stdexcept>
//bad_typed/bad_alloc/ios_base::failure
#include <iostream>
#include <stdexcept>
using namespace std;
class TestA
{
virtual void Func()
{
}
};
class TestB:public TestA
{
public:
void disp()
{
cout<<"TestB OK."<<endl;
}
};
void dispObject(TestA&t)
{
try
{
TestB &tb=dynamic_cast<TestB&>(t);
//在此转换若不安全,会抛出bad_cast异常
tb.disp();
}
catch (bad_cast&e)
{
cerr<<e.what()<<endl;
}
}
int main()
{
TestB objb;
dispObject(objb);
return 0;
}
std::thread 多线程
//std::thread 多线程
//-pthread
//编译和执行
//编译g++ 文件名 -o 执行文件名
//执行./执行文件
#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;
}

本文介绍了C++19中的一些新特性,包括STL的unordered_set和unordered_map,它们提供了高效的数据存储和检索。此外,还讲解了function函数对象、atomic_flag原子标志、condition_variable条件变量用于多线程同步,以及异常处理机制和std::thread多线程的支持。
14万+

被折叠的 条评论
为什么被折叠?



