Jump to content

C++ string handling: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Usage: Fixed link to null terminated string
I have added a good reference for strings and found that This reference not working http://www2.roguewave.com/support/docs/sourcepro/edition9-update1/html/stdlibref/string.html
Line 92: Line 92:
A std::basic_string can be made of any C++ object, even user-defined classes, similar to how a [[std::vector]] can be made of any class. They are also an [[Sequence container (C++)|STL container]], and thus the functions in the [[Algorithm (C++)|STL Algorithm Library]] can be applied to the code units in strings, though these are of limited use as they rarely deal with sequences or patterns of items.
A std::basic_string can be made of any C++ object, even user-defined classes, similar to how a [[std::vector]] can be made of any class. They are also an [[Sequence container (C++)|STL container]], and thus the functions in the [[Algorithm (C++)|STL Algorithm Library]] can be applied to the code units in strings, though these are of limited use as they rarely deal with sequences or patterns of items.


Thus the class <code>std::string</code> provides functionality for strings consisting of the C <code>[[Byte|char]]</code><ref>http://www2.roguewave.com/support/docs/sourcepro/edition9-update1/html/stdlibref/string.html</ref> (not to be confused with actual "characters", which may consist of several of the units in the string, such as when [[UTF-8]] encoding is used).
Thus the class <code>std::string</code> provides functionality for strings consisting of the C <code>[[Byte|char]]</code>
<ref>{{cite web
| title=C++ Strings</code>
| url=https://www.tutorialcup.com/cplusplus/strings.htm
| publisher=TutorialCup.com
| accessdate=17 February 2015
}}</ref>
(not to be confused with actual "characters", which may consist of several of the units in the string, such as when [[UTF-8]] encoding is used).


There is a similar class <code>std::wstring</code>, which consists of <code>[[wchar_t]]</code>, and is most often used to store [[UTF-16]] text on Windows and [[UTF-32]] on Unix. As the difference between these made writing portable code that shared data structures impossible, [[C++11]] added two new classes: <code>std::u16_string</code> and <code>std::u32_string</code> (made up of the new types <code>char16_t</code> and <code>char32_t</code>), which are the given number of bits per code unit on all platforms.<ref>{{cite web
There is a similar class <code>std::wstring</code>, which consists of <code>[[wchar_t]]</code>, and is most often used to store [[UTF-16]] text on Windows and [[UTF-32]] on Unix. As the difference between these made writing portable code that shared data structures impossible, [[C++11]] added two new classes: <code>std::u16_string</code> and <code>std::u32_string</code> (made up of the new types <code>char16_t</code> and <code>char32_t</code>), which are the given number of bits per code unit on all platforms.<ref>{{cite web

Revision as of 08:19, 17 February 2015

In the C++ programming language, the std::string class is a standard representation for a string of text. This class alleviates many of the problems introduced by C-style strings by putting the onus of memory ownership on the string class rather than on the programmer. The class provides some typical string operations like comparison, concatenation, find and replace, and a function for obtaining substrings. It can be constructed from a C-style string, and a C-style string can also be obtained from it.

Overview of functions

    • string::string(constructor) – Constructs the string from variety of sources
    • string::~string(destructor) – Destructs the string
    • string::operator= – Replace the string
    • string::assign – Replace the string
    • string::get_allocator – Returns the allocator used to allocate memory for the bytes
  • Byte access
    • string::at – Accesses specified byte with bounds checking.
    • string::operator[] – Accesses specified byte
    • string::front – Accesses the first byte
    • string::back – Accesses the last byte
    • string::data – Accesses the underlying array
  • Iterators
    • string::begin – Returns an iterator to the beginning of the string
    • string::end – Returns an iterator to the end of the string
    • string::rbegin – Returns a reverse iterator to the reverse beginning of the string
    • string::rend – Returns a reverse iterator to the reverse end of the string
  • Capacity
    • string::empty – Checks whether the string is empty
    • string::size – Returns the number of bytes in the string.
    • string::max_size – Returns the maximum possible number of bytes in the string.
    • string::reserve – Reserves storage in the string
    • string::capacity – Returns the current reservation
    • string::shrink_to_fit (C++11) – Reduces memory usage by freeing unused memory
  • Modifiers
    • string::clear – Clears the contents
    • string::insert – Inserts bytes or strings
    • string::erase – Deletes bytes
    • string::push_back – Appends a byte
    • string::append – Appends bytes or strings
    • string::operator+= – Appends
    • string::pop_back – Removes the last byte
    • string::resize – Changes the number of stored bytes
    • string::swap – Swaps the contents with another string
    • ...
  • Search
    • ...

Usage

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string foo = "fighters";
    string bar = "stool";

    // "!=" compares string contents for inequality, even though they are different objects.
    if (foo != bar)
    {
        cout << "The strings are different." << endl;
    }

    // Prints "stool fighters" by creating a temporary object, which is automatically freed.
    cout << bar + " " + foo << endl;

    return 0;
}

Because a string may be stored by value, copying may take as long as O(n) (i.e., copying takes time proportional to the length of the string). It will also cause heap memory to be allocated, which is usually far more expensive than the copy. For that reason, string is generally passed by reference-to-const to avoid unnecessary copying:

void print_the_string(const string& str)
{
    cout << str;
}

To interoperate with C-interfaces, it is often necessary to obtain a null terminated string from a basic_string. The c_str() member function yields a pointer to the first element of an array whose elements correspond to the bytes in the original string and this array has a 0 at the offset of the length. If the null terminator is not needed, the data() method returns the pointer without any work needed to make sure the 0 is there. If the string is modified or its lifetime ends, the pointers returned by these methods become invalid.

std::string is a typedef for a particular instantiation of the basic_string class template. Its definition is found in the <string> header and in the std namespace.[1]

typedef basic_string<char> string;

A std::basic_string can be made of any C++ object, even user-defined classes, similar to how a std::vector can be made of any class. They are also an STL container, and thus the functions in the STL Algorithm Library can be applied to the code units in strings, though these are of limited use as they rarely deal with sequences or patterns of items.

Thus the class std::string provides functionality for strings consisting of the C char [2]

(not to be confused with actual "characters", which may consist of several of the units in the string, such as when UTF-8 encoding is used).

There is a similar class std::wstring, which consists of wchar_t, and is most often used to store UTF-16 text on Windows and UTF-32 on Unix. As the difference between these made writing portable code that shared data structures impossible, C++11 added two new classes: std::u16_string and std::u32_string (made up of the new types char16_t and char32_t), which are the given number of bits per code unit on all platforms.[3]

References

  1. ^ "C++ reference for basic_string". Cppreference.com. Retrieved 11 January 2011.
  2. ^ "C++ Strings". TutorialCup.com. Retrieved 17 February 2015.
  3. ^ "C++11 Paper N3336". Open Standards. Programming Language C++, Library Working Group. 13 January 2012. Retrieved 2 November 2013.