Not able to run the code mentioned in PS on c++ insights.
Get the following error :
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at [email protected] to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.
The smaller code snippets work.
Kindly mention if there is a limitation based on code length in c++ insights.
Thank you,
Kapil
PS : Code below compiles and executes well on Ubuntu 14.04 g++
`
#ifndef MY_STRING_H_
#define MY_STRING_H_
#include <cstring>
#include <iostream>
#include <memory>
#include <algorithm>
#include <stdexcept>
#include <limits>
namespace kapil {
class string final {
private:
size_t sz_;
std::unique_ptr<char[]> ptr_;
public:
string();
string(const string&);
string(string&&) noexcept;
explicit string(const char*);
explicit string(char);
~string() noexcept;
size_t size() const noexcept;
size_t length() const noexcept;
void resize(size_t, char ch = '\0');
void clear() noexcept;
bool empty() const noexcept;
char& at(size_t);
const char& at(size_t) const;
char& back();
const char& back() const;
char& front();
const char& front() const;
string& append(const string&);
string& append(const char*);
string& append(string&&);
string& append(char);
void push_back(char);
string& assign(const string&);
string& assign(const char*);
string& assign(string&&);
void swap(string&);
const char* c_str() const noexcept;
const char* data() const noexcept;
string& operator = (const string&);
string& operator = (string&&) noexcept;
string& operator = (const char*);
string& operator = (char);
string& operator += (const string&);
string& operator += (const char*);
string& operator += (char);
string& operator += (string&&);
char& operator[] (size_t);
const char& operator[] (size_t) const;
friend std::ostream& operator << (std::ostream&, const string&);
friend string operator + (const string&, const string&);
friend string operator + (const string&, const char*);
friend string operator + (const char*, const string&);
friend string operator + (string&&, string&&);
friend string operator + (string&&, const char*);
friend string operator + (const char*, string&&);
friend string operator + (const string&, string&&);
friend string operator + (string&&, const string&);
friend string operator + (const string&, char);
friend string operator + (char, const string&);
friend string operator + (string&&, char);
friend string operator + (char, string&&);
friend bool operator == (const string&, const string&) noexcept;
friend bool operator == (const string&, const char*) noexcept;
friend bool operator == (const char*, const string&) noexcept;
friend bool operator == (const string&, char) noexcept;
friend bool operator == (char, const string&) noexcept;
friend bool operator == (const string&, string&&) noexcept;
friend bool operator == (string&&, const string&) noexcept;
friend bool operator == (string&&, string&&) noexcept;
friend bool operator == (string&&, char) noexcept;
friend bool operator == (char, string&&) noexcept;
friend bool operator == (const char*, string&&) noexcept;
friend bool operator == (string&&, const char*) noexcept;
friend bool operator != (const string&, const string&) noexcept;
friend bool operator != (const string&, const char*) noexcept;
friend bool operator != (const char*, const string&) noexcept;
friend bool operator != (const string&, char) noexcept;
friend bool operator != (char, const string&) noexcept;
friend bool operator != (const string&, string&&) noexcept;
friend bool operator != (string&&, const string&) noexcept;
friend bool operator != (string&&, string&&) noexcept;
friend bool operator != (string&&, char) noexcept;
friend bool operator != (char, string&&) noexcept;
friend bool operator != (const char*, string&&) noexcept;
friend bool operator != (string&&, const char*) noexcept;
class iterator
{
private:
string* str_;
size_t index_;
public:
iterator(string* = nullptr, size_t = 0) noexcept;
iterator(const iterator&) noexcept;
iterator(iterator&&) noexcept;
~iterator() noexcept;
iterator& operator = (const iterator&) noexcept;
iterator& operator = (iterator&&) noexcept;
bool operator != (const iterator&) const noexcept;
bool operator == (const iterator&) const noexcept;
iterator& operator ++ ();
iterator& operator ++ (int);
iterator& operator -- ();
iterator& operator -- (int);
char& operator * () const;
};
iterator begin();
iterator end();
class const_iterator
{
private:
const string* str_;
size_t index_;
public:
const_iterator(const string* = nullptr, size_t = 0);
const_iterator(const const_iterator&);
const_iterator(const_iterator&&) noexcept;
~const_iterator();
const_iterator& operator = (const const_iterator&);
const_iterator& operator = (const_iterator&&) noexcept;
bool operator != (const const_iterator&) const noexcept;
bool operator == (const const_iterator&) const noexcept;
const_iterator& operator ++ ();
const_iterator& operator ++ (int);
const_iterator& operator -- ();
const_iterator& operator -- (int);
const char& operator * () const;
};
const_iterator cbegin();
const_iterator cend();
class reverse_iterator
{
private:
string* str_;
size_t index_;
public:
reverse_iterator(string* = nullptr, size_t = 0);
reverse_iterator(const reverse_iterator&);
reverse_iterator(reverse_iterator&&) noexcept;
~reverse_iterator();
reverse_iterator& operator = (const reverse_iterator&);
reverse_iterator& operator = (reverse_iterator&&) noexcept;
bool operator != (const reverse_iterator&) const noexcept;
bool operator == (const reverse_iterator&) const noexcept;
reverse_iterator& operator ++ ();
reverse_iterator& operator ++ (int);
reverse_iterator& operator -- ();
reverse_iterator& operator -- (int);
char& operator * () const;
};
reverse_iterator rbegin();
reverse_iterator rend();
class reverse_const_iterator
{
private:
const string* str_;
size_t index_;
public:
reverse_const_iterator(const string* = nullptr, size_t = 0);
reverse_const_iterator(const reverse_const_iterator&);
reverse_const_iterator(reverse_const_iterator&&) noexcept;
~reverse_const_iterator();
reverse_const_iterator& operator = (const reverse_const_iterator&);
reverse_const_iterator& operator = (reverse_const_iterator&&) noexcept;
bool operator != (const reverse_const_iterator&) const noexcept;
bool operator == (const reverse_const_iterator&) const noexcept;
reverse_const_iterator& operator ++ ();
reverse_const_iterator& operator ++ (int);
reverse_const_iterator& operator -- ();
reverse_const_iterator& operator -- (int);
const char& operator * () const;
};
reverse_const_iterator crbegin();
reverse_const_iterator crend();
};
} //kapil
namespace kapil {
/**************************************** member functions *********************/
string::string() {
ptr_ = std::make_unique<char[]>(1);
ptr_.get()[0] = '\0';
sz_ = 0;
}
string::string(const string& other) {
sz_ = other.sz_;
ptr_ = std::make_unique<char[]>(sz_ + 1);
std::strcpy(ptr_.get(), other.ptr_.get());
}
string::string(string&& rval) noexcept
: sz_{ rval.sz_ }, ptr_{ std::move(rval.ptr_) } {
}
string::string(const char* c_string) {
sz_ = std::strlen(c_string);
ptr_ = std::make_unique<char[]>(sz_ + 1);
std::strcpy(ptr_.get(), c_string);
}
string::string(char ch) {
sz_ = 1;
ptr_ = std::make_unique<char[]>(sz_ + 1);
ptr_.get()[0] = ch;
ptr_.get()[1] = '\0';
}
string::~string() noexcept {
ptr_.reset(nullptr);
sz_ = 0;
};
/**************************************** member functions *********************/
size_t string::size() const noexcept {
return sz_;
}
size_t string::length() const noexcept {
return sz_;
}
void string::resize(size_t n, char ch) {
if (n == sz_) {
return;
}
std::unique_ptr<char[]> temp = std::make_unique<char[]>(n + 1);
if (n < sz_) {
std::strncpy(temp.get(), ptr_.get(), n);
temp.get()[n] = '\0';
} else if (n > sz_) {
std::strncpy(temp.get(), ptr_.get(), sz_);
std::fill(temp.get() + sz_, temp.get() + n + 1, ch);
}
ptr_.reset(nullptr);
sz_ = n;
ptr_ = std::move(temp);
}
void string::clear() noexcept {
ptr_.reset(nullptr);
sz_ = 0;
}
bool string::empty() const noexcept {
return sz_ == 0;
}
char& string::at(size_t idx) {
if (idx < 0 || idx >= sz_) {
throw std::out_of_range{"out of range memory access"};
}
return (*this)[idx];
}
const char& string::at(size_t idx) const {
if (idx < 0 || idx >= sz_) {
throw std::out_of_range{"out of range memory access"};
}
return (*this)[idx];
}
char& string::back() {
return (*this)[sz_ - 1];
}
const char& string::back() const {
return (*this)[sz_ - 1];
}
char& string::front() {
return (*this)[0];
}
const char& string::front() const {
return (*this)[0];
}
string& string::append(const string& rhs) {
(*this) += rhs;
return *this;
}
string& string::append(const char* rhs) {
(*this) += rhs;
return *this;
}
string& string::append(string&& rhs) {
(*this) += rhs;
return *this;
}
string& string::append(char ch) {
(*this) += ch;
return *this;
}
void string::push_back(char ch) {
(*this) += ch;
return;
}
string& string::assign(const string& rhs) {
(*this) = rhs;
return *this;
}
string& string::assign(const char* rhs) {
(*this) = rhs;
return *this;
}
string& string::assign(string&& rhs) {
(*this) = rhs;
return *this;
}
void string::swap(string &str) {
string temp{str};
str = *this;
*this = temp;
}
const char* string::c_str() const noexcept {
return ptr_.get();
}
const char* string::data() const noexcept {
return c_str();
}
/**************************************** member operator overloads*********************/
string& string::operator = (const string& rhs) {
if (this != &rhs) {
ptr_.reset(nullptr);
sz_ = rhs.sz_;
ptr_ = std::make_unique<char[]>(sz_ + 1);
std::strcpy(ptr_.get(), rhs.ptr_.get());
}
return *this;
}
string& string::operator = (string&& rval) noexcept {
ptr_.reset(nullptr);
sz_ = rval.sz_;
ptr_ = std::move(rval.ptr_);
return *this;
}
string& string::operator = (const char* c_string) {
ptr_.reset(nullptr);
sz_ = std::strlen(c_string);
ptr_ = std::make_unique<char[]>(sz_ + 1);
std::strcpy(ptr_.get(), c_string);
return *this;
}
string& string::operator = (char ch) {
ptr_.reset(nullptr);
sz_ = 1;
ptr_ = std::make_unique<char[]>(sz_ + 1);
ptr_.get()[0] = ch;
ptr_.get()[1] = '\0';
return *this;
}
string& string::operator += (const string& rhs) {
std::unique_ptr<char[]> temp = std::make_unique<char[]>(sz_ + rhs.sz_ + 1);
std::strcpy(temp.get(), ptr_.get());
std::strcpy(temp.get() + sz_, rhs.ptr_.get());
ptr_.reset(nullptr);
sz_ += rhs.sz_;
ptr_ = std::move(temp);
return *this;
}
string& string::operator += (const char* rhs) {
size_t rhs_len = std::strlen(rhs);
std::unique_ptr<char[]> temp = std::make_unique<char[]>(sz_ + rhs_len + 1);
std::strcpy(temp.get(), ptr_.get());
std::strcpy(temp.get() + sz_, rhs);
ptr_.reset(nullptr);
sz_ += rhs_len;
ptr_ = std::move(temp);
return *this;
}
string& string::operator += (char ch) {
std::unique_ptr<char[]> temp = std::make_unique<char[]>(sz_ + 1 + 1);
std::strcpy(temp.get(), ptr_.get());
temp.get()[sz_] = ch;
temp.get()[sz_ + 1] = '\0';
ptr_.reset(nullptr);
sz_ += 1;
ptr_ = std::move(temp);
return *this;
}
string& string::operator += (string&& rval) {
std::unique_ptr<char[]> temp = std::make_unique<char[]>(sz_ + rval.sz_ + 1);
std::strcpy(temp.get(), rval.ptr_.get());
std::strcpy(temp.get() + sz_, rval.ptr_.get());
ptr_.reset(nullptr);
sz_ += rval.sz_;
ptr_ = std::move(temp);
return *this;
}
char& string::operator [] (size_t idx) {
return ptr_.get()[idx];
}
const char& string::operator [] (size_t idx) const {
return ptr_.get()[idx];
}
/**************************************** friend operator overloads *********************/
std::ostream& operator << (std::ostream& out, const string& str) {
if (str.size() > 0) {
out.write(str.c_str(), str.size());
}
return out;
}
string operator + (const string& lhs, const string& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (const string& lhs, const char* rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (const char* lhs, const string& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (string&& lhs, string&& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (string&& lhs, const char* rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (const char* lhs, string&& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (const string& lhs, string&& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (string&& lhs, const string& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (const string& lhs, char rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (char lhs, const string& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (string&& lhs, char rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
string operator + (char lhs, string&& rhs) {
string temp{lhs};
temp += rhs;
return temp;
}
bool operator == (const string& lhs, const string& rhs) noexcept {
return (lhs.sz_ == rhs.sz_) &&
((lhs.sz_ == 0) ? true : (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) == 0));
}
bool operator == (const string& lhs, const char* rhs) noexcept {
return (lhs.sz_ == std::strlen(rhs)) &&
((lhs.sz_ == 0) ? true : (std::strncmp(lhs.ptr_.get(), rhs, lhs.sz_) == 0));
}
bool operator == (const char* lhs, const string& rhs) noexcept {
return (strlen(lhs) == rhs.sz_) &&
((rhs.sz_ == 0) ? true : (std::strncmp(lhs, rhs.ptr_.get(), rhs.sz_) == 0));
}
bool operator == (const string& lhs, char rhs) noexcept {
return (lhs.sz_ == 1) &&
(lhs.ptr_.get()[0] == rhs);
}
bool operator == (char lhs, const string& rhs) noexcept {
return (rhs.sz_ == 1) &&
(lhs == rhs.ptr_.get()[0]);
}
bool operator == (const string& lhs, string&& rhs) noexcept {
return (lhs.sz_ == rhs.sz_) &&
((lhs.sz_ == 0) ? true : (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) == 0));
}
bool operator == (string&& lhs, const string& rhs) noexcept {
return (lhs.sz_ == rhs.sz_) &&
((lhs.sz_ == 0) ? true : (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) == 0));
}
bool operator == (string&& lhs, string&& rhs) noexcept {
return (lhs.sz_ == rhs.sz_) &&
((lhs.sz_ == 0) ? true : (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) == 0));
}
bool operator == (string&& lhs, char rhs) noexcept {
return (lhs.sz_ == 1) &&
(lhs.ptr_.get()[0] == rhs);
}
bool operator == (char lhs, string&& rhs) noexcept {
return (rhs.sz_ == 1) &&
(rhs.ptr_.get()[0] == lhs);
}
bool operator == (string&& lhs, const char* rhs) noexcept {
return (lhs.sz_ == std::strlen(rhs)) &&
((lhs.sz_ == 0) ? true : (std::strncmp(lhs.ptr_.get(), rhs, lhs.sz_) == 0));
}
bool operator == (const char* lhs, string && rhs) noexcept {
return (std::strlen(lhs) == rhs.sz_) &&
((rhs.sz_ == 0) ? true : (std::strncmp(lhs, rhs.ptr_.get(), rhs.sz_) == 0));
}
bool operator != (const string& lhs, const string& rhs) noexcept {
return !(lhs == rhs);
}
bool operator != (const string& lhs, const char* rhs) noexcept {
return !(lhs == rhs);
}
bool operator != (const char* lhs, const string& rhs) noexcept {
return !(lhs == rhs);
}
bool operator != (const string& lhs, char rhs) noexcept {
return !(lhs == rhs);
}
bool operator != (char lhs, const string& rhs) noexcept {
return !(lhs == rhs);
}
bool operator != (const string& lhs, string&& rhs) noexcept {
return (lhs.sz_ != rhs.sz_) || (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) != 0);
}
bool operator != (string&& lhs, const string& rhs) noexcept {
return (lhs.sz_ != rhs.sz_) || (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) != 0);
}
bool operator != (string&& lhs, string&& rhs) noexcept {
return (lhs.sz_ != rhs.sz_) || (std::strncmp(lhs.ptr_.get(), rhs.ptr_.get(), lhs.sz_) != 0);
}
bool operator != (string&& lhs, char rhs) noexcept {
return (lhs.sz_ != 1) || (lhs.ptr_.get()[0] != rhs);
}
bool operator != (char lhs, string&& rhs) noexcept {
return (rhs.sz_ != 1) || (rhs.ptr_.get()[0] != lhs);
}
bool operator != (string&& lhs, const char* rhs) noexcept {
return (lhs.sz_ != std::strlen(rhs)) || (std::strncmp(lhs.ptr_.get(), rhs, lhs.sz_) != 0);
}
bool operator != (const char* lhs, string && rhs) noexcept {
return (std::strlen(lhs) != rhs.sz_) || (std::strncmp(lhs, rhs.ptr_.get(), rhs.sz_) != 0);
}
/**************************************** iterator related implementations *********************/
using iterator = string::iterator;
iterator::iterator(string *str, size_t index) noexcept
: str_{str}, index_{index} {
}
iterator::iterator(const iterator& itr) noexcept
: str_{itr.str_}, index_{itr.index_} {
}
iterator::iterator(iterator&& rval) noexcept
: str_{rval.str_}, index_{rval.index_} {
}
iterator::~iterator() noexcept {
str_ = nullptr;
index_ = 0;
}
iterator& iterator::operator = (const iterator& rhs) noexcept {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
iterator& iterator::operator = (iterator&& rhs) noexcept {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
bool iterator::operator != (const iterator& rhs) const noexcept {
return (str_ != rhs.str_) || (index_ != rhs.index_);
}
bool iterator::operator == (const iterator& rhs) const noexcept {
return (str_ == rhs.str_) && (index_ == rhs.index_);
}
iterator& iterator::operator ++ () {
++index_;
return *this;
}
iterator& iterator::operator ++ (int dummy) {
++(*this);
return *this;
}
iterator& iterator::operator -- () {
--index_;
return *this;
}
iterator& iterator::operator -- (int dummy) {
--(*this);
return *this;
}
char& iterator::operator * () const {
return (*str_)[index_];
}
iterator string::begin() {
return iterator(this);
}
iterator string::end() {
return iterator(this, sz_);
}
using const_iterator = string::const_iterator;
const_iterator::const_iterator(const string* str, size_t index)
: str_{str}, index_{index} {
}
const_iterator::const_iterator(const const_iterator& itr)
: str_{itr.str_}, index_{itr.index_} {
}
const_iterator::const_iterator(const_iterator&& rval) noexcept
: str_{rval.str_}, index_{rval.index_} {
}
const_iterator::~const_iterator() {
str_ = nullptr;
index_ = 0;
}
const_iterator& const_iterator::operator = (const const_iterator& rhs) {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
const_iterator& const_iterator::operator = (const_iterator&& rhs) noexcept {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
bool const_iterator::operator != (const const_iterator& rhs) const noexcept {
return (str_ != rhs.str_) || (index_ != rhs.index_);
}
bool const_iterator::operator == (const const_iterator& rhs) const noexcept {
return (str_ == rhs.str_) && (index_ == rhs.index_);
}
const_iterator& const_iterator::operator ++ () {
++index_;
return *this;
}
const_iterator& const_iterator::operator ++ (int dummy) {
++(*this);
return *this;
}
const_iterator& const_iterator::operator -- () {
--index_;
return *this;
}
const_iterator& const_iterator::operator -- (int dummy) {
--(*this);
return *this;
}
const char& const_iterator::operator * () const {
return (*str_)[index_];
}
const_iterator string::cbegin() {
return const_iterator(this);
}
const_iterator string::cend() {
return const_iterator(this, sz_);
}
using reverse_iterator = string::reverse_iterator;
reverse_iterator::reverse_iterator(string *str, size_t index)
: str_{str}, index_{index} {
}
reverse_iterator::reverse_iterator(const reverse_iterator& itr)
: str_{itr.str_}, index_{itr.index_} {
}
reverse_iterator::reverse_iterator(reverse_iterator&& rval) noexcept
: str_{rval.str_}, index_{rval.index_} {
}
reverse_iterator::~reverse_iterator() {
str_ = nullptr;
index_ = 0;
}
reverse_iterator& reverse_iterator::operator = (const reverse_iterator& rhs) {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
reverse_iterator& reverse_iterator::operator = (reverse_iterator&& rhs) noexcept {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
bool reverse_iterator::operator != (const reverse_iterator& rhs) const noexcept {
return (str_ != rhs.str_) || (index_ != rhs.index_);
}
bool reverse_iterator::operator == (const reverse_iterator& rhs) const noexcept {
return (str_ == rhs.str_) && (index_ == rhs.index_);
}
reverse_iterator& reverse_iterator::operator ++ () {
--index_;
return *this;
}
reverse_iterator& reverse_iterator::operator ++ (int dummy) {
++(*this);
return *this;
}
reverse_iterator& reverse_iterator::operator -- () {
++index_;
return *this;
}
reverse_iterator& reverse_iterator::operator -- (int dummy) {
--(*this);
return *this;
}
char& reverse_iterator::operator * () const {
return (*str_)[index_];
}
reverse_iterator string::rbegin() {
return reverse_iterator(this, sz_ - 1);
}
reverse_iterator string::rend() {
return reverse_iterator(this, -1);
}
using reverse_const_iterator = string::reverse_const_iterator;
reverse_const_iterator::reverse_const_iterator(const string* str, size_t index)
: str_{str}, index_{index} {
}
reverse_const_iterator::reverse_const_iterator(const reverse_const_iterator& itr)
: str_{itr.str_}, index_{itr.index_} {
}
reverse_const_iterator::reverse_const_iterator(reverse_const_iterator&& rval) noexcept
: str_{rval.str_}, index_{rval.index_} {
}
reverse_const_iterator::~reverse_const_iterator() {
str_ = nullptr;
index_ = 0;
}
reverse_const_iterator& reverse_const_iterator::operator = (const reverse_const_iterator& rhs) {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
reverse_const_iterator& reverse_const_iterator::operator = (reverse_const_iterator&& rhs) noexcept {
str_ = rhs.str_;
index_ = rhs.index_;
return *this;
}
bool reverse_const_iterator::operator != (const reverse_const_iterator& rhs) const noexcept {
return (str_ != rhs.str_) || (index_ != rhs.index_);
}
bool reverse_const_iterator::operator == (const reverse_const_iterator& rhs) const noexcept {
return (str_ == rhs.str_) && (index_ == rhs.index_);
}
reverse_const_iterator& reverse_const_iterator::operator ++ () {
--index_;
return *this;
}
reverse_const_iterator& reverse_const_iterator::operator ++ (int dummy) {
++(*this);
return *this;
}
reverse_const_iterator& reverse_const_iterator::operator -- () {
++index_;
return *this;
}
reverse_const_iterator& reverse_const_iterator::operator -- (int dummy) {
--(*this);
return *this;
}
const char& reverse_const_iterator::operator * () const {
return (*str_)[index_];
}
reverse_const_iterator string::crbegin() {
return reverse_const_iterator(this, sz_ - 1);
}
reverse_const_iterator string::crend() {
return reverse_const_iterator(this, -1);
}
} //kapil
#endif
using namespace kapil;
int main() {
string x1{"hey man"};
std::cout << x1;
std::cout << " start : \n";
//string s98 = string{"hello"} + string{" brother"};
std::cout << " start : \n";
string s1{"hello"};
string s2;
s2 = std::move(s1);
string s3 = string{"brother"};
string s4{s3};
string s5{""};
s5 = "batloluaaa";
s5 += s3;
s3 += " i am added to s3 as const char*";
s4 += string{" i am added as rval "};
s4 = s3 + s5 + " s3 + s5";
s3 = s5 + " i am s5 + const char*";
s2 = " i am const char* + s5" + s5;
s1 = s2 + 'a';
s1 = "kapil";
s1[0] = 's';
s1.back() = '$';
s1.at(3) = '';
s1.front() = '~';
s1.append('f');
s1.append("aswd");
s1.assign("char assigned");
s3.assign(s2);
std::cout << "\n should call move \n";
s4.assign(string{"moved"});
std::cout << "\n called move \n";
s4.swap(s1);
std::cout << "\n start of loop \n";
for (auto it : s1) {
std::cout << it;
}
std::cout << "\n end of loop \n";
if (s1 != s4) {
std::cout << " s1 != s2 \n";
}
s2 = s4 = {"same"};
if (s2 == s4) {
std::cout <<" s2 == s4 and same \n";
}
auto it53 = s2.cbegin();
auto it75 = s2.cbegin();
it75 = it53;
std::cout << "\n reverse loop start \n";
for (auto it = s2.rbegin(); it != s2.rend(); ++it) {
std::cout << *it;
}
std::cout << "\n reverse loop end \n";
std::cout << "\n const reverse loop start \n";
for (auto it = s2.crbegin(); it != s2.crend(); ++it) {
std::cout << *it;
}
std::cout << "\n const reverse loop end \n";
std::cout << " s1 : " << s1 << std::endl << " s2 : " << s2 << std::endl << " s3 : " << s3 << std::endl << " s4 : " << s4 << std::endl << " s5 : " << s5 << std::endl;
s1 = s2;
if (s1 == string{s2}) {
std::cout << " s1 is same as temp s2 \n";
}
return 0;
}
`