1

In C++ - The Complete Reference, the author gives us a challenge after showing how he implements a custom C++ string class. Excerpt from the book:

A Challenge: Try implementing StrType (the string class) using the STL. That is, use a container to store the characters that comprise a string. Use iterators to operate on the strings, and use the algorithms to perform the various string manipulations.

I understand the basic concept here, but am having trouble implementing it. should I do std::vector < char > and push_back for every char or something like that? What about the string manipulations? Need some help. Sample code will be accepted gratefully, or you can explain how I may be able to implement this.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
  • 2
    Did you even *try* to create your own concatenation function? You will learn nothing if you ask others for completely solving the problem for you. – Doc Brown Aug 27 '11 at 13:08

2 Answers2

2

Yes, std::vector<char> sounds like a great idea. It will save you from the troubles of writing a custom destructor, copy constructor and copy assignment operator. Plus all the iterator member functions (begin, end and co.) can just delegate to the std::vector<char> versions.

can u give some code on how to do string manipulations? e.g concatenation ?

Sure thing, here is how I would overload operator+= and operator+ for the string type:

class StrType
{
    std::vector<char> vec;

public:

    // ...

    StrType& operator+=(const StrType& rhs)
    {
        vec.insert(vec.end(), rhs.vec.begin(), rhs.vec.end());
        return *this;
    }
};

StrType operator+(StrType lhs, const StrType& rhs)
{
    lhs += rhs;
    return lhs;
}

There's probably a more efficient version of operator+, but you can figure that out on your own.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • Thanks I guess you are right but, can u give some code on how to do string manipulations? e.g concatenation ? – ApprenticeHacker Aug 27 '11 at 12:16
  • Better to have the return type as const StrType. – Jagannath Aug 28 '11 at 05:04
  • @jaga: The guideline to return const objects is no longer recommended since [move semantics](http://stackoverflow.com/questions/3106110/) were added to the language. If `a + b` is const, you cannot move from it. Instead, you have to copy, which is very inefficient with strings. – fredoverflow Aug 28 '11 at 07:46
0

Using std::vector<char> would probably be the best container to use in this case (random access iterators and low overhead make it an attractive choice for a string).

Further to your comment on FredOverflow's answer, you can perform a string concatenation as follows:

std::vector<char> firstString;
firstString.push_back('A');
firstString.push_back('B');
std::vector<char> secondString;
secondString.push_back('X');
secondString.push_back('Y');

firstString.insert( firstString.end(), secondString.begin(), secondString.end() );

for( auto it = firstString.begin(); it != firstString.end(); ++it )
{
     std::cout << (*it);
}

In this case this would print out: ABXY. You can see it here: http://ideone.com/OmdoU

Thomas Russell
  • 5,870
  • 4
  • 33
  • 68