Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data Structures In C++ #TechLunch

Data Structures In C++ #TechLunch

2012/01/04(水) @ Livesense TechLunch
発表者:桂 大介

E60aa4f80303f3f386898546ddb3686a?s=128

Livesense Inc.
PRO

April 23, 2014
Tweet

Transcript

  1. Data Structures In C++ Data Structures In C++ katsura@livesense.co.jp

  2. C++

  3. Bjarne Stroustrup Bjarne Stroustrup

  4. Philosophy Statically typed Free-form Multi-paradigm Compiled General-purpose

  5. Domains Systems software Application software Device drivers Embedded software High-performance

    server and client applications Entertainment software such as video games
  6. Features Classes Virtual Functions Operator overloading Multiple Inheritance Templates Exception

    handling
  7. C++11

  8. Core language usability enhancements Initializer lists Uniform initialization Type inference(

    型推論 ) Range-based for-loop Lambda functions and expressions Alternative function syntax Object construction improvement Explicit overrides and final Identifiers with special meaning Null pointer constant Strongly typed enumerations Right angle bracket Explicit conversion operators Alias templates Unrestricted unions
  9. Core language functionality improvements Variadic templates New string literals User-defined

    literals Multitasking memory model Thread-local storage Explicitly defaulted and deleted special member functions Type long long int Static assertions Allow sizeof to work on members of classes without an explicit object Allow garbage collected implementations
  10. C++ standard library changes Upgrades to standard library components Threading

    facilities Tuple types Hash tables Regular expressions General-purpose smart pointers Extensible random number facility Wrapper reference Polymorphic wrappers for function objects Type traits for metaprogramming Uniform method for computing the return type of function objects
  11. #include <iostream> void main() { std::cout << "Hello, world!\n"; }

  12. Data Structures

  13. Queue

  14. #include <iostream> #include <queue> using namespace std; int main() {

    queue<int> q; q.push(1); q.push(2); q.push(3); q.push(4); int size = q.size(); for (int i = 0; i < size; i++) { cout << q.front() << endl; q.pop(); } return 0; }
  15. Linked list

  16. template<class T> class Node { public: Node(T v) : value(v),

    next() {} void dump() { cout << value << endl; if (next) { next->dump(); } } Node &push(Node *n) { Node *last = this; while (last->next) last = last->next; last->next = n; return *this; } Node &unshift(Node *n) { n->next = this; return *n; } private: T value; Node *next; };
  17. int main() { typedef Node<int> IntNode; Node<int> head(1); IntNode third

    = 3; head.push(new Node<int>(2)) .push(&third) .push(new Node<int>(4)) .unshift(new IntNode(0)) .unshift(new Node<int>(-1)) .dump(); return 0; }
  18. Binary search tree

  19. class Node { private: int value; Node *left; Node *right;

    public: Node(int v) { value = v; left = right = NULL; } void add(int v) { Node **target = value >= v ? &left : &right; if (*target) { (*target)->add(v); } else { *target = new Node(v); } } void dump() { if (left) left->dump(); cout << value << endl; if (right) right->dump(); } };
  20. class Tree { private: Node *root; public: Tree(int v) {

    root = new Node(v); } Tree add(int v) { root->add(v); return *this; } void dump() { root->dump(); } };
  21. int main() { Tree tree(5); tree.add(6) .add(3) .add(10) .add(1) .add(8)

    .dump() ; return 0; }
  22. 次回テーマ

  23. Memcached or Mongo あたりで コードリーディング