String, vector, stringvector

Source: Internet
Author: User

String, vector, stringvector

1. String object
1. string s;
S. size ();

// Returns the number of characters in s, which is also the length of s;

// The string object does not end with a NULL Character

// Size () returns the string: size_type, that is, the unsigned type, instead of the int type.
2. Add two string objects

The "+" operator is overloaded in the string standard library;

String s1 ("holo ");

String s2 ("world ");

String s3 = s1 + s2; // OK

Note that at least one of the left and right operands of the + operator must be of the string type:

String s4 = "hello" + "world ";//Error:No string operand

String s5 = s1 + "," + "world ";//OK

String s6 = "hello" + "," + s2 ;//Error

 

Ii. Standard Library vector type
1. Definition and initialization of a vector object

Several Methods for initializing a vector object

Vector <T> v1;

Objects whose vector type is T are saved. Default constructor, v1 is empty

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

 

Vector <int> ivec;

Vector <sales_item> Sales_vec;

 

Although memory can be pre-allocated to the vector object of the number of given elements, a more effective method is to initialize an empty vector object and then dynamically add elements. (The vector is similar to the array, but the array cannot dynamically add elements, and it is worth noting that the vector subscript operation does not add elements, which is discussed in the following 3)

 

2. The size of the vector object

The size function of the vector object returns the size_tyoe value defined by the corresponding vector class, but it is worth noting that when using the size_type type, it must be pointed out where the type is defined. The vector type always includes the element type of the vector:

Vector <int>: size_type// OK

Vector: sizet_ype// Error

3. No element is added for the subscript operation.

# Include <iostream>

# Include <vector>

Using std: cin;

Using std: cout;

Using std: endl;

Using std: vector;

Int main (){
Vector <int> ivec; // empty vector
// For (vector <int >:: size_type I = 0; I! = 10; ++ I)
// Ivec [I] = I;

/*Try to insert 10 elements into ivec, And the element values are integers 0-9 in sequence. However, the ivec here is an empty vector object, and the subscript can only obtain existing elements. */

For (vector <int >:: size_type I = 0; I! = 10; ++ I)
Ivec. push_back (I );// Add an element whose value is t at the end of push_back (t)
For (vector <int >:: size_type I = 0; I! = Ivec. size (); ++ I)
Cout <ivec [I] <"";
Cout <endl;

Return 0;
}

It is very important to use the subscript operator [] to extract only existing elements. Attempts to subscript nonexistent elements are common errors during programming and may cause program crashes.

 

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.