Programmer Application Note

레이블이 c인 게시물을 표시합니다. 모든 게시물 표시
레이블이 c인 게시물을 표시합니다. 모든 게시물 표시

2015년 11월 17일 화요일

c++ string replace all

오전 12:26 Posted by PAN.SPOT , No comments
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int main()
{
   cout << "Hello World" << endl;
   string str = "ab0cd0e0fg";
   replace_if(str.begin(),str.end(),bind2nd(equal_to<char>(),char('0')),char('-'));
   cout << str << endl;
   return 0;
}

output:
        ab-cd-e-fg

2015년 8월 9일 일요일

복사생성자와대입연산자 코딩 시 주의 할 점

오후 6:05 Posted by PAN.SPOT , No comments
디폴트 대입연산자를 사용하지 않고 직접 코딩 한다면 우측 연관 연산을 고려하여 자기자신의 참조자를 반환하도록 하자. 또한 자기 대입에 대한 처리도 하자.
우측연관 연산의 예
x = y = z = 15;
자기 대입에 대한 예
x = x; , a[i] = a[j];
class Widget{
   Widget(const Widget& rhs); 
   Widget& operator=(const Widget& rhs);
}
Widget& Widget::operator=(const Widget& rhs)
{
   Widget temp (rhs); // 자기 대입에 대한 처리
   swap(temp); // 자기 대입에 대한 처리 
   return *this; //우측연관 연산 처리 
}
복사생성자와 대입연산자를 코딩할때는 모든 맴버를 복사하도록 하자
class Customer{
   Customer(const Customer& rhs); 
   Customer& operator=(const Customer& rhs);
private:
   sting str;
   Data data;
}
str,data도 반드시 복사 하도록 하자.
class ACustomer : Customer{
     Customer(const Customer& rhs) : Customer(rhs) 
     {
     .....   
     }
     Customer& operator=(const Customer& rhs)
     {
        Customer::operator=(rhs);
     }
}
위와 같이 베이스 클래스의 생성자와 대입연산자를 호출하여, 베이스 클래스의 복사도 해주자.

2015년 7월 27일 월요일

함수포인터

오후 5:04 Posted by PAN.SPOT , No comments
  • 함수 정의
void cStyleFunction(int param){...}

class Aclass {
 void classFunction(int param){...}
 static void classStaticFunction(int param){...}
};

namespace NameSpace{
 void namespaceFunction(int param){...}
}
  • 함수포인터 선언
void (*FunctionPointer) (int param);
typedef void (*TypedefFunctionPointer) (int param);
void (Aclass::*ClassFunctionPointer) (int param);
  • 함수포인터 사용
void (*FunctionPointer) (int param);
void main(){
 void (*FunctionPointer) (int param);
 FunctionPointer = cStyleFunction; //ok
 FunctionPointer = Aclass::classStaticFunction; //ok
 FunctionPointer = NameSpace::namespaceFunction; //ok 
 FunctionPointer = Aclass::classFunction; //error
 FunctionPointer(param);
}
typedef void (*TypedefFunctionPointer) (int param);
typedef void (*TypedefFunctionPointer) (int param);
void main(){
 TypedefFunctionPointer tfp;
 tfp = cStyleFunction; //ok
 tfp = Aclass::classStaticFunction; //ok
 tfp = NameSpace::namespaceFunction; //ok
 tfp = Aclass::classFunction; //error
 tfp(param);
}
typedef void (Aclass::*ClassFunctionPointer) (int param);
void main(){
 Aclass *a = new Aclass();
 void (Aclass::*ClassFunctionPointer) (int param);
 ClassFunctionPointer = &Aclass::classFunction; //ok
 (a->*ClassFunctionPointer)(param);
}

2015년 7월 23일 목요일

디폴트 생성자 , 소멸자, 복사 생성자 , 대입연산자

오후 5:22 Posted by PAN.SPOT , No comments

컴파일러가 자동으로 생성하는 것들

