C/C++字符串处理之String ADT

概要

字符串是什么?我们认为,与其说它是一个类,不如说它只是一个ADT(抽象数据类型)。

目前C++中的 字符串类

目前广泛采用的C++字符串类有二:std::string(basic_string,由STL提供)、CString(由MFC或者WTL提供 )。它们的实现非常类似,都是带引用计数的、基于线性数据结构的字符串。不过SGI STL的Rope打破了这个规矩。它采用了一 种基于树结构的组织方式来实现字符串。

如何理解字符串只是ADT?

我们知道,基于值的容器主要有:

动 态数组(std::vector)

双向链表(std::list)

单向链表(std::slist,非STL标准)

双向队列 (std::deque)

std::deque其实是分段连续的、介于数组和链表之间的数据结构。这里不进行详细介绍,关于 std::deque的介绍,请参见这里。

这些容器都可以成为实现字符串的基础容器。例如,我们的StringBuilder基于 std::vector实现;我们的TextPool基于std::deque实现。

也许你有疑问:是的,基于std::vector或者std::deque可以 理解,但是,这世上有基于链表的字符串吗?然而世界之大,确实无奇不有。据“不完全”统计,多数函数式语言( 如Erlang)确实采用单向链表实现字符串。

无论采用什么具体的实现,最后我们都会尽力去提供一个一致的字符串操作 界面。所以,从这个意义上说,字符串只是一个ADT(抽象数据类型),它可以有多种实现,使用者按照具体的需求选择一种最 合适自己用况的字符串类。

字符串操作界面

在StdExt库中,字符串这个ADT的规格定义如下:

常字符串

不可变的字符串类,应该至少包含以下方法:

template <class _E>
concept  ConstString
{
public:
  typename value_type;
  typename size_type,  difference_type;
  typename reference, const_reference;
  typename iterator,  const_iterator;

public:
  iterator begin() const;
  iterator end() const;

  reverse_iterator rbegin() const;
  reverse_iterator rend() const;

   const_reference at(size_type i) const;
  const_reference operator[](size_type i) const;

  size_type size() const;
  bool empty() const;

  basic_string<_E> stl_str()  const; // 转为STL string

public:
  // 取字符串的字串

  template <class  AllocT>
  BasicString<_E> substr(
    AllocT& alloc, size_type from = 0,  size_type count = (size_type)-1) const;

public:
  // 在字符串中查找子串(正向查找)。

  iterator find(const TempString<_E> pattern, iterator from = begin()) const;
   iterator find(const _E* pattern, size_type len, iterator from = begin()) const;

public:
  // 在字符串中查找子串(反向查找)。

  iterator rfind(const TempString<_E> pattern,  iterator from = begin()) const;
  iterator rfind(const _E* pattern, size_type len, iterator  from = begin()) const;

public:
  // 查找某个集合中的字符在字符串中第一次出现的位置(正向查 找)。

  iterator find_first_of(
    const TempString<_E> pattern, iterator  from = begin()) const;

  iterator find_first_of(
    const _E* pattern, size_type  len, iterator from = begin()) const;

public:
  // 查找某个集合中的字符在字符串中第一次出 现的位置(反向查找)。

  reverse_iterator find_last_of(
    const TempString<_E>  pattern, reverse_iterator from = rbegin()) const;

  reverse_iterator find_last_of(
     const _E* pattern, size_type len, reverse_iterator from = rbegin()) const;

public:
  // 在字符串中查找不在集合中出现的第一个字符的位置(正向查找)。

  iterator find_first_not_of (
    const TempString<_E> pattern, iterator from = begin()) const;

   iterator find_first_not_of(
    const _E* pattern, size_type len, iterator from = begin())  const;

public:
  // 在字符串中查找不在集合中出现的第一个字符的位置(反向查找)。

  reverse_iterator find_last_not_of(
    const TempString<_E> pattern, reverse_iterator  from = rbegin()) const;

  reverse_iterator find_last_not_of(
    const _E* pattern,  size_type len, reverse_iterator from = rbegin()) const;

public:
  // 比较两个字符串。

  int compare(const TempString<_E> b) const;
  int compare(const _E* b,  size_type blen) const;
  int compare(size_type from, size_type count, const TempString<_E>  b) const;
  int compare(size_type from, size_type count, const _E* b, size_type blen)  const;

public:
  // 比较两个字符串(传入单字符的比较函数)。

  template  <class _Compr>
  int compare_by(const TempString<_E> b, _Compr cmp) const;

  template <class _Compr>
  int compare_by(const _E* b, size_type blen, _Compr cmp)  const;

public:
  // 比较两个字符串(忽略大小写)。

  int icompare(const  TempString<_E> b) const;
  int icompare(const _E* b, size_type blen) const;

public:
  // 判断是否包含指定的串。

  bool contains(const TempString<_E> b)  const;
  bool contains(const _E* b, size_type blen) const;

public:
  template  <class LogT>
  void trace(LogT& log) const; // 在log中显示该字符串。

public:
  // 交换两个字符串

  void swap(ConstString& b);
}

