336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

보통 Try, Catch 문이라고 하면 이런식으로 쓰게 될 것입니다.

void function()
{
    Try
    {
    }
    Catch
    {
        Throw;
    }
}

하지만 throw 를 하게 되면 내부적으로 다소 많은 부하가 걸릴 수 있다고 합니다.
(이 부분에 대해서 개발 실장님께 들었는데 정확한 정보는 찾아보지 못했습니다. 찾게 되면 추가적으로 내용을 쓰도록 하겠습니다.)
그래서 대신에 이런 식으로 처리하면 어떨까 합니다.

void function()
{
    do // dummy do
    {
        // 여기가 try
        if(오류) break;
        if(오류) break;

        // 오류에 걸리지 않았다면 처리
        return;
    } while(false)
    // 여기가 catch
    ERROR("오류가 났습니다.");
}

특히 서버 같은 경우 최적화가 매우 중요하므로,
특별한 상황이 아니라면 throw 를 이용하지 않는 것이 좋지 않을까 생각합니다.

do 든 while 이든 자주 사용하는 것이지만, '이런 식으로 응용을 할 수 있구나' 라고
생각하게 되는 좋은 예제라고 생각합니다.


출처 : http://www.dingpong.net/tt/8?category=2

Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

* Formula

 

* Source

sqrt( pos( x1 - x2, 2 ) + pow( y1 - y2, 2 ) + pow( z1 - z2, 2 ) );

 

속도는 모르겠다 ㅋㅋ

'Programming > C / C++' 카테고리의 다른 글

64비트 int (INT64) printf 에서 출력하기  (0) 2015.02.07
try catch 문 대체  (0) 2015.02.07
stringstream  (0) 2015.02.07
bitset 을 이용한 퀘스트 완료목록 관리  (0) 2015.02.07
EXIT_SUCCESS / EXIT_FAILURE  (0) 2015.02.07
Posted by 역시인생한방
,

stringstream

Programming/C / C++ 2015. 2. 7. 19:37
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include <iostream>
using namespace std;
#include <sstream>

 

int main()
{
    stringstream ss("/exp 1000");

// ss << "/exp 1000";

char command[20] = {0};
int val = 0;

ss >> command;
ss >> val;

assert( !ss.fail() && !ss.bad() && ss.eof() );
if( ss.fail() || ss.bad() || !ss.eof() )
{
    // 예외 처리
}
 
return EXIT_SUCCESS;

}

'Programming > C / C++' 카테고리의 다른 글

try catch 문 대체  (0) 2015.02.07
공간 속의 두 점 사이의 거리  (0) 2015.02.07
bitset 을 이용한 퀘스트 완료목록 관리  (0) 2015.02.07
EXIT_SUCCESS / EXIT_FAILURE  (0) 2015.02.07
numeric_limits  (0) 2015.02.07
Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

#include <bitset>
#include <limits>
using namespace std;


const int QUEST_SECTION = numeric_limits<char>::digits;
const int QUEST_SECTION_MAX = numeric_limits<char>::max();
const int QUEST_COUNT_COLUMN = 3;
const int QUEST_COUNT_MAX = QUEST_SECTION * QUEST_COUNT_COLUMN;


