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.

1
  • 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. Commented Aug 27, 2011 at 13:08

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I guess you are right but, can u give some code on how to do string manipulations? e.g concatenation ?
Better to have the return type as const StrType.
@jaga: The guideline to return const objects is no longer recommended since move semantics 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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.