C + + Learning notes-standard library type __c++

Source: Internet
Author: User

—————— warn yourself that you are "in danger" when you are young and do nothing. Uphill is very tiring, when you feel relaxed, then you are in the downhill. From "Vozki Sood"

A


1, String type

Different from the string in Java, the first case is not the same. Be aware of when you are accustomed to the Java language format. Use steps:

A: The introduction of header file <string>//note with angle brackets, because belong to the system header file, different from C header file, no suffix ". h";

B: namespaces, using std::string//are primarily to prevent name collisions;

String operation: (Simple to enumerate, and Java usage and meaning is basically consistent)

S.empty ()//judgment is empty;

S.size ()//Get the number of string characters

s1+s2;//splicing operation;

==//comparison Content

Note that: "Hello" + "world"//error, are the right value can not be spliced. At this point "+" is treated as a plus operator, so the error.

The size () function returns the type that is best not to use int, because the memory of int is too small, error is not correct. and C + + stipulates the return type of size string::size_type;

Traversing the general practice is also somewhat different: for (string::size_type idx = 0;idx!= s.size (); idx++)//The difference is the size return type and the decision condition (later)


The processing function for characters in string is the following table://character processing rather than string

Function name return value
Isalnum () If the argument is alphanumeric, or a letter or number, the function returns True
Isalpha () If the argument is a letter, the function returns a true
Isblank () If the argument is a space or a horizontal tab, the function returns True
Iscntrl () If the argument is a control character, the function returns True
IsDigit () If the argument is a number (0~9), the function returns True
Isgraph () If the parameter is a print character other than a space, the function returns True
Islower () If the argument is a lowercase letter, the function returns True
Isprint () If the argument is a printed character (including a space), the function returns True
Ispunct () If the argument is a punctuation mark, the function returns True
Isspace ()

If the parameter is a standard white-space character, such as a space, paper feed, line break, carriage return

, horizontal tab, or Vertical tab, this function returns True

Isupper () If the argument is uppercase, the function returns True
Isxdigit () If the argument is a hexadecimal number, that is, 0~9, A~f, a~f, the function returns True
ToLower () If the argument is a uppercase character, it returns its lowercase, otherwise the parameter is returned
ToUpper () If the argument is a lowercase letter, it is returned in uppercase, otherwise the argument is returned


2. Vector

The use of vector vectors is similar to the Java Collection set (List, set), and so on, which can dynamically add objects to the end. The method of operation is similar. As with string, you need to have header files and namespaces,

A: The introduction of header file <vector>//note with angle brackets, because belong to the system header file, different from C header file, no suffix ". h";

B: namespaces, using std::vector//are primarily to prevent name collisions;

Vector<string> VEC;

Add the object to the end using Vec.push_back ();//.

Here the value can be taken as a subscript. VEC[4], take the 5th element.

Iterators

Iterating through collections in Java typically uses iterators. So is the vector of the container. Code:

Vector<string>:: Iterator iter = Vec.begin ();

To access the available solution references for an element: *iter

There is actually an iterator that uses and accesses only, that is, cannot be modified. Code:

Vector<string>::const_iterator iter = Vec.begin (); 3, Bitset

Bitset bitwise operation, the definition is the same as the above two. Note that when Bitset is initialized for strings, the code

String str ("1100");

bitset<32> Bitvec (str);

The 2nd and 3rd positions in the Bitvec bit pattern are 1, that is, the string object and the Bitset object are reversed, and the order is reversed when the Bitset object is initialized with a string object.

B.any ()

Whether there is a bits of 1 in B.

B.none ()

There is no bits for 1 in B.

B.count ()

Number of bits with 1 in b

B.size ()

Number of bits in B

B[pos]

Access to bits at POS in B

B.test (POS)

B in the POS at the BITS is 1.

B.set ()

Put all the bits in B to 1.

B.set (POS)

Place the binary position of B in the POS at 1

B.reset ()

Put all the bits in B to 0.

B.reset (POS)

Place the binary position of B in the POS at 0

B.flip ()

Take all the bits in B to the opposite position

B.flip (POS)

Take the bits in the POS at the B

B.to_ulong ()

Returns a unsigned long value with the same bits in B

OS << b

Output the bit set in B to the OS stream






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.