Humaneer
2014년 12월 29일 월요일
(링크)STL deque에 대한 분석 글, "An In-Depth Study of the STL Deque Container"
[원문]
2014년 12월 26일 금요일
simple csv parser
내가 살짝 수정함. 근데 먼가 버그스러운 것이 있음. -_-;
std::vector
csv_read_row(std::string &line, char delimiter) { std::istringstream ss(line); return csv_read_row(ss, delimiter); } std::vector
csv_read_row(std::istream &in, char delimiter) { std::stringstream ss; bool inquotes = false; std::vector
row;//relying on RVO while(in.good()) { char c = in.get(); if (!inquotes && c=='"') //beginquotechar { inquotes=true; } else if (inquotes && c=='"') //quotechar { if ( in.peek() == '"')//2 consecutive quotes resolve to 1 { ss << (char)in.get(); } else //endquotechar { inquotes=false; } } else if (!inquotes && c==delimiter) //end of field { row.push_back( ss.str() ); ss.str(""); } else if (!inquotes && (c=='\r' || c=='\n') ) { if(in.peek()=='\n') { in.get(); } row.push_back( ss.str() ); return row; } else if (c == static_cast
(0xff)) { // 이상하게 c에 0xff가 들어가있는 경우가 발생함. 제일 마지막에... row.push_back( ss.str() ); return row; } else { ss << c; } } }
2014년 12월 23일 화요일
operator= 는 자기대입 처리할 것, 객체는 빠짐없이 복사할 것.
effective c++, chapter 11
#include
#include
#include
#include
#include
#include
struct GenRand { template
void operator()(T& rhs) { rhs = static_cast
(rand() % 255); } }; struct TransRand { template
T operator()(const T& rhs) { return (rand() % 255) * rhs; } }; class Widget { protected: int _count; friend std::ostream& operator << (std::ostream& os, const Widget& rhs); virtual void swap(Widget& rhs) { std::cout << "Widget Swap...\n"; int tempCount = _count; int* tempData = _data; _count = rhs._count; _data = rhs._data; rhs._count = tempCount; rhs._data = tempData; } int* _data; public: ~Widget() { if(_count) delete [] _data; std::cout << "Widget Destroy...\n"; } Widget(const int& count = 0) : _count(count), _data(0) { std::cout << "Widget Create Default...\n"; if (_count) { _data = new int[_count]; std::for_each(_data, _data+_count, GenRand()); } } Widget(const Widget& rhs) : _count(rhs._count) { std::cout << "Widget Create Copy...\n"; _data = new int[_count]; std::copy(rhs._data, rhs._data+rhs._count, _data); } Widget& operator=(Widget rhs) { std::cout << "Widget Assign...\n"; swap(rhs); return *this; } }; std::ostream& operator << (std::ostream& os, const Widget& rhs) { std::copy(rhs._data, rhs._data+rhs._count, std::ostream_iterator
(os, " ")); os << "\n"; return os; } class WidgetDerived : public Widget { private: float* _fdata; public: friend std::ostream& operator << (std::ostream& os, const WidgetDerived& rhs); ~WidgetDerived() { std::cout << "WidgetDerived Destroy...\n"; if (_count) delete [] _fdata; } WidgetDerived(const int& count = 0) : Widget(count) { std::cout << "WidgetDerived Create Default...\n"; if (_count) { _fdata = new float[_count]; std::transform(_data, _data+_count, _fdata, TransRand()); } } WidgetDerived(const WidgetDerived& rhs) : Widget(rhs) { std::cout << "WidgetDerived Create Copy...\n"; _fdata = new float[_count]; std::copy(rhs._fdata, rhs._fdata+rhs._count, _fdata); } virtual void swap(WidgetDerived& rhs) { Widget::swap(rhs); std::cout << "WidgetDerived Swap...\n"; float* tempFData = _fdata; _fdata = rhs._fdata; rhs._fdata = tempFData; } WidgetDerived& operator=(WidgetDerived rhs) { std::cout << "Widget Assign...\n"; swap(rhs); return *this; } }; std::ostream& operator << (std::ostream& os, const WidgetDerived& rhs) { os << (Widget&)rhs; std::copy(rhs._fdata, rhs._fdata+rhs._count, std::ostream_iterator
(os, " ")); return os; } int main() { //WidgetDerived wd(5); //wd = wd; //Widget a(5); srand(time(NULL)); WidgetDerived wd(5); std::cout << wd << "\n\n"; WidgetDerived b = wd; b = b; std::cout << b << "\n"; return 0; }
최근 게시물
이전 게시물
홈
피드 구독하기:
글 (Atom)