String reference class for efficient operation of strings

Source: Internet
Author: User

As in the following code, a function accepts a std::string constant reference, and within its function you need to use some of the functions of the std::string to manipulate the string.

void foo (const std::string& param) {  ...}

The parameter uses a constant reference and does not require an additional copy if it is passed into a std::string. However, if a string constant is passed in at the time of invocation, a Std::string object is generated and a memory copy is created.

The following code tests can be used to copy a string occurrence:

#include <string>namespace {const char* sstring = "123456"; void foo (const std::string& str) {  printf ("Inpu T string Address:%x\n ", Str.c_str ());}}  Namespaceint Main () {  printf ("Const string Address:%x\n", sstring);  Foo (sstring);  return 0;}

This is a typical problem, in fact, as long as you do not modify the contents of the string, there is no need to copy another. Especially for larger strings, avoiding copying can be a great benefit to memory and performance. So Jeffrey Yasskin presented a string reference:a non-owning reference to a string. Many large projects offer their own implementations, including Boost::stringref, LLVM stringref, chromium base::stringpiece.

(StL also has a string of copy-on-write implementations, but depends on the implementation version.) Here are a few more notes: Std::string's copy-on-write: Better than the imagination. )


The following is an example of stringpiece. Its principle is also very simple, stringpiece internal only holds the string pointer and a length value, and then refer to the Std::string interface provides a set of operation functions. Like find, find_first_of, RFind, substr.

  Template <typename string_type> class Basicstringpiece {public:  ...  Basicstringpiece substr (size_type pos,                          size_type n = basicstringpiece::npos) const {    return INTERNAL::SUBSTR (* This, POS, n);  } Protected:  const value_type* PTR_;  Size_type     length_;};
typedef basicstringpiece<std::string> STRINGPIECE;

It does not release strings when it is refactored, because Stringpiece does not take ownership of strings, that is, the life cycle of the string object itself must be longer than the Stringpiece object.

You can see that stringpiece is a template class, primarily because it needs to support parameters that are passed in both std::string and C string.


Taking the operation of the substring provided above as an example, a basic idea is to create a new stringpiece, pointing its pointer to the starting position of the substring, and then setting the length of the substring to the length of the string. There is no copy of the string in this process.

Template<typename str>basicstringpiece<str> substrt (const basicstringpiece<str>& Self,                              size_t POS,                              size_t N) {  if (pos > Self.size ()) pos = Self.size ();  if (n > Self.size ()-pos) n = self.size ()-Pos;  Return basicstringpiece<str> (Self.data () + POS, n);}


Enjoy it!



String reference class for efficient operation of strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.