#include <iostream>
#include <chrono>
#include <assert.h>
#include <thread>
#include <unistd.h>
#include <algorithm>
#include <numeric>
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <array>
#include <string>
#include <list>
#include <forward_list>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
using namespace std;
// method to print items of containers
template<typename T> void print_container(T& c) {
for(typename T::iterator iter=c.begin();iter!=c.end();iter++) {
std::cout << *iter << " ";
}
std::cout << '\n';
}
class TimerClock
{
public:
TimerClock(){
update();
}
~TimerClock(){}
void update(){
_start = chrono::high_resolution_clock::now();
}
double getTimerSecond(){
return getTimerMicroSec() * 0.000001;
}
double getTimerMilliSec(){
return getTimerMicroSec()*0.001;
}
long long getTimerMicroSec(){
return chrono::duration_cast<chrono::microseconds>(chrono::high_resolution_clock::now() - _start).count();
}
private:
chrono::time_point<chrono::high_resolution_clock>_start;
};
int main(){
cout<<" *********** STL code demo by czp **************"<<endl;
//************************** contruct ********************//
// array
array<int,3> arr{1,2,3};
// vector
vector<int> vec{1,2,3};
// string
string str("123");
// deque
deque<int> deq{1,2,3};
// list
list<int> list{1,2,3};
// forwarr_list
forward_list<int> flist{1,2,3};
// container-adapter
// stack
stack<int> stack;
for(int i=1;i<4;++i){
stack.push(i);
}
// queue
queue<int> queue;
for(int i=1;i<4;++i){
queue.push(i);
}
// priority_queue
priority_queue<int> pqueue;
for(int i=1;i<4;++i){
pqueue.push(i);
}
//************************** algorithm ********************//
// test read-only algo
cout<<"---------- Test read-only algorithms -----------"<<endl;
cout<<"Test find..."<<endl;
cout << *find(arr.cbegin(),arr.cend(),2)<<endl;
cout<<"Test count..."<<endl;
cout << count(vec.cbegin(),vec.cend(),2)<<endl;
cout<<"Test accumulate..."<<endl;
cout << accumulate(list.cbegin(),list.cend(),0)<<endl;
cout<<"Test equal..."<<endl;
cout << equal(vec.cbegin(),vec.cend()-1,arr.begin())<<endl; // 1
cout << equal(vec.cbegin(),vec.cend(),str.begin())<<endl; // 0
// test write algo
cout<<"---------- Test write algorithms -----------"<<endl;
cout<<"Test fill..."<<endl;
fill(vec.begin(),vec.end(),2);
cout << equal(vec.cbegin(),vec.cend()-1,arr.begin())<<endl; // 0
cout<<"Test fill_n..."<<endl;
vector<int> vec1(10000);
fill_n(vec1.begin(),10000,1);
cout << count(vec1.cbegin(),vec1.cend()-1,1)<<"size:"<<vec1.size()<<endl; // 9
//print_container(vec1);
auto insert_iter = back_inserter(vec1);
//vec1.reserve(200000);
cout<<"vec1 capacity:"<<vec1.capacity()<<endl;
TimerClock tc;
fill_n(insert_iter,100000,2);
cout<<"time cost:"<<tc.getTimerMicroSec()<<"us"<<endl;
cout<<"vec1 capacity:"<<vec1.capacity()<<endl;
//print_container(vec1);
cout<<"Test replace..."<<endl;
print_container(deq);
replace(deq.begin(),deq.end(),2,22);
print_container(deq);
cout<<"Test copy..."<<endl;
print_container(vec);
copy(deq.begin(),deq.end(),vec.begin());
print_container(vec);
cout<<"Test unique..."<<endl;
vector<int> v1{1,7,2,2,3,2,4,5,4,6};
sort(v1.begin(),v1.end());
auto end_unique = unique(v1.begin(),v1.end());
v1.erase(end_unique,v1.end());
print_container(v1);
cout<<"Test list remove/merge/remove_if..."<<endl;
print_container(list);
auto list1=list;
list.remove(2);
list.remove_if([](int x){return x==3;});
list.merge(list1);
print_container(list);
cout<<"Test set ..."<<endl;
vector<int> v2{1,7,2,2,3,2,4,5,4,6};
set<int> set1(v2.begin(),v2.end());
print_container(set1);
unordered_set<int> set2(v2.begin(),v2.end());
for_each(set2.begin(),set2.end(),[](int n){cout<<n<<" ";});
}