std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();

int tmpSec = 0;

while (1)

{

std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();

std::chrono::seconds sec = std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime);

if (sec.count() > tmpSec)

{

tmpSec++;

printf("%d ", sec.count());

}

}

'c++' 카테고리의 다른 글

std::function 쓰는이유  (0) 2015.07.13
C++11 move semantic 관련  (0) 2015.07.12
대각선 이동  (0) 2015.03.19
std::find_if, std::lamda  (0) 2015.02.05
std::function 배열, 벡터  (0) 2015.01.08
Posted by 에브리피플
,

std::function 쓰는이유

c++ 2015. 7. 13. 10:36

 std::function을 사용하는 것은 람다 자체를 함수 인수로 넣을 때나, 람다를 넣을 STL 컨테이너를 만드는 등의 작업을 할 때 쓰일 수 있으니 기억해 주자.

'c++' 카테고리의 다른 글

std::chrono 시간 카운트 계산 초단위  (0) 2016.10.24
C++11 move semantic 관련  (0) 2015.07.12
대각선 이동  (0) 2015.03.19
std::find_if, std::lamda  (0) 2015.02.05
std::function 배열, 벡터  (0) 2015.01.08
Posted by 에브리피플
,

C++11 move semantic 관련

c++ 2015. 7. 12. 17:42

#include "stdafx.h"

#include <string>

#include <iostream>

using namespace std;

class Test

{

static const int size;

private:

char* pData;

public:

Test()

{

cout << "디폴트" << endl;

pData = new char[size];

}

Test(const Test& other)

{

cout << "복사" << endl;

pData = new char[size];

memcpy(pData, other.pData, size);

}

Test(Test&& other)

{

cout << "이동" << endl;

pData = other.pData;

other.pData = nullptr;

Test& operator=(const Test& other)

{

cout << "할당 연산자" << endl;

if (this == &other)

return *this;

delete[] pData;

pData = new char[size];

memcpy(pData, other.pData, size);

return *this;

}

Test& operator=(Test&& other)

{

cout << "이동 할당 연산자" << endl;

if (this == &other)

return *this;

delete[] pData;


pData = other.pData;

other.pData = nullptr;

return *this;

}

~Test()

{

delete[] pData;

}

};


const int Test::size = 100;


int _tmain(int argc, _TCHAR* argv[])

{

Test aaa;

Test bbb = aaa;

aaa = bbb;

aaa = std::move(bbb);

Test ccc = Test(std::move(aaa));

return 0;

}



'c++' 카테고리의 다른 글

std::chrono 시간 카운트 계산 초단위  (0) 2016.10.24
std::function 쓰는이유  (0) 2015.07.13
대각선 이동  (0) 2015.03.19
std::find_if, std::lamda  (0) 2015.02.05
std::function 배열, 벡터  (0) 2015.01.08
Posted by 에브리피플
,

대각선 이동

c++ 2015. 3. 19. 20:26

atan2()

현재 지점에서 목표지점에 x, y축 증가량대입

atan2( y축 증가량, x축 증가량 )

반환되는 값을 x, y 좌표에 이동량으로 더해줌 ( 스피드 )

'c++' 카테고리의 다른 글

std::function 쓰는이유  (0) 2015.07.13
C++11 move semantic 관련  (0) 2015.07.12
std::find_if, std::lamda  (0) 2015.02.05
std::function 배열, 벡터  (0) 2015.01.08
c++11 lamda 사용  (0) 2015.01.08
Posted by 에브리피플
,

std::find_if, std::lamda

c++ 2015. 2. 5. 12:14

class User
 {
 private:
  int idx;
  bool die;
 public:
  User(){idx = 0; die = false; }
  void SetIndex( const int i ) { idx = i; }
  int GetIndex(){ return idx; }
  void SetDie() { die = true; }
  bool IsDie() { return die; }
 };
 std::vector< User > Users;

    User tUser1; tUser1.SetIndex(1); Users.push_back(tUser1);
    User tUser2; tUser2.SetIndex(2); tUser2.SetDie();
    Users.push_back(tUser2);
    User tUser3; tUser3.SetIndex(3); Users.push_back(tUser3);

    std::vector< User >::iterator Iter;
    Iter = std::find_if( Users.begin(), Users.end(), [](User& tUser) -> bool { return
        true == tUser.IsDie(); } );