template  <class _E> // 比较两个字符串
bool operator<cmp>(const ConstString<_E>& a,  const ConstString<_E>& b);
// 这里<cmp>是各种比较的算符,如==、!=、<、<=、>、 >=等等。

时间: 2024-09-17 03:27:24

C/C++字符串处理之String ADT的相关文章

在类中使用string类的问题。我定义的构造函数,想把字符串赋给string对象,却实现不了。

问题描述 在类中使用string类的问题.我定义的构造函数,想把字符串赋给string对象,却实现不了. //12.2.ano.h #ifndef STRING2_H_ #define STRING2_H_ #include #include using std::string; using std::cout; using std::cin; using std::endl; class String { private : string str; public : String(); Str

C/C++字符串处理之String

Table of Contents 概要 理解String(BasicString) 关于TempString基类 源码 参考阅 读 概要 我们知道,C++标准库(STL)提供了string(basic_string)类进行字符串操作.字符串很可能除了内存 分配器(allocator)1外使用最为频繁的STL类.但是C++社区对string的指责从来就没有停止过. 归纳起来,STL的 string类主要有以下这些争议点: 接口过多且规格和其他STL容器没有达成很好的一致性.例如,string::

字符串-关于C++ string 的问题

问题描述 关于C++ string 的问题 10C 实现C++ string中一个函数函数形式为replace(S1,i,jS2)S1是string对象,i表示从S1对象的i位置开始删除长度为j的字符串,并用S2替换这个字符串.能否给个思路? 解决方案 如果你要用数组的话,程序要满足一个条件,strlen(S2)=j-i;类似的程序,你可以参考写一下#include #include using namespace std; int main(){ string a;/////指定串,可根据要求

我为什么不能把从数据库读出来的字符串赋值给String的变量String str=rs.getString(&amp;amp;quot;name&amp;amp;quot;);

问题描述 我为什么不能把从数据库读出来的字符串赋值给String的变量Stringstr=rs.getString("name"):总是出错.但是能直接输出System.out.println(rs.getString("name"))急啊我在线等哪位帮我解决一下33XXXXXX! 解决方案 解决方案二:错误提示Exceptioninthread"main"java.sql.SQLException:afterendofresultset....

javascript正则表达式和字符串RegExp and String(一)_正则表达式

前言 正则表达式是javascript非常重要和常用的功能,在jquery等大型框架中用的非常频繁,最近抽时间学习了解了相关知识,记录下来与需要的朋友分享. 思维导图 RegExp(正则表达式)的创建方式 可以通过两种方式创建一个RegExp,具体如下: 通过/-./的方式来创建正则表达式(注意: /--/两边是没有单引号或双引号的) 通过RegExp构造方法来创建一正则表达式 为了更好的描述模式,正则表达式提供了3个标识,分别是: g/i/m g: 全局匹配:在整个字符串中匹配,而不是在第一次

C# 枚举类型转换字符串 Enum to string

c# 枚举类型转换字符串 enum to string 枚举类型都是值类型. system.enum是一个抽象类(abstract class),所有枚举类型都直接继承自它,当然也同时继承了它的所有成员. 所有的值类型都是system.valuetype的后代,枚举类型也不例外,枚举类型直接继承自system.enum,而system.enum却又直接继承自system.valuetype的,所以,枚举类型也是 system.valuetype的后代. 值类型都是system.valuetype

javascript正则表达式和字符串RegExp and String(二)_正则表达式

在上篇文章给大家介绍了javascript正则表达式和字符串RegExp and String(一),本文给大家继续分享相关内容. 前言 我认为,在学习一门编程语言的过程中,字符串.数组.容器是非常重要的部分,为了提高编程效率,我通常会对字符串.数组以及容器作深入的学习,详细了解相关特点及对应的API.这篇文章是针对javascript字符串学习写下的笔记,在此与需要的朋友分享. 思维导图 字符串的创建方式 字符串是javascript中的基本类型之一,它对应的类型是String,可以通过两种方

javascript正则表达式和字符串RegExp and String(二)

在上篇文章给大家介绍了javascript正则表达式和字符串RegExp and String(一),本文给大家继续分享相关内容. 前言 我认为,在学习一门编程语言的过程中,字符串.数组.容器是非常重要的部分,为了提高编程效率,我通常会对字符串.数组以及容器作深入的学习,详细了解相关特点及对应的API.这篇文章是针对javascript字符串学习写下的笔记,在此与需要的朋友分享. 思维导图 字符串的创建方式 字符串是javascript中的基本类型之一,它对应的类型是String,可以通过两种方

javascript正则表达式和字符串RegExp and String(一)

前言 正则表达式是javascript非常重要和常用的功能,在jquery等大型框架中用的非常频繁,最近抽时间学习了解了相关知识,记录下来与需要的朋友分享. 思维导图 RegExp(正则表达式)的创建方式 可以通过两种方式创建一个RegExp,具体如下: 通过/-./的方式来创建正则表达式(注意: /--/两边是没有单引号或双引号的) 通过RegExp构造方法来创建一正则表达式 为了更好的描述模式,正则表达式提供了3个标识,分别是: g/i/m g: 全局匹配:在整个字符串中匹配,而不是在第一次