//---------------------------------------------------------------------------
// 설  명 : std::bitset 을 이용한 퀘스트 완료목록 테스트
// 인  자 : 
// 리턴값 : 
// 작성자 : 최민혁 - 작성일 : 2010-02-11 18:10:34
//---------------------------------------------------------------------------
int main()
{
    char data[QUEST_COUNT_COLUMN] = { 2, 4, 8 };
    char result[QUEST_COUNT_COLUMN] = { 0 };

std::bitset<QUEST_COUNT_MAX> completeList;
for( char i = 0; i < QUEST_COUNT_COLUMN; ++i )
{
    completeList |= (data[i] << (QUEST_SECTION * i));
}

std::bitset<QUEST_COUNT_MAX> marker(QUEST_SECTION_MAX);

for( char i = 0; i < QUEST_COUNT_COLUMN; ++i )
{
    result[i] = (char)((completeList >> (QUEST_SECTION * i)) & marker).to_ulong();
}


/*int alskdjf = completeList.to_ulong();

std::bitset<QUEST_COUNT_MAX> xxx;
std::bitset<QUEST_COUNT_MAX> aaa(2);
std::bitset<QUEST_COUNT_MAX> bbb(4);
std::bitset<QUEST_COUNT_MAX> ccc(8);

cout << xxx << endl;

int i = 0;
xxx |= (aaa << (QUEST_SECTION * i) );
++i;
xxx |= (bbb << (QUEST_SECTION * i) );
++i;
xxx |= (ccc << (QUEST_SECTION * i) );

cout << xxx << endl << endl;

i = 0;
cout << aaa << endl;
aaa <<= (QUEST_SECTION * i);
cout << aaa << endl;

++i;
cout << bbb << endl;
bbb <<= (QUEST_SECTION * i);
cout << bbb << endl;

++i;
cout << ccc << endl;
ccc <<= (QUEST_SECTION * i);
cout << ccc << endl;

std::bitset<QUEST_COUNT_MAX> fff(QUEST_SECTION_MAX);

i = 0;
std::bitset<QUEST_COUNT_MAX> temp(xxx);
temp.flip();
temp <<= (QUEST_SECTION * (QUEST_COUNT_COLUMN - 1 - i));
temp >>= (QUEST_SECTION * (QUEST_COUNT_COLUMN - 1 - i));
fff <<= (QUEST_SECTION * i);
int x = (temp ^= fff).to_ulong();

// 00000000000000000000000000000000
temp = xxx;
temp >>= (QUEST_SECTION * 1);
int y = (temp &= fff).to_ulong();
// 00000000000000000000000000000000

++i;
temp = xxx;
temp.flip();
temp <<= (QUEST_SECTION * (QUEST_COUNT_COLUMN - 1 - i));
temp >>= (QUEST_SECTION * (QUEST_COUNT_COLUMN - 1 - i));
fff <<= (QUEST_SECTION * i);
y = (temp ^= fff).to_ulong();

++i;
temp = xxx;
temp.flip();
temp <<= (QUEST_SECTION * (QUEST_COUNT_COLUMN - 1 - i));
temp >>= (QUEST_SECTION * (QUEST_COUNT_COLUMN - 1 - i));
fff <<= (QUEST_SECTION * i);
int z = (temp ^= fff).to_ulong();*/

return EXIT_SUCCESS;

}

'Programming > C / C++' 카테고리의 다른 글

공간 속의 두 점 사이의 거리  (0) 2015.02.07
stringstream  (0) 2015.02.07
EXIT_SUCCESS / EXIT_FAILURE  (0) 2015.02.07
numeric_limits  (0) 2015.02.07
bitset  (0) 2015.02.07
Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

<stdlib.h>


#define EXIT_SUCCESS 0

#define EXIT_FAILURE 1

 

int main()

{

    return EXIT_SUCCESS;

}


return 0; 보다 좀더 간지남 ㅋ

'Programming > C / C++' 카테고리의 다른 글

stringstream  (0) 2015.02.07
bitset 을 이용한 퀘스트 완료목록 관리  (0) 2015.02.07
numeric_limits  (0) 2015.02.07
bitset  (0) 2015.02.07
const 를 이용한 포인터 상수화  (0) 2015.02.07
Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

This class is specialized for each of the fundamental types, with its members returning or set to the different values that define the properties that type has in the specific platform in which it compiles.

For all the other types (non-fundamental types) a specialization of this class should not exist.

The non-specialized class is defined as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
template <class T> class numeric_limits {
public:
  static const bool is_specialized = false;
  static T min() throw();
  static T max() throw();
  static const int  digits = 0;
  static const int  digits10 = 0;
  static const bool is_signed = false;
  static const bool is_integer = false;
  static const bool is_exact = false;
  static const int radix = 0;
  static T epsilon() throw();
  static T round_error() throw();

  static const int  min_exponent = 0;
  static const int  min_exponent10 = 0;
  static const int  max_exponent = 0;
  static const int  max_exponent10 = 0;

  static const bool has_infinity = false;
  static const bool has_quiet_NaN = false;
  static const bool has_signaling_NaN = false;
  static const float_denorm_style has_denorm = denorm absent;
  static const bool has_denorm_loss = false;
  static T infinity() throw();
  static T quiet_NaN() throw();
  static T signalign_NaN() throw();
  static T denorm_min() throw();

  static const bool is_iec559 = false;
  static const bool is_bounded = false;
  static const bool is_modulo = false;

  static const bool traps = false;
  static const bool tinyness_before = false;
  static const float_round_style round_style = round_toward_zero;
}