    log("found Die User Index : %d" , Iter->GetIndex());
 Iter->SetIndex(3);
 log("%d ", Iter->GetIndex() );

'c++' 카테고리의 다른 글

C++11 move semantic 관련  (0) 2015.07.12
대각선 이동  (0) 2015.03.19
std::function 배열, 벡터  (0) 2015.01.08
c++11 lamda 사용  (0) 2015.01.08
생성자 만들땐 explict  (0) 2015.01.08
Posted by 에브리피플
,

std::function 배열, 벡터

c++ 2015. 1. 8. 15:08
std::function<int(void)> *pfuncPtr = new std::function<int(void)>[3];
pfuncPtr[0] = []() -> int { return 100; };
pfuncPtr[1] = []() -> int { return 200; };
pfuncPtr[2] = []() -> int { return 300; };
 
for ( int i = 0; i < 3; i++ )
{
    printf( "pfuncPtr[%d]() => %d\n", i, pfuncPtr[i]() );
}
 
delete []pfuncPtr;

 

 

std::function<void ()> hello_world = []{ log("Hello "); };
 std::function<void ()> hello = [] { log("world"); };
 std::function<void ()> world = [] { log("a"); };
 std::vector<std::function<void()> > vFunc;
 vFunc.push_back(hello_world);
 vFunc.push_back(hello);
 vFunc.push_back(world);
 for ( auto f : vFunc )
  f();

'c++' 카테고리의 다른 글

대각선 이동  (0) 2015.03.19
std::find_if, std::lamda  (0) 2015.02.05
c++11 lamda 사용  (0) 2015.01.08
생성자 만들땐 explict  (0) 2015.01.08
stl 사용시  (0) 2015.01.07
Posted by 에브리피플
,

c++11 lamda 사용

c++ 2015. 1. 8. 14:29

[]: 어떤 변수도 캡처하지 않습니다.
[=]: 람다 외부 변수 모두 복사 캡처, 클래스 멤버는 참조 캡처
[&]: 람도 외부 변수 모두 참조 캡처, 클래스 멤버는 참조 캡처
[this]: 클래스 멤버만 참조 캡처
[var]: 람다 외부의 var 변수를 복사 캡처
[&var]: 람다 외부의 var 변수를 참조 캡처
[=, &var]: 람다 외부의 var 변수는 참조 캡처, 그외 변수는 복사 캡처
[&, var]: 람다 외부의 var 변수는 복사 캡처, 그외 변수는 참조 캡처

int num = 0;
 std::vector<int> m_v;
 m_v.resize(10);
 std::for_each( m_v.begin(), m_v.end(), [&](int) {
  num += 5;
 });
 printf("%d\n ", num );

 [=]() mutable { num += 5; };
 printf("%d\n ", num ); //50
 [&]() mutable { num += 5; };
 printf("%d\n ", num ); // 50
 num = [](int num) mutable { num += 5; return num; }(num);
 printf("%d\n ", num ); // 55
 [](int& num) mutable { num += 5; }(num);
 printf("%d\n ", num ); // 60

'c++' 카테고리의 다른 글

std::find_if, std::lamda  (0) 2015.02.05
std::function 배열, 벡터  (0) 2015.01.08
생성자 만들땐 explict  (0) 2015.01.08
stl 사용시  (0) 2015.01.07
std::all_of, std::any_of, std::none_of  (0) 2015.01.07
Posted by 에브리피플
,

생성자 만들땐 explict

c++ 2015. 1. 8. 14:25

생성자 만들땐 exlict를 사용하는 것이 좋다.

묵시적으로 생성자 호출을 막고 C스타일의 초기화 문법 자체가 혼란스러우니

'c++' 카테고리의 다른 글

std::function 배열, 벡터  (0) 2015.01.08
c++11 lamda 사용  (0) 2015.01.08
stl 사용시  (0) 2015.01.07
std::all_of, std::any_of, std::none_of  (0) 2015.01.07
std::move, RValue, LValue, 이동 생성자  (0) 2015.01.07
Posted by 에브리피플
,

stl 사용시

c++ 2015. 1. 7. 16:02

push_back() 대신 emplace_back() 사용

복사 비용 줄임.

'c++' 카테고리의 다른 글

c++11 lamda 사용  (0) 2015.01.08
생성자 만들땐 explict  (0) 2015.01.08
std::all_of, std::any_of, std::none_of  (0) 2015.01.07
std::move, RValue, LValue, 이동 생성자  (0) 2015.01.07
함수포인터, std::function, std::bind  (0) 2015.01.07
Posted by 에브리피플
,

#include <algorithm>

std::vector<int> vInt;

for ( int i = 0; i < 10; i++ )

{

vInt.push_back(10);

}

auto check = std::all_of( vInt.begin(), vInt.end(), [](int i) 

return i >= 5 ? true : false; 

});

std::all_of : 범위에 있는 모든 요소가 조건에 만족한다면

std::any_of : 범위에 있는 요소 중에 한 개라도 조건을 만족한다면

std::none_of : 범위에 있는 요소 중에 한 개라도 조건을 만족하지 않는다면

'c++' 카테고리의 다른 글

생성자 만들땐 explict  (0) 2015.01.08
stl 사용시  (0) 2015.01.07
std::move, RValue, LValue, 이동 생성자  (0) 2015.01.07
함수포인터, std::function, std::bind  (0) 2015.01.07
c++11 <random >  (0) 2015.01.06
Posted by 에브리피플
,