전체 글

·MMOServer
#include "pch.h"#include #include atomic sum;void Add() { //sum++; sum.fetch_add(1);}void Sub() { //sum--; sum.fetch_sub(1);}int main(){ thread t1(Add); thread t2(Sub); t1.join(); t2.join(); std::cout  atomic은 원자성을 의미하며, All or Nothihg하게 적용되는 객체이다.c++에서 전역변수 sum은 section .data일 것이고, bss이기 떄문에 0으로 시작할텐데atomic없이 Add와 Sub함수를 thread를 사용해서 실행하고 sum을 출력하면 0이아닌 이상하게 연산된 값이 출력된다. sum++, sum--이 한줄로 고급언어로..
·MMOServer
#include "pch.h"#include void HelloThread() { cout v; for (int i = 0; i C++ 11부터 표준이된 를 사용해야 여러 운영체제에서 사용가능함.의 ::CreateThread는 windows용이고, Linux는 또 다르다. Thread.hardware_concurrency() : CPU의 논리 프로세서의 개수를 나타냄 즉 동시에 실행할 수 있는 thread 개수Thread.joinable() : thread의 id가 0인지를 확인함. 0이면 쓰레드를 사용할 수 없다Thread.join() : main thread가 끝나도 join()이 끝나지않으면 기다려줌.
·백준
https://www.acmicpc.net/problem/1715#include #include using namespace std;int main() { priority_queue, greater> pq; int N; cin >> N; for (int i = 0; i > tmp; pq.push(tmp); } int sumOfFirstTwoVal = 0; while (pq.size() > 1) { int tmp = pq.top(); pq.pop(); tmp += pq.top(); pq.pop(); pq.push(tmp); sumOfFirstTwoVal += tmp; } std::cout 데이터의 범위를 보니 정렬을 해도 될 것 같은데, 굳이 정렬할 필요 없이 제일 작은 두값을 받아서 합해야..
·백준
https://www.acmicpc.net/problem/1300이진 탐색을 이용하면 간단하지만 그 떠올리는 과정이 상당히 힘들었었던 문제이다.#include using namespace std;int main() { cin.tie(0)->sync_with_stdio(false); int N; long long K; cin >> N >> K; //B[k] K보다 작거나 같은 수가 적어도 K개 있다. long long start = 1; long long end = K; long long ans = 0; while (start N) { count += N; } else { count += (mid / i); } } if (count a[N][N] 을 b[N^2]으로 1차원 배열로..
·백준
https://www.acmicpc.net/problem/2343간단한 이진 탐색 문제이다. #include #include #include using namespace std;templateint BinarySearch(const T&, int);int main() { cin.tie(0)->sync_with_stdio(false); int lessonNum, bluRayNum; cin >> lessonNum >> bluRayNum; vector v(lessonNum); for (auto& it : v) { cin >> it; } cout int BinarySearch(const T& container, int bluRayNum) { typename T::const_iterator iter = con..
·c++
#include #include #include using namespace std;int main() { vector v(10); int num = 1; for(auto& it: v) { it = num++; }// std::find // auto iter = find(v.begin(), v.end(), 11); // if(iter != v.end()) { // cout ::iterator iter = remove_if(v.begin(), v.end(), [](int tmp) {return (tmp % 2);}); // 홀수면 제거하고 싶을 때 v.erase(remove_if(v.begin(), v.end(), [](int tmp) {ret..
·c++
#include using namespace std;templateclass Node {public: Node(): next_(nullptr), prev_(nullptr), data_(T()) {} Node(const T& data): next_(nullptr), prev_(nullptr), data_(data) {}public: Node* next_; Node* prev_; T data_;};templateclass Iterator {public: Iterator() : node_(nullptr) {} Iterator(Node* node): node_(node) {} Iterator& operator++() { node_ = node_->next_; return *this; } Iterator o..
·c++
#include templateclass Iterator {public: Iterator() : ptr_(nullptr) {} Iterator(T* ptr) : ptr_(ptr) {} Iterator& operator++() { ptr_++; return *this; } Iterator operator++(int) { Iterator tmpItr = *this; ptr_++; return tmpItr; } Iterator& operator--() { ptr_--; return *this; } Iterator operator--(int) { Iterator tmpItr = *this; ptr_--; return tmpItr; } Iterator operator+(const int coun..
·c++
타입 캐스팅---------------------------------------------타입 변환 유형---------------------------------------------1.값 타입 변환 의미를 유지하기 위해서, 원본 객체와 다른 비트열 재구성int a = 5; float b = a;   클래스에서는 일반적으로 안 됨 (예외: 타입 변환 생성자, 타입 변환 연산자) class Dog() {public:            Dog(const Knight& k){ // 타입 변환 생성            k.age = age_;}            operator Knight() {return (Knight)(*this)} // 타입 변환 연산자 }Knight knight;Dog dog =..
·c++
#include class Knight {protected: int hp_; int attack_; int positionX_; int positionY_;public: Knight(): hp_(100), attack_(1), positionX_(2), positionY_(3) { // 기본 생성자 } Knight(const Knight& k) { // 복사 생성자 hp_ = k.hp_; attack_ = k.attack_; positionX_ = k.positionX_; positionY_ = k.positionY_; } Knight(int hp) : hp_(hp), attack_(0), positionX_(0), positionY_(0) {} // 타입 변환 생성자 Knight k1 = 1; ..
이야기prog
이야기