A specialization exists for each of the fundamental types: boolcharsigned charunsigned charwchar_tshortunsigned shortint,unsigned intlongunsigned longfloatdouble and long double. These specializations define the specific values for the differentstatic const members, and all have is_specialized defined as true.


출처 : http://www.cplusplus.com/reference/std/limits/numeric_limits/

'Programming > C / C++' 카테고리의 다른 글

bitset 을 이용한 퀘스트 완료목록 관리  (0) 2015.02.07
EXIT_SUCCESS / EXIT_FAILURE  (0) 2015.02.07
bitset  (0) 2015.02.07
const 를 이용한 포인터 상수화  (0) 2015.02.07
override, abstract, __interface, sealed  (0) 2015.02.07
Posted by 역시인생한방
,

bitset

Programming/C / C++ 2015. 2. 7. 19:29
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Proxy Pattern 으로 구현된 vector<bool>은 이런저런 문제가 있다는 것만 기억해 두시기 바랍니다

그러니 vector<bool>을 사용할 것이라면 bitset을 사용하세요


출처 : http://cafe.naver.com/cafec.cafe?iframe_url=/ArticleRead.nhn%3Fclubid=10026632%26page=1%26menuid=101%26boardtype=L%26articleid=56315

'Programming > C / C++' 카테고리의 다른 글

EXIT_SUCCESS / EXIT_FAILURE  (0) 2015.02.07
numeric_limits  (0) 2015.02.07
const 를 이용한 포인터 상수화  (0) 2015.02.07
override, abstract, __interface, sealed  (0) 2015.02.07
switch 분할 호출  (0) 2015.02.07
Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

1. 포인터가 가리키는 변수의 상수화

 

void main()

{

int a = 10;

const int* p = &a;

*p = 20;        // 에러

a = 20;         // 정상 출력

}

 

 

즉, p가 가리키는 대상이 상수화가 된다.

그렇기 때문에 *p = 20 했을 때, *p가 가리키는 대상이 상수화 되기 때문에 값을 변경하지 못한다.

그러나, 포인터가 가리키는 값만 변경하지 못할 뿐, 일반 변수로써의 a값은 변경 가능하다.

 

 

 

2. 포인터 상수화

 

void main()

{

int a = 10;

int b = 20;


int* const p = &a;

p = &b;        // 에러

*p = 30;       // 정상 출력

}

 

 

p 자체가 상수화 된다.

p는 원래 a를 가리켰기 때문에, 다른 위치(b)를 가리킬 수 없다.

즉, p가 가리키는 주소는 다른 주소로 변경될 수 없다.

 

 

 

3. 포인터가 가리키는 변수의 상수화 + 포인터 상수화

  

const int* const p = &a;

 

1번과 2번을 합쳐놓은 형태이다.

포인터 p가 가리키는 변수의 값도 변경하지 못하고, 포인터 p는 프로그램이 종료될 때까지

a만 가리켜야 한다.


출처 : http://blog.naver.com/ac7979/140084296409

'Programming > C / C++' 카테고리의 다른 글

numeric_limits  (0) 2015.02.07
bitset  (0) 2015.02.07
override, abstract, __interface, sealed  (0) 2015.02.07
switch 분할 호출  (0) 2015.02.07
가변 인수  (0) 2015.02.07
Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

override

꽤 갈망해왔던 키워드이다.

base class에 있는 virtual을 상속받아서 쓰는경우에 base class에 있는 함수를 바꾸고 drived class 의 함수를 바꾸지 않았을 때 오작동하는 잡기 쉽지 않은 버그가 컴파일할때 에러가 나주면 얼마나 감사할까... 할 때 쓰시면 된다.

 

struct AAA

{
    virtual void f();
};


struct BBB : public AAA

{
    virtual void f() override {} // OK
    virtual void f2() override {} // error C3668: 'BBB::f2' : method with override specifier 'override' did not override any base class methods
};

 

abstract

virtual foo() = 0;

대신에

virtual foo() abstract;

로 쓸 수 있다.

함수에서는 별 필요 없지만 클래스 에서는 강력하게 사용할 수 있다.

class X abstract {};

int main()

{
    X * MyX = new X; // C3622 cannot instantiate abstract class
}

이런식으로 class 통째로 abstract기능을 걸 수 있다.

인터페이스 class 를 만들 때 유용할 듯...이라고 생각할 수 있지만 인터페이스 클래스는 __interface 키워드가 있으므로 걍 추상 클래스에만 사용하쟈...

