“This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

1, the namespace using

  • Library functions are basically in the using namespace, for example:std::vector
  • Using declarations make it easier to use members of a namespace

The format is as follows:

using namespace::name/ / 1
Copy the code

Or:

using namespace name_of_namespace/ / 2
Copy the code

The difference is that 1 can only use the name of the declared namespace, while 2 can use the used member of that namespace

The sample

namespace name_b
{
    int a=20;
    namespace name_c
    {
        struct student
        {
          string name;
          intage; }; }}Copy the code

C++ notes that using has the same function as typedefs — aliases for classes in variables and primitives (2)

2. Initialize the string string

  • Default initialization: string s1; S1 is an empty string
  • Copy constructor (deep copy in library practice) (overloaded constructor) : string s2=s1; S2 is a duplicate of s1. String s3=”haha”; S3 is a duplicate of HAHA
  • Constructor with arguments: string s4(10,’C’); S4 is CCCCCCCCCC

Copy initialization and direct initialization

If a variable is initialized with the = sign, copy initialization is performed, whereas if it is not initialized with the = sign, direct initialization is performed

The sample

string s4(10.'C'); // Direct initialization
string s3="haha";// Copy initialization
string s2=s3;// Copy initialization
string s5=string(10.'C'); // Create an implicit (temporary) variable and copy initialization
Copy the code

3. Operations on strings

  • All string s,s1,s2; As an example

    • OS <
    • Is >> S reads a string from the IS input stream to S, blank delimited, getLine (is, s), reads a line from IS to S, and returns IS
    • S.mpty () Specifies the string to be null
    • S.size () or s.length() : Returns the number of characters in s
    • S [n] returns a reference to the string with index n
    • <,>,<=,>=, ==! = Lexicographical comparison is case-sensitive
    • S1 +s2 returns the concatenation of S1 and S2
    • S1 =s2, deep-copy assignment
    • S.push_back (ch) adds the character ch to the end
    • S.pop_back () pops up from the end
    • S.insert () inserts a character or string
    • S.substr () s is a string of characters
    • S.clear () deletes all characters
    • S.ase () removes some characters or strings
      • Wait, these are the most common ones
  • String characters are traversed using the range for in the following format

for(declaration:expression)
    statement
Copy the code

Example: Output each character in STR

for(auto &ch:str)
    cout<<ch<<endl;
Copy the code

Processing string characters (processing characters) (with the help of the ctype.h header file, aka ccType)

  • Isalnum (c) True if c isa numeric or alphabetic character

  • Isalpha (c) True if c is an alphabetic character

  • Iscntrl (c) True if c is a control character

  • Isdigit (c) Returns true if c is a numeric character

  • Isgraph (c) c is not a space but can be printed to return true

  • Islower (c) True if c is a lowercase character

  • Isprint (c) Returns true if c is a printable character, and c is a space or has the but form

  • Ispunct (c) c is returned true if it is a punctuation character

  • Isspace (c) The value is returned to true if c is a blank character. Blank characters include Spaces, horizontal tabs, vertical tabs, carriage return, line feed, and paper feed

  • Isupper (c) True if c is a lowercase character

  • Isxdigit (c) Returns true when c is a hexadecimal digit character

  • Tolower (c) from lowercase to uppercase

  • Toupper (c) uppercase to lowercase

Overloading []

The [] subscript operator is overloaded by strings so that strings can access characters in a similar way to arrays, but the index IDX cannot exceed the size of the string

Example:

string str(10,a);
for(int i=0; i<str.size(a); ++i){ cout<<str[i]<<endl }Copy the code