⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nstring.h

📁 Think in C++ 第二版源码
💻 H
字号:
//: C21:NString.h

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// A "numbered string" that indicates which

// occurrence this is of a particular word

#ifndef NSTRING_H

#define NSTRING_H

#include <string>

#include <map>

#include <iostream>



class NString {

  std::string s;

  int occurrence;

  struct Counter {

    int i;

    Counter() : i(0) {}

    Counter& operator++(int) { 

      i++;

      return *this;

    } // Post-incr

    operator int() { return i; }

  };

  // Keep track of the number of occurrences:

  typedef std::map<std::string, Counter> csmap;

  static csmap occurMap;

public:

  NString() : occurrence(0) {}

  NString(const std::string& x) 

    : s(x), occurrence(occurMap[s]++) {}

  NString(const char* x) 

    : s(x), occurrence(occurMap[s]++) {}

  // The synthesized operator= and 

  // copy-constructor are OK here

  friend std::ostream& operator<<(

    std::ostream& os, const NString& ns) {

    return os << ns.s << " [" 

      << ns.occurrence << "]";

  }

  // Need this for sorting. Notice it only 

  // compares strings, not occurrences:

  friend bool 

  operator<(const NString& l, const NString& r) {

    return l.s < r.s;

  }

  // For sorting with greater<NString>:

  friend bool 

  operator>(const NString& l, const NString& r) {

    return l.s > r.s;

  }

  // To get at the string directly:

  operator const std::string&() const {return s;}

};



// Allocate static member object. Done here for

// brevity, but should actually be done in a 

// separate cpp file:

NString::csmap NString::occurMap;

#endif // NSTRING_H ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -