string (C++)

From Seo Wiki - Search Engine Optimization and Programming Languages
Jump to navigationJump to search

In the C++ programming language, the std::string class is a standard representation for a string of text. This class removes 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.

Usage

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.

typedef basic_string<char> string;

which is equivalent to

basic_string<char, char_traits<char>, allocator<char> >

Simple usage includes:

#include <iostream>
#include <string>
 
int main() {
  std::string foo = "hi";
  using std::string;
  // Now we can just say "string".
  string bar = "there";
  if (foo == bar) { // operator== compares string contents for equality
    std::cout << "The strings are the same"<<std::endl;// this line will not print
  }
  std::cout << foo + bar << std::endl; // Prints "hithere" (without quotes) by creating a temporary object

  return 0;
}

Because a string may be stored by value, copying may take as long as θ(n) (i.e., copying takes time proportional to the length of the string). For that reason, string is generally passed by reference-to-const to avoid unnecessary copying:

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

To interoperate with legacy code, 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 characters in the original string. If the string is modified or its lifetime ends, the pointers returned by calls to c_str are invalid.

Operators

The string class is fitted with a number of mathematical and logical operators for ease of use. All operators for the string class have member function equivalents, and all can accept another string or character-string, so operators are purely for appearance and have no other advantage. All operators can also accept single characters; some libraries, however, might not accept a single character in the assignment operator (=). Most allow the left operand to be a character-string.

The most basic, and obvious, is the assignment operator.

string str;
str = "abc"; // Assignment operator accepts string literals.
str = 'a';   // Invalid in some libraries -- assignment operator might not accept a char.
char* c_str1 = str; // Invalid -- a character string cannot take the value of a string.
const char* c_str2 = str.c_str();  // valid, but immutable;  do not delete c_str2!
char* c_str3 = new char[str.size() + 1]; strcpy(c_str3, str.c_str()); // mutable, but must delete c_str3
const char* c_str4 = str.data(); // similar to c_str(), but without terminating zero character

The string class also has a good few options for concatenation.

string str1 = "qwe";
string str2 = "rty";

string str3;
str3 = str1 + str2;  // str3 = "qwerty"
str3 = "qwe" + str2; // Constructs a temporary string object from the string literal
str1 += str2; // str1 = "qwerty"

str1 -= str2; // Invalid -- subtraction operations are not supported.
str3 = "qwe" + "rty"; // Invalid -- addition of string literals not defined

Strings can be tested for equality with operator==. This is a syntactic advantage to c-strings where operator== evaluates to true only if the pointers refer to the same address. Also, !=, <=, and >= are valid operations for strings. The following example assumes that the implementation uses ASCII character encoding.

string str1 = "Banana";
string str2 = "Banana";

assert( str1 == str2 );

string str3 = "Apple";
assert( str1 > str3 ); // True because 'A' is lower than 'B' on the ASCII table.

assert( str1 < "apple" ); // True because 'B' comes before lower-case 'a' on the ASCII table.

Lastly is the random access operator. It allows a string to act as a character-string, returning a reference to a char.

string person = "Jakw"; // Should be "Jake".
person[3] = 'e';
char j = person[0];

Relative classes

In C++, the class string is just an instantiation of a template basic_string, which provides functionality for strings consisting of any characters[1]. There is a similar class wstring, which consists of wide characters.

External links

If you like SEOmastering Site, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...