The standard library string of --C ++ 11 (fifteen)

#include < String >
 String Compose ( const  String & name, const  String & Domain) 
{ 
    return name + ' @ ' + Domain;    // use constructor returns move, copy does not exist, will be efficient 
} 

S + = " ABC " ;   /// / additional characters 
string m = name.substr ( 2 , . 5 ); // extract a substring 
name.replace ( 0 , . 5 , " ABC " );   //Alternatively from 0, 5 sub-length string is "ABC" 
name.c_str ();   // converted to C-style string, returns a pointer to a pointer to a character name 
Auto S = " CAT " S;   // STD :: String type 
Auto P = " Dog " ;   // C-style string: const char * type

 

String string implementation:

Template <typename Char>
 class the basic_string {
     // Char type of string 
}
 the using  String = the basic_string < char >;   // define an alias
 // user definable arbitrary character string type, such as Chinese simplified 
the using Cstring the basic_string = <cchar> ;

 

String view string_view:

It is basically a (pointer, length) pair represents a sequence of characters, on a similar concept in golang slices.

You can achieve access to a continuous sequence of characters, which is similar to a pointer or reference, because it does not have the character it points by string_view.

string_view is a read-only view of its characters.

String cat(string_view sv1, string_view sv2)
{
    string res(sv1.length()+sv2.length());
    char* p = &res[0];
    for (char c: sv1)
        *p++ = c;
    copy(sv2.begin(), sv2.end(), p); //类似迭代器
}
string a = "abc";
auto s1 = cat(a, "mcd"); // string和const char*
auto s2 = cat("abd", "ddd"sv); // const char* 和string_view
auto s3 = cat({&king[0],2}, "hdd"sv); //abhdd

Guess you like

Origin www.cnblogs.com/share-ideas/p/11921297.html