Chapter III C + +: string strings, vector vectors and arrays

Source: Internet
Author: User
Tags uppercase letter
Chapter 2 introduces built-in types. This chapter introduces abstract data type libraries.

-Among them, string and vector are the two most important standard library types. The former supports variable-length strings and the latter represents variable-length collections.
-Another standard library type is the iterator, which is a matching type of string and vector. It is often used to access characters in strings and elements in vectors.

Chapter 3 Strings, Vectors, and Arrays
The header file should not contain a using statement.

Standard library type string
#include <string> using std :: string
Initialization string

Direct initialization

Copy initialization: use the equal sign

string s1; string s2 (s1); string s2 = s2; string s3 ("hiya"); string s3 = "hiya"; string s4 (10, 'c'); // The content of s4 is cccccccccc
Operations on string objects

1. cout << s2. Cin >> s // Read the string object into s and stop when it encounters a blank 3. getline (is, s) // Read a line from the input stream is and assign it to s (newline characters are read Taken, but discarded without assigning to s), returns is4. S.empty () // returns true if s is empty, otherwise returns false5. S.size () 6. S [n] // returns the first in s n character quotes, position n starts from 0
7. s1 + s2 // Return the result of connecting s1 and s2 8. s1 = s29. S1 == s210. S1! = S211. <, <=,>,> =
Logical NOT operator (!)

string :: size_type Type: The return value type of s.size () is a matching type of the string class. It is an unsigned value that can hold the size of any string object.

Handling characters in string objects
cctype header file: functions for judging or processing single characters

isupper (c) // true if c is an uppercase letter islower (c) tolower (c) // if c is an uppercase letter, the corresponding lowercase letter is output; otherwise toupper (c) isalpha (c) // c is output as is True when it is a letter isdigit (c) // true when c is a number isalnum (c) // true when c is a letter or number ispunct (c) // true when c is punctuation isspace (c) // true if c is blank (that is, c is a type of space, horizontal tab, vertical tab, carriage return, line feed, or paper feed) iscntrl (c) // true if c is a control character isgraph (c) // c is not a space but is true when it is printable isprint (c) // c is true when it is a printable character (ie c is a space or has a visual form) isxdigit (c) // Hexadecimal number
Use the C ++ version of the C standard library header files of the form name.h. In C ++++ these files are named cname with the same contents, and the names defined in the header file cname belong to the namespace std.

Range for statement

for (declaration: expression)
    statement

For example: string str ("some string"); // Each line outputs a character in str for (auto c: str) cout << c << endl;
Standard library type vector
vector is a class template. A vector represents a collection of objects, where all objects are of the same type. Each object in the collection has a corresponding index, which is used to access the object. Vectors are often called containers.

Define and initialize vector objects

vector <T> v1vector <T> v2 (v1) vector <T> v2 = v1vector <T> v3 (n, val) vector <T> v4 (n) vector <T> v5 {a, b, c ... } vector <T> v5 = {a, b, c ...}
List initialization vector object

vector <string> articles = {"a", "an", "the"};
Add elements to a vector object
The exact number of vector objects is known at runtime:

string word; vector <string> text; while (cin >> word) {
    text.push_back (word);
}
Vector objects can grow efficiently: vectors can add elements quickly and efficiently at runtime.

If the body of the loop contains a statement that adds elements to a vector object, you cannot use a ++ scope for ++ loop.

push_back: "Push" a value as the tail element of the vector object "back" of the vector object.

vector <int> v2; for (int i = 0; i! = 100; ++ 1)
    v2.push_back (i);
Other vector operations
v.empty ()
v.size ()
v.push_back (t)
v [n] // Returns a reference to the element at the nth position in v v1 = v2v1 = (a, b, c ...) v1 == v2v1! = v2
<, <=,>,> =
Iterator
A general mechanism: implement the use of subscript operators to access characters of string objects or elements of vector objects.

Like pointer types, it provides indirect access to objects.

begin and end members
// The compiler determines the type of b and e // b represents the first element of v, and e represents the next element of the v tail element auto b = v.begin (), e = v.end (); // b, e type are the same
The iterator returned by the end member is often called an off-the-end iterator, or simply an iterator.

If the container is empty, both begin and end return iterators.

Standard container iterator operator
* iter returns a reference to the element pointed to by the iterator iter-> men dereferences iter and gets the member of the calcium element named mem, which is equivalent to (* iter) .mem ++ iter--iteriter1 == iter2iter1! = iter2
// Change the first letter of the string to uppercase string s ("some string"); if (s.begin ()! = S.end ()) {auto it = s.begin ();
    * it = toupper (* it);
} // Move the iterator from one element to another, and change the first word in the string to uppercase // Note that you use! = Instead of <for (auto it = s.begin (); it! = s.end () &&! isspace (* it); ++ it)
    * it = toupper (* it);
#### Iterator Type

The standard library with iterators uses iterator and const_iterator to represent the type of iterator:

vector <int> :: iterator it; // it can read and write elements in vector <int> string :: iterator it2; // it2 can read and write characters in string object vector <int> :: const_iterator it3; // it3 can only read elements, cannot write elements string :: const_iterator it4; // it4 can only read characters, cannot write characters
Reference: C ++ Primer Fifth Edition

Chapter 2 introduces built-in types. This chapter introduces abstract data type libraries.
-Among them, string and vector are the two most important standard library types. The former supports variable-length strings and the latter represents variable-length collections.
-Another standard library type is the iterator, which is a matching type of string and vector. It is often used to access characters in strings and elements in vectors.

related articles:

Chapter C ++: Function Return Values, GNU Compiler Commands

Chapter 2 C ++: Variables and Basic Types
Related Article

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.