Skip to content
Reini Urban edited this page Jan 4, 2021 · 1 revision

string - CTL - C Container Template library

Defined in header <ctl/string.h>, CTL prefix str. deriving from vector.

SYNOPSIS

#undef POD
#define T int
#include <ctl/string.h>

str_int a = str_int_init ();

str_digi_resize(&a, 1000, 0);
for (i=0; i<1000; i++)
  str_int_push_back(&a, i);
for (i=0; i<20; i++)
   str_digi_pop_back(&a);
str_int_erase(&a, 5);
str_int_insert(&a, 5, 2);

str_int_free(&a);

DESCRIPTION

The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a string may be passed to any function that expects a pointer to an element of an array.

The function names are composed of the prefix str_, the user-defined type T and the method name. E.g str_int with #define T int.

Reallocations are usually costly operations in terms of performance. The reserve function can be used to eliminate reallocations if the number of elements is known beforehand.

The complexity (efficiency) of common operations on a string is as follows:

  • Random access - constant 𝓞(1)
  • Insertion or removal of elements at the end - amortized constant 𝓞(1)
  • Insertion or removal of elements - linear in the distance to the end of the string 𝓞(n)

Member types

T value type

A being str_T container type

I being str_T_it iterator type

Member functions

init ()

constructs the string.

free (A* self)

destructs the string.

assign (A* self, size_t count, T value)

replaces the contents of the container.

assign_range (A* self, I* first, I* last)

replaces the contents of the container with the values from range.

copy (A* self)

returns a copy of the container.

Element access

at (A* self, size_t index)

access specified element with bounds checking

front (A* self)

access the first element

back (A* self)

access the last element

data (A* self)

access the underlying array

Iterators

begin (A* self)

returns an iterator to the beginning

end (A* self)

returns an iterator to the end

Capacity

empty (A* self)

checks whether the container is empty

size (A* self)

returns the number of elements

max_size ()

returns the maximum possible number of elements

reserve (A* self, const size_t capacity)

reserves storage

capacity (A* self)

returns the number of elements that can be held in currently allocated storage

shrink_to_fit (A* self)

reduces memory usage by freeing unused memory

Modifiers

clear (A* self)

clears the contents

insert (A* self, T key)

inserts the element (C++17)

emplace (A* self, ...)

constructs elements in-place

emplace_back (A* self, I* position, ...)

constructs elements in-place at position

erase (A* self, size_t index)

erases the element by index

erase_it (A* self, I* position)

erases the element at position

erase_range (A* self, I* first, I* last)

erases elements from to

swap (A* self, A* other)

swaps the contents

extract (A* self, T key)

extracts a node from the container. NYI

extract_it (A* self, I* position)

extracts nodes from the container. NYI

merge (A* self)

splices nodes from another container

Lookup

count (A* self)

returns the number of elements matching specific key

find (A* self, T key)

finds element with specific key

contains (A* self, T key)

checks if the container contains element with specific key. (C++20)

equal_range (A* self)

returns range of elements matching a specific key. (NYI)

lower_bound (A* self)

returns an iterator to the first element not less than the given key. (NYI)

upper_bound (A* self)

returns an iterator to the first element greater than the given key. (NYI)

Observers

value_comp (A* self)

Returns the function that compares keys in objects of type value_type T. (NYI)

Non-member functions

swap (A* self)

specializes the swap algorithm

remove_if (A* self, int T_match(T*))

Removes all elements satisfying specific criteria.

erase_if (A* self, int T_match(T*))

erases all elements satisfying specific criteria (C++20)

intersection (A* self, A* other)

union (A* self, A* other)

difference (A* self, A* other)

symmetric_difference (A* self, A* other)

Clone this wiki locally