C + + string ADT

Source: Internet
Author: User
Tags abstract bool comparison count data structures

Profile

What is a string? We think that it is not so much a class as it is just a ADT (abstract data type).

Current string classes in C + +

Currently widely used C + + String class has two: Std::string (basic_string, provided by the STL), CString (provided by MFC or WTL). Their implementations are very similar, with reference-counting strings that are based on linear data structures. However, the SGI STL rope broke the rule. It uses a tree structure based organization to implement the string.

How do you understand strings just ADT?

We know that the value-based containers are mainly:

Dynamic Array (std::vector)

Two-way linked list (std::list)

One-way linked list (std::slist, non-STL standard)

bidirectional queues (std::d eque)

std::d eque is actually a segmented, contiguous data structure between an array and a linked list. This is not a detailed description of std::d eque, see here.

These containers can be the base container for implementing strings. For example, our StringBuilder is based on std::vector implementations; Our Textpool is based on std::d eque implementation.

Perhaps you have a question: Yes, based on std::vector or std::d eque is understandable, but is there a string based on a list in this world? But the world is big, it's really strange. According to "incomplete" statistics, most functional languages (such as Erlang) do use one-way linked lists to implement strings.

No matter what specific implementation is used, we will try to provide a consistent string manipulation interface at the end. So, in this sense, the string is just a ADT (abstract data type), it can have a variety of implementations, the user in accordance with the specific requirements of the choice of the most suitable for their own use of the string class.

String Manipulation interface

In the Stdext Library, the ADT specification for the string is defined as follows:

Constant string

Immutable string classes should contain at least the following methods:

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; Convert to STL string

Public
string of strings to take

Template <class alloct>
Basicstring<_e> substr (
alloct& alloc, Size_type from = 0, size_type count = (size_type)-1) const;

Public
Finds a substring in a string (forward lookup).

Iterator Find (const tempstring<_e> pattern, iterator from = BEGIN ()) const;
Iterator Find (const _e* pattern, Size_type len, iterator from = BEGIN ()) const;

Public
Finds a substring in a string (reverse lookup).

Iterator RFind (const tempstring<_e> pattern, iterator from = BEGIN ()) const;
Iterator RFind (const _e* pattern, Size_type len, iterator from = BEGIN ()) const;

Public
Finds the first occurrence of a character in a collection in a string (forward lookup).

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
Finds the first occurrence of a character in a collection in a string (reverse lookup).

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
Finds the position in the string that is not the first character that appears in the collection (forward lookup).

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
Finds the position in the string that is not the first character that appears in the collection (reverse lookup).

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
Compares two strings.

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
Compares two strings (a comparison function that passes in a single character).

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
Compares two strings (ignores case).

int Icompare (const tempstring<_e> b) const;
int Icompare (const _e* B, size_type blen) const;

Public
Determines whether the specified string is included.

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; Displays the string in log.

Public
Swap two strings

void Swap (conststring& b);
}

Template <class _e>//Compare two strings
BOOL Operator<cmp> (const conststring<_e>& A, const conststring<_e>& B);
Here <cmp> is a variety of comparison operators, such as = =,!=, <, <=, >, >= and so on.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.