사실 인터페이스용으로 사용해도 내용으로는 구분이 안되는 내용이지만 알아서 구분해 쓰자

 

__is_abstract(type) 으로 컴파일 타임 체크 가능

 

__interface

__interface I1

{
    virtual void f();
};

위의 abstract 키워드로 선언한 class 와의 차이

멤버변수를 가질 수 없다.

static류를 사용할 수 없다.

생성자, 파괴자, 오퍼레이터 오버로딩 불가(엑-.ㅜ 인터페이스 클래스에서 이거 꽤 쓰는데..;;)

인터페이스는 상속 가능, base class는 상속 안됨

public pure virtual함수만 가질 수 있음

장점은 virtual table을 차지하지 않음

 

sealed

더이상 오버라이딩 못하게 할 때 사용한다.

함수와 클래스 둘 다 사용가능

__is_sealed (type) 으로 컴파일 타임 체크 가능

 

.끝.

native 기준이고 CLR 에서만 사용가능한건 적지 않았음

위에 몇 개 나왔지만 __is_abstract 같은 컴파일 타임에 사용가능한 TypeTraits 가 꽤 있다.

특정 클래스가 base 클래스를 상속 받았는지를 체크하는것도 있고(CLR에서만 되지만)

__has_copy(type) 나 __has_assign(type) 같은

native에서 쓸만한 것도 꽤 있다.

근데 사실 TypeTraits 는 필요한건 native 지원 안하면 loki를 써버리면 그만


출처 : http://cafe.naver.com/jzsdn/5941

'Programming > C / C++' 카테고리의 다른 글

bitset  (0) 2015.02.07
const 를 이용한 포인터 상수화  (0) 2015.02.07
switch 분할 호출  (0) 2015.02.07
가변 인수  (0) 2015.02.07
STL without warnings  (0) 2015.02.07
Posted by 역시인생한방
,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

소스를 분석하다가 switch 문을 여러개로 분할해서 호출하는 것을 보고 의구심이 들어 테스트를 해보았다 ㅋ


#include <iostream>
using namespace std;
#include <windows.h>

 

const DWORD TEST_COUNT = 1000000000;

 

void Test1( DWORD i );
void Test2( DWORD j );

 

int main( void )
{
    int start1 = GetTickCount();
    for( DWORD i = 0; i < TEST_COUNT; i++ )
        Test1( i );
    int end1 = GetTickCount();

 

    int start2 = GetTickCount();
    for( DWORD j = 0; j < TEST_COUNT; j++ )
        Test2( j );
    int end2 = GetTickCount();

 

    cout << end1 - start1 << endl;
    cout << end2 - start2 << endl;

 

    return 0;
}

 

void Test1( DWORD i )
{
    int command = i % 10;

 

    switch( command )
    {
    case 0:
        command = 0;
        break;
    case 1:
        command = 0;
        break;
    case 2:
        command = 0;
        break;
    case 3:
        command = 0;
        break;
    case 4:
        command = 0;
        break;
    case 5:
        command = 0;
        break;
    case 6:
        command = 0;
        break;
    case 7:
        command = 0;
        break;
    case 8:
        command = 0;
        break;
    case 9:
        command = 0;
        break;
    }

}

 

void Test2( DWORD j )
{
    int command = j % 10;

 

    switch( command )
    {
    case 0:
        command = 0;
        break;
    case 1:
        command = 0;
        break;
    }

   

    switch( command )
    {
    case 2:
        command = 0;
        break;
    case 3:
        command = 0;
        break;
    case 4:
        command = 0;
        break;
    }

 

    switch( command )
    {
    case 5:
        command = 0;
        break;
    }

 

    switch( command )
    {
    case 6:
        command = 0;
        break;
    case 7:
        command = 0;
        break;
    case 8:
        command = 0;
        break;
    case 9:
        command = 0;
        break;
    }
}

 

 

 

<출력결과>

42172

48578

몇초씩 차이난다 ㄷㄷ


결과는 예상했던대로 분할하는 것은 좋지 않음!

'Programming > C / C++' 카테고리의 다른 글

const 를 이용한 포인터 상수화  (0) 2015.02.07
override, abstract, __interface, sealed  (0) 2015.02.07
가변 인수  (0) 2015.02.07
STL without warnings  (0) 2015.02.07
구조체 초기화  (0) 2015.02.07
Posted by 역시인생한방
,