开发者

Replace a substring inside a string (c++)

I'm building my own String class and I want to write my own replace method for the class

However I do开发者_StackOverflown't know how to get the position (int) of the first occurrence of substring inside the string.

My replace method should take 2 arguments, String searchString, String newString. It works like this

String example="AppLe";

example.replace("L", "banana");

I need to somehow obtain the position of L inside example string. for my replace function to have as result "Appbananae"

Please make notice that I'm writing my own String class because I'm taking an object oriented programing course at school to learn about classes. Because of that I cannot use "find()" from the standard library string class because that ruins the purpose. Thanks a lot


Although this might not be of direct help, it is practical: I suggest using the standard library strings rather than try to build your own class. It takes time and is bound to be error prone.


Assuming your string class will store the current value in an ASCIIZ buffer, you can use strstr().


Please make notice that I'm writing my own String class because I'm taking an object oriented programing course at school to learn about classes. Because of that I cannot use "find()" from the standard library string class because that ruins the purpose.

It would not make sense to use the standard library string class, but it does make sense to use container-agnostic algorithms:

template<class ForwardIterator1, class ForwardIterator2>
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
                        ForwardIterator2 first2, ForwardIterator2 last2);

template<class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
                        ForwardIterator2 first2, ForwardIterator2 last2,
                        BinaryPredicate pred);

Returns: The first iterator i in the range [first1, last1 - (last2 - first2)) such that for any non-negative integer n less than last2 - first2 the following corresponding conditions hold: *(i + n) == *(first2 + n), pred(*(i + n), *(first2 + n)) != false. Returns last1 if no such iterator is found.

[Source: C++03 §25.1.9]

Std::search does exactly what you want to do. If you are still not allowed to use container-agnostic algorithms, then you can implement std::search yourself under a new name and use that. (Such a prevention on std::search is a braindead requirement precisely because you can imitate it exactly without relying on any details of your String class.)

Here is a naive (meaning obvious and it works, but perhaps more slowly than possible) implementation:

template<class IterA, class IterB>
IterA search(IterA a_begin, IterA a_end, IterB b_begin, IterB b_end) {
  for (IterA start = a_begin; start != a_end; ++start) {
    IterA a = start;
    for (IterB b = b_begin; a != a_end; ++b, ++a) {
      if (b == b_end) return start;
      if (*b != *a) break;
    }
  }
  return a_end;
}

Note it's more verbose to call std::equal from this implementation because you don't know that distance(b_begin, b_end) <= distance(a_begin, a_end), and std::equal has that requirement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