Skip to content

C++ string class that mimics functionality of std::string, including iterators.

Notifications You must be signed in to change notification settings

hoskylucky/String

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 

Repository files navigation

String Class

A simple customisable implementation of a String class that mimics functionality of std::string, including iterators.

Using Statements

using size_type = size_t; // size_type for positions of char in cstring
using const_pointer = const_iterator;
using pointer = iterator;
using const_reverse_pointer = const_reverse_iterator;
using reverse_pointer = reverse_iterator;

Constructor

String(); // default constructor  
String(const String &); // copy constructor  
String(const String& other, size_type pos, size_t len = npos); // substring constructor  
String(String &&); // move constructor  
String(const char *); // from c-string  
String(const char* s, size_t n); // from buffer  
String(size_t n, char c);// fill constructor  
String(const const_iterator first, const const_iterator second); // range constructor 

Equality operator

String & operator= (const String &); // copy assignment   
String & operator = (String &&); // move assignment

Destructor

~String();

Iterators

Returns iterator/const_iterator to the first character

iterator begin(); 
const_iterator begin() const;   

Returns iterator/const_iterator pointing to past-the-end character of the string

iterator end();  
const_iterator end() const; 

Returns const_iterator pointing to the first character / past-the-end character of the string

const_iterator cbegin() const;  
const_iterator cend() const; 

Returns reverse_iterator/const_reverse_iterator to the last character

reverse_iterator rbegin();  
const_reverse_iterator rbegin() const; 

Returns reverse_iterator/const_reverse_iterator pointing to reverse past-the-end character of the string

reverse_iterator rend(); 
const_reverse_iterator rend() const; 

Returns const_reverse_iterator pointing to the last character / reverse past-the-end character of the string

const_reverse_iterator crbegin() const; 
const_reverse_iterator crend() const; 

Element access

Get character of string

const char & operator [] (size_type) const;
char & operator [] (size_type);

Get character in string

const char & at(size_type) const;
char & at(size_type);

Access first character

const char & front() const;
char & front();

Access last character

const char & back() const;
char & back();

Modifiers

Append to String

String & operator += (const String &); // string (1)
String & operator += (const char *); // c - string (2)
String & operator += (char); // char (3)

Append to String

String& append(const String& str); // string (1)
String& append(const String& str, size_type subpos, size_t sublen = npos); // substring (2)
String& append(const char* s); // c - string (3)
String& append(const char* s, size_t n); // buffer(4)
String& append(size_type n, char c); // fill(5)
String& append(const const_iterator first, const const_iterator second); // range(6)
Append character to string

String & push_back(char);

Insert into string

String& insert(size_type pos, const String& other); 	// string(1)
String& insert(size_type pos, const String& other, size_type subpos, size_t sublen = npos); 	// substring(2)
String& insert(size_type pos, const char* other); // c - string(3)
String& insert(size_type pos, const char* s, size_t n); 	// buffer(4)
String& insert(size_type pos, size_t n, char c); 	// fill(5)
void insert(iterator p, size_t n, char c); 	// fill(6)
iterator insert(iterator p, char c); 	// single character(7)
void insert(iterator p, const const_iterator first, const const_iterator last); 	// range(8)  

Erase characters from string

String& erase(size_type pos = 0, size_t len = npos); 	// sequence(1)
iterator erase(const_iterator p); // character(2)
iterator erase(const_iterator first, const_iterator last); // range(3)  

Replace portion of string

String& replace(size_type pos, size_t len, const String& other); // string(1)
String& replace(const_iterator i1, const_iterator i2, const String& other); // string(2)
String& replace(size_type pos, size_t len, const String& other, size_type subpos, size_t sublen = npos); // substring(3)
String& replace(size_type pos, size_t len, const char* s); // c - string(4)
String& replace(const_iterator i1, const_iterator i2, const char* other);  // c - string(5)
String& replace(size_type pos, size_t len, const char* other, size_t n); // buffer(6)
String& replace(const_iterator i1, const_iterator i2, const char* other, size_t n); // buffer(7)	
String& replace(size_type pos, size_t len, size_t n, char c); // fill(8)
String& replace(const_iterator i1, const_iterator i2, size_type n, char c); // fill(9)
String& replace(const_iterator i1, const_iterator i2, const_iterator first, const_iterator second); // range(10)  