Empty() {...}; //기본 생성자
~Empty() {...};//기본 소멸자
Empty(const Empty& rhs) {...}; //복사 생성자
Empty& operator=(const Empty& rhs) {...}; //복사 대입 연산자
Empty e1; //기본 생성자,기본 소멸자 Empty e2(e1); // 복사 생성자 e2 = e1; //복사 대입 연산자

만약 하나밖에 없는 자원이라 복사생성자나 대입 연산자가 필요 없다면

pirvate:
    HomeForSale(const HomeForSale&);
    HomeForSale& operator=(const HomeForSale&);

2015년 5월 5일 화요일

predicate / function object / unary / binary

오후 5:38 Posted by PAN.SPOT , No comments

predicate / function object / unary / binary

용어정리

  • function object(함수객체 Functor) : 클래스에서 연산자 ()를 오버로딩 하여 해당 클래스의 인스턴스를 함수 형태로 호출할 수 있게 한 클래스 객체를 말한다.
  • predicate (술어) : 적어도 하나 이상의 개체를 전달받아서 bool 값을 반환 하는 함수 객체 이다.
  • unary : 단항
  • binary : 이항

STL 템플릿 예제로 확인 해보자.

예제

for_each

std::for_each
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function fn);

parameters

매개변수를 확인 해보면 아래와 같다. 즉 iterator와 단항 함수 객체를 매개변수로 받는다.
*  @param  __first  An input iterator.
*  @param  __last   An input iterator.
*  @param  __f      A unary function object.

example

// for_each example
#include <iostream>     // std::cout
#include <algorithm>    // std::for_each
#include <vector>       // std::vector

void myfunction (int i) {  // function:
  std::cout << ' ' << i;
}

struct myclass {           // function object type: 
  void operator() (int i) {std::cout << ' ' << i;}
} myobject;

int main () {
  std::vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);

  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myfunction);
  std::cout << '\n';

  // or:
  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myobject);
  std::cout << '\n';

  return 0;
}
Output:
myvector contains: 10 20 30
myvector contains: 10 20 30
  • int i 하나를 매개변수로 가지고 () 연산자를 오버로딩하여 for_each 3번째 인자인 Function에 전달할 단항 함수 객체를 생성
      struct myclass {           //unary function object type: 
      void operator() (int i) {std::cout << ' ' << i;}
      } myobject;
    
위의 예제에서 function object / unary / binary 3가지에 대해 알아보았으니 predicate에 대해 알아보자.

예제

find_if

std::find_if
template <class InputIterator, class UnaryPredicate>
   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

parameters

매개변수를 확인 해보면 , iterator 와 단항 술어 임을 확인 할 수 있다.
*  @param  __first  An input iterator.
*  @param  __last   An input iterator.
*  @param  __pred   A predicate.

example

// find_if example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector


struct myclass {           // unary predicate 
  bool operator() (int i) {return ((i%2)==1);}
} myobject;

int main () {
  std::vector<int> myvector;

  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

  std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), myobject);
  std::cout << "The first odd value is " << *it << '\n';

  return 0;
}
Output:
The first odd value is 25
  • int i 하나를 매개변수로 가지고 () 연산자를 오버로딩하며 bool을 반환하는 find_if 3번째 인자인 Function에 전달할 단항 함수 개체를 생성 얼핏 보면 위의 예제와 다를 것이 없지만 bool을 반환 하는 함수 객체 즉 predicate라는 것을 알 수 있다.
      struct myclass {           // unary predicate 
        bool operator() (int i) {return ((i%2)==1);}
      } myobject;

2015년 4월 9일 목요일

C++ 표준

오후 4:52 Posted by PAN.SPOT , No comments
연도
C++ 표준
비공식 이름
2011년
ISO/IEC 14882:2011
C++11 (c++0x)
2007년
ISO/IEC TR 19768:2007
2003년
ISO/IEC 14882:2003
C++03
1998년
ISO/IEC 14882:1998
C++98