C ++ simplify04-standard library string and vector

Source: Internet
Author: User

1. Standard Library string

1-1: Declaration: # include <string> usingstd: string; // or using namespace STD;

1-2: Common constructors of string objects

String S1; // default constructor. S1 is an empty string.

String S2 (S1); // initialize S2 as a copy of S1

String S3 ("value"); // initialize S3 as a copy of the string literal value

String S4 (n, 'C'); // initializes S4 into N copies of the character 'C'

1-3: String object read/write CIN >>str; cout <STR;

Read and ignore all leading blank characters (such as spaces, line breaks, and tabs)

Read the character until it encounters a blank character again. The read ends.

Read an unknown number of strings

String STR;

While (CIN> Str)

Cout <STR <Endl;

Stop loop when invalid characters are entered (enter Ctrl + Z)

1-4: Getline reads the entire line of text, does not include line breaks, and does not ignore line breaks starting with airline.

Getline parameters: istream input stream object and string object

1-5: String Common Operations

Str. Empty (); If STR is an empty string, true is returned; otherwise, false bool is returned.

Str. Size (); returns the number of characters in S size_type

STR [N]; returns the characters whose position is N in Str. The position starts from 0 and the char type is counted.

Str1 + str2; string connection, returns the new string type. (+ Either the left or right must have at least one string, not all of them are literal values)

Str1 + = str2; append str2 to the end of str1

Str1 = str2; string assignment

Str1 = str2; true is returned if the operation is equal; otherwise, false bool is returned.

! =, <, <=,>,> = Always have meaning

1-6: Supplement string: size_type

In fact, size () returns a value of the string: size_type type. Size_type is the unsigned type.

The string length stored by size_type is twice that of the int type. To store large text strings and avoid overflow, the safest way to save the string object size is to use the string: size_type type, for example:

String STR;

Cin> STR;

String: size_type ST = Str. Size ();

1-7: String size comparison: (same as the dictionary sorting policy, the top of the dictionary is smaller than the back of the dictionary)

If the length of a string is different, and the short string object matches the previous part of the long string object, the short string object is a string object that is light rain long;

If the two string objects have different characters, the first unmatched character is compared.

String str1 = "hello"; string str2 = "Hello World"; string str3 = "hiya ";

Then str1 <str2; str3> str1; str3> str2;

1-8: Assignment nature: String STR = "hello"; sting str2 = "world"; str1 = str2;

First, release the memory occupied by str1, allocate enough memory space for str1 to store str2 copies, and finally copy all characters in str2 to the new memory space.

1-9: single char Character Processing

2. Standard Library Vector

2-1: vector is a set of objects of the same type. Each object has a corresponding integer index value. Like a String object, the standard library manages memory related to storage elements. We make vector a container and can contain other objects of the same type ). Mandatory declaration:

# Include <vector>

Using STD: vector;

2-2: vector is a class template.

2-3: vector object definition and initialization:

Vector <t> V1; the vector stores objects of the T type. The default constructor V1 is null.

Vector <t> V2 (V1); V2 is a copy of V1.

Vector <t> V3 (n, I); V3 contains n elements whose values are I.

Vector <t> V4 (n); V4 contains N copies of elements whose values are initialized.

2-4: The vector supports dynamic growth. Push_back (); Function

2-5: Common Operations of vector:

V. Empty (); If V is null, true is returned; otherwise, false is returned.

V. sizse (); returns the size_type of the number in the vector.

V. push_back (t); add an element whose value is T.

V [N];
Returns the element whose position is N in v.

V1 = V2;
Assignment

V1 = V2; judgment and so on

! =, <,>, <=,> = Comparison

3. iterator. The iterator is a checkContainer(Must be a container class, such as a vector) element and traverse the Data Type of the element.

3-1: Each container type defines its own iterator type, such as vector:

Vector <int>: iterator ITER; // vector iterator Definition Statement of int type

3-2: begin and end operations.

Iverc. Begin (); returns the first element vector pointed to by the iterator <int>: iterator iter = iverc. Begin ();

Iverc. End (); exceeds the end iterator. The iterator returned by the end operation points to the "next to the end" of the vector to act as the Sentinel.

3-3: auto-increment and reference calculation (*)

* ITER returns the element currently pointed to by the iterator. (* Called the unreferenced operator)

ITER ++; The iterator moves forward to the next element in the container.

3-4: const_iterator this type can only be used to read elements in the container and cannot change its value.

Note: There is an underline in the middle, not a constiterator.

The iterator object declared by const must be initialized at the time of declaration. Once initialized, it cannot be changed, and auto-increment and other operations cannot be implemented.

The const_iterator object can use constvector or non-const vector, because the element value cannot be rewritten. Const iterators of this type are almost useless: Once initialized, they can only be used to rewrite the elements that point to the element, but cannot point to any other element.

 

Three questions per day)

(1) read a group of Integers to the vector object and calculate and output the sum of adjacent elements of each pair. If the number of read elements is odd, the system prompts that the last element has no sum and outputs its value. Then modify the program: pairing the first and end elements (first and last, second and last, and so on), calculate the sum of each pair of elements, and output them.

(2) read a piece of text into a vector object. Each word is stored as an element in the vector. Converts each word in a vector object to an uppercase letter. Output the converted elements in a vector object. Every eight words are output in one row.

(3) write a program to create a vector object with 10 elements. Use the iterator to change each element value to twice the current value.

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.