Swap string values

void swap(String &);

Delete last character

String & pop_back();  

String Operation

Get C string equivalent

const char * c_str() const;

Copy sequence of characters from string

size_t copy(char* s, size_t len, size_type pos = 0) const;

Find content in string

size_type find(const String& other, size_type pos = 0) const; //string(1)
size_type find(const char* s, size_type pos = 0) const; // c - string(2)
size_type find(const char* s, size_type pos, size_type n) const; // buffer(3)
size_type find(char c, size_type pos = 0) const; // character(4)

Find last occurrence of content in string

size_type rfind(const String& other, size_type pos = npos) const; // string(1)
size_type rfind(const char* s, size_type pos = npos) const; // c - string(2)
size_type rfind(const char* s, size_type pos, size_t n) const; // buffer(3)
size_type rfind(char c, size_type pos = npos) const; // character(4)

Find character in string

size_type find_first_of(const String& other, size_type pos = 0) const; // string(1)
size_type find_first_of(const char* other, size_type pos = 0) const; 	// c - string(2)
size_type find_first_of(const char* other, size_type pos, size_t n) const; 	// buffer(3)
size_type find_first_of(char c, size_type pos = 0) const; // character(4)

Find character in string from the end

size_type find_last_of(const String& other, size_type pos = String::npos) const; // string(1)
size_type find_last_of(const char* other, size_type pos = String::npos) const; // c - string(2)
size_type find_last_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_last_of(char c, size_type pos = String::npos) const; 	// character(4)

Find absence of character in string

size_type find_first_not_of(const String& other, size_type pos = 0) const; // string(1)
size_type find_first_not_of(const char* other, size_type pos = 0) const; 	// c - string(2)
size_type find_first_not_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_first_not_of(char c, size_type pos = 0) const; // character(4)

Find non-matching character in string from the end

size_type find_last_not_of(const String& other, size_type pos = String::npos) const; // string(1)
size_type find_last_not_of(const char* other, size_type pos = String::npos) const; // c - string(2)
size_type find_last_not_of(const char* other, size_type pos, size_t n) const; // buffer(3)
size_type find_last_not_of(char c, size_type pos = String::npos) const; // character(4)

Generate substring

String substr(size_type pos = 0, size_t len = npos) const;

Capacity

Returns length of string

size_t length() const;
size_t size() const;

Returns maximum size of string

size_t max_size() const;

Resize string

void resize(size_t n);
void resize(size_type n, char c);

Return size of allocated storage

size_t capacity() const;

Request a change in capacity

void reserve(size_t n = 0);

Clear string

void clear();

Return true is string is empty

bool empty() const;

Shrink to fit

void shrink_to_fit();

Member constants

static const size_t npos = -1; // Maximum value for size_t

Non-member functions overloads

Comparisions

 // Size is compared and if equal then comapred lexicographically
friend bool operator == (const String &, const String &);   
friend bool operator != (const String &, const String &);   
friend bool operator < (const String &, const String &);  
friend bool operator > (const String &, const String &);  
friend bool operator <= (const String &, const String &);  
friend bool operator >= (const String &, const String &);

Exchanges the values of two strings

void swap(String& x, String& y);

Returns concatenated Strings

String operator+ (const String& lhs, const String& rhs);
String operator+ (const String& lhs, const char*   rhs);
String operator+ (const char*   lhs, const String& rhs);
String operator+ (const String& lhs, char rhs);
String operator+ (char lhs, const String& rhs);

Insert string into stream

std::ostream& operator<< (std::ostream& os, const _JD String& str);

Extract string from stream

std::istream& operator>> (std::istream& is, _JD String& str);

Get line from stream into string

std::istream& getline(std::istream& is, _JD String& str, char delim);
std::istream& getline(std::istream& is, _JD String& str);

About

C++ string class that mimics functionality of std::string, including iterators.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%