一级目录
#include<iostream>
#include<mutex>
#include<condition_variable>
#include<queue>
#include<thread>
#include<stack>
#include<stdio.h>
#include<vector>
#include<unordered_map>
#include<time.h>
#include<algorithm>
using namespace std;
typedef char ElemType;
typedef struct BtNode
{
ElemType data;
BtNode* leftchild;
BtNode* rightchild;
BtNode* parent;
}BtNode, * BinaryTree;
#if 0
int find(vector <int>& a)
{
while (1)
{
srand(time(NULL));
int index = rand() % a.size();
int cnt = 0;
int ma = a[index];
for (int i = 0; i < a.size(); ++i)
{
if (a[i] == ma)
{
cnt += 1;
}
}
if (cnt > a.size() / 2)
{
return ma;
}
}
}
int main()
{
vector<int> a = { 2,3,3,3,3,3,3,3,6,7,8,8,9 };
cout << find(a) << endl;
}
int find(vector<int>& a)
{
sort(a.begin(), a.end());
return a[a.size() / 2];
}
int find(vector<int>& a)
{
int cot = 0;
int m = a.front();
for (int i = 0; i < a.size(); ++i)
{
if (m == a[i])
{
cot++;
}
else
{
cot--;
}
if (cot == 0)
{
m = a[i + 1];
}
}
return m;
}
int find(vector<int>& a)
{
unordered_map<int, int> counts;
int ma = 0, cnt = 0;
for (auto x : a)
{
++counts[x];
}
int n = a.size() / 2;
for (auto x : counts)
{
if (x.second > n)
{
return x.first;
}
}
return -1;
}
int Find(vector<int>& a, int val)
{
int pos = -1;
int left = 0;
int right = a.size() - 1;
while (left <= right)
{
int mid = left + (right - left) / 2;
if (val <= a[mid])
{
right = mid - 1;
}
else if (val > a[mid])
{
left = mid + 1;
}
else
{
while (mid > 0 && a[mid - 1] == val)
{
--mid;
}
break;
}
}
return pos;
}
int main()
{
vector<int> a = { 1,2,3,4,5,6,7,8 };
Find(a, 10);
}
typedef struct ListNode
{
int data;
struct ListNode* next;
}ListNode;
void Insert(ListNode* head, int value)
{
ListNode* p = new ListNode();
p->data = value;
p->next = NULL;
if (head == NULL)
{
head = p;
}
else
{
ListNode* node = head;
while (node->next != NULL)
{
node = node->next;
}
node->next = p;
}
}
mutex mtx;
condition_variable cv;
queue<int> q;
int dat = 0;
int maxsize = 10;
void Produce()
{
while (true)
{
this_thread::sleep_for(chrono::microseconds(1000));
unique_lock<mutex> lock(mtx);
cv.wait(lock, []() {
return q.size() < maxsize;
});
q.push(++dat);
cout << "p id" << this_thread::get_id() << "dat" << dat << " q.size" << q.size() << endl;
cv.notify_all();
}
}
void Consumer()
{
while (true)
{
this_thread::sleep_for(chrono::microseconds(500));
unique_lock<mutex> lock(mtx);
cv.wait(lock, []()
{
return q.size() > 0;
});
int c_data = q.front();
q.pop();
cout << "c id" << this_thread::get_id() << "c_data " << c_data << " q.size " << q.size() << endl;
cv.notify_all();
}
}
int main()
{
thread t1[3];
thread t2[3];
for (auto& t : t1)
{
t = thread(Consumer);
}
for (auto& t : t2)
{
t = thread(Produce);
}
for (auto& t : t1)
{
t.join();
}
for (auto& t : t2)
{
t.join();
}
return 0;
}
template<class T>
class Stack
{
public:
Stack(int size);
~Stack();
bool Empty();
bool IsFull();
bool pop();
bool Push(T value);
void traverse();
bool StackClear();
int stackLengh();
private:
T value;
T* base;
T top;
int maxsize;
};
template<class T>
Stack<T>::Stack(int size)
{
maxsize = size;
base = new T[size];
top = 0;
}
template<class T>
Stack<T>::~Stack()
{
delete[] base;
base = NULL;
}
template<class T>
bool Stack<T>::IsFull()
{
return top = maxsize;
}
template<class T>
bool Stack<T>::Empty()
{
if (top = base)
{
return true;
}
return false;
}
int main()
{
stack<int> s;
int sum = 0;
for (int i = 0; i <= 10; ++i)
{
s.push(i);
}
cout << "size is" << s.size() << endl;
while (!s.empty())
{
cout << " " << s.top() << endl;
s.pop();
}
cout << endl;
return 0;
}
mutex mtx;
condition_variable cv;
int maxsize = 10;
int dat = 0;
queue<int> q;
void produce()
{
while (1)
{
this_thread::sleep_for(chrono::microseconds(1000));
unique_lock<mutex> lock(mtx);
cv.wait(lock, []() {
return q.size() < maxsize;
});
q.push(++dat);
cout << "p id" << this_thread::get_id() << " dat:" << dat <<" q.size"<< q.size() << endl;
cv.notify_all();
}
}
void consume()
{
while (1)
{
this_thread::sleep_for(chrono::microseconds(500));
unique_lock<mutex> lock(mtx);
cv.wait(lock, []() {
return q.size() > 0;
});
int c_data = q.front();
q.pop();
cout << "c id" << this_thread::get_id() << "c_data:" << c_data << " q.size" << q.size() << endl;
cv.notify_all();
}
}
int main()
{
thread t1[3];
thread t2[3];
for (auto& t : t1)
{
t = thread(consume);
}
for (auto& t : t2)
{
t = thread(produce);
}
for (auto& t : t1)
{
t.join();
}
for (auto& t : t2)
{
t.join();
}
return 0;
}
void fun()
{
cout << " 线程" << endl;
}
int main()
{
thread t1(fun);
t1.detach();
return 0;
}
class Queue
{
public:
Queue() :qu() {}
~Queue() {}
void put(int val)
{
unique_lock<mutex> lock(mtx);
qu.push_back(val);
}
int get()
{
unique_lock<mutex> lock(mtx);
int val = qu.front(); qu.pop_front();
return val;
}
private:
deque<int> qu;
std::mutex mtx;
};
const int MaxElem = 5;
Queue qu;
std::counting_semaphore psm(MaxElem);
std::counting_semaphore csm(0);
std::mutex mtx;
const int n = 100;
int val = 0;
void Producer(int id)
{
for (int i = 0; i < n; ++i)
{
psm.acquire();
this_thread::sleep_for(std::chrono::milliseconds(10));
cout << "Producer id: " << id << "Prod Elem: " << ++val << endl;
qu.put(val);
csm.release(1);
}
cout << "Producer end" << endl;
}
void Consumer(int id)
{
for (int i = 0; i < n; ++i)
{
csm.acquire();
this_thread::sleep_for(std::chrono::milliseconds(200));
int x = qu.get();
cout << "Consumer: id " << id << " Cons Elem: " << &x << " " << x << endl;
psm.release(1);
}
cout << "Consumer end" << endl;
}
int main()
{
std::thread prod1(Producer, 1);
std::thread prod2(Producer, 2);
std::thread cons1(Consumer, 1);
std::thread cons2(Consumer, 2);
prod1.join();
prod2.join();
cons1.join();
cons2.join();
return 0;
}
#endif