answersLogoWhite

0

A string is a sequence of character data types typically stored in a vector or an array. The two main string classes are std::string and std::wstring, both of which can be found in the <string> header file. std::string is a specialisation of std::vector<char> while std::wstring specialises std::vector<wchar_t>. The latter is often known as wide-string, ideally suited to UTF-16 encoding. The former can be used for both ASCII and UTF-8 encoding. These are generally all you need.

You can construct objects of the string classes as follows:

std::string str ("Hello world!"); // C++ string

std::wstring wstr (L"Hello world!"); // C++ wide-string

You can also make use of C-style strings, however it's best to limit their use to fixed-length strings. These can be declared as follows:

char cstr[] = "Hello world!";

wchar_t cwstr[] = L"Hello world!";

You can use variable length C-strings if you wish but you're simply creating unnecessary work for yourself. The only real advantage is that you save some memory (4-bytes per string on a 32-bit system) but restrict yourself to the C standard library functions, most of which are inefficient compared to the equivalent C++ string member methods (primarily because the size of a C++ string does not need to be continually recalculated -- it is a given). C++ strings are also easily converted to and from C-style strings and they manage their own resources for you, which ultimately makes them much easier to use.

Although not physically part of the Standard Template Library, C++ strings adhere to STL semantics, including bidirectional, random-access iteration through the character sequence as well as subscript notation to access individual characters. STL algorithms and functions can also be applied, although the member functions are sufficient for most of the operations you will need to perform upon strings, such as appending strings and searching within strings.

Note that insertion of one string within another is not supported. This is because vectors are specifically designed to add new elements to the end of the sequence rather than the middle or beginning of the sequence. To insert one string inside another, it is more efficient to create a new string, reserving the number of characters required for both the original string plus the inserted sub-string, and simply appending the sub-strings accordingly. The new string can then be assigned to the original string if required.

By reserving characters, the new string doesn't have to be resized with each append operation. Resizing only occurs when an append operation exceeds the current allocation and may result in a reallocation if the current memory block has no room to expand. Reallocations are expensive as the original string must be copied to the new allocation, which means you need more than double the memory of the original allocation (at least for the duration of the copy operation), not to mention the performance impact of physically copying the string. The fewer reallocations you make the better, so plan ahead. Calculate and reserve what you need in advance.

C++11 also introduces move semantics which helps eliminate a lot of the copying operations that would otherwise occur when passing string values to or from functions (thus creating temporary strings). For instance, when you assign a function that returns a string to another string, two complete copies of the string are made. The first is made by the function itself, copying its local string to the function's return value (thus creating a temporary). The second is made when that return value is subsequently assigned to your string (if you don't assign, the temporary simply falls from scope, but still results in an unnecessary copy being made). With move semantics, no copies are made, the temporary simply takes ownership of the function's string and then passes that ownership onto your string.

This is made possible by the fact a string is nothing more than a pointer to a memory allocation plus the size of that allocation (in characters). By passing these two values alone, the new string immediately takes ownership of the allocation. The old string simply nullifies its pointer so when it falls from scope it doesn't destroy the allocation. As an implementation detail, all the actual work is done Behind the Scenes so the user is largely unaware of what's going on, the only visible sign being the much-improved performance. Move semantics apply to the entire STL in C++11 but can also be applied to your own classes. If you classes "own" resources, remember to provide move semantics so they can pass that ownership on automatically. Move semantics are achieved through a move constructor as well as a move assignment operator. It won't entirely eliminate the need to copy objects, but it will completely eliminate the need to make unnecessary copies.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

What is a Syntex error?

In computer science a syntax error refers to an error in a sequence of characters which need to be in a specific programming language.


What is a syntex?

In computer science a syntax error refers to an error in a sequence of characters which need to be in a specific programming language.


What is syntex?

In computer science a syntax error refers to an error in a sequence of characters which need to be in a specific programming language.


How do you solve error c plus plus ntvdm CPU illegal?

Use the debugger to locate the error.


Microsoft visual c plus plus rumetime library runtime error cant access game?

runtime error


When was Welspun Syntex Ltd created?

Welspun Syntex Ltd was created in 1982.


When you install visual c plus plus 2008 express edition it says an error?

Use the Microsoft support website to locate the error message and the solution to the error.


Syntex for accessing data members of the structure in c plus plus?

Use the member accessor (.) operator. struct object { int m_data; }; int main() { object o; o.m_data = 100; std::cout &lt;&lt; o.m_data &lt;&lt; std::cout; return(0); }


How do you fix the Microsoft c plus plus error?

Too many errors possible to say. Please elaborate.


Why wont your C plus plus programs compile?

C++ programs won't compile if they contain compiler errors. The compiler will tell you precisely where the error is, and the type of error, unless the error is in a macro. The compiler cannot see macro definitions because they are inline expanded prior to compilation.


What kind of an error in c plus plus is dividing by zero?

The answer is in your own question. A divide by zero error is a divide or mod by zero type of error. In MSVC++ it has the error code C2124. Ultimately it is a fatal error and will either produce a compile time error or throw an unhandled exception at runtime.


How do you solve this error under-fined symbol 'cin'in c plus plus?

#include &lt;iostream&gt; using namespace std;