c中vector的用法(the use of vector in c)

上传人:bao****ty 文档编号:117071810 上传时间:2019-11-18 格式:DOC 页数:17 大小:41.50KB
返回 下载 相关 举报
c中vector的用法(the use of vector in c)_第1页
第1页 / 共17页
c中vector的用法(the use of vector in c)_第2页
第2页 / 共17页
c中vector的用法(the use of vector in c)_第3页
第3页 / 共17页
c中vector的用法(the use of vector in c)_第4页
第4页 / 共17页
c中vector的用法(the use of vector in c)_第5页
第5页 / 共17页
点击查看更多>>
资源描述

《c中vector的用法(the use of vector in c)》由会员分享,可在线阅读,更多相关《c中vector的用法(the use of vector in c)(17页珍藏版)》请在金锄头文库上搜索。

1、c+中vector的用法(The use of vector in c+)C+s built-in array supports the mechanism of containers, but it does not support the semantics of container abstractions. To solve this problem, we implement such a class ourselves. In standard C+, container vectors (vector) are used. The container vector is also

2、 a class template.The standard library vector type uses the required header file: #include . Vector is a class template. Is not a data type, vector is a data type. The storage space of Vector is contiguous, and list is not continuous storage.Definition and initializationVector V1; / / V1 default is

3、empty, so the following assignment is wrong v10=5;Vectorv2 (V1); or v2=v1; or vector V2 (v1.begin (), v1.end (); /v2 is a copy of V1; if v1.size () v2.size (), then the assignment v2.size () is expanded to v1.size ().Vector V3 (n, I); /v3 contains typeName type elements with n values of IVector V4 (

4、n); /v4 contains elements with n values of 0Int a4=0,1,2,3,3; vector V5 (a, a+5); the size of /v5 is 5, and V5 is initialized as the 5 value of A. The latter pointer points to the next location of the last element to be copied.Vector V6 (V5); /v6 is a copy of V5Vector identifier (maximum capacity, i

5、nitial all values);Two, value initialization1 if you do not specify an element initializer, the standard library itself provides an initialization value to initialize the values.2 if the saved formula contains elements of the class type of the constructor, the standard library is initialized with th

6、e constructor of that type.3 if the saved formula does not have the elements of the constructors class type, the standard library produces an object with an initial value that initializes the value using this object.Three, vector objects, the most important of several operations1. v.push_back (T) ad

7、ds a value to t at the end of the container, and the size of the container becomes large.In addition, list has the push_front () function, inserted in the front end, and the index in the following elements increases in turn.2. v.size () returns the number of data in the container, and size returns t

8、he value of the size_type of the corresponding vector class definition. V.resize (2*v.size) orV.resize (2*v.size, 99) doubles the capacity of V (and initializes the value of the new element to 99)3., v.empty () to determine whether the vector is empty4. vn returns the element n in the V5. v.insert (

9、pointer, number, content) inserts the contents of the number content to the point where the pointer in V is pointing.Also, V. insert (pointer, content), v.insert (pointer, a2, a4) inserts a2 into the three element of a4.6. v.pop_back () removes the last element of the container and does not return t

10、he element.7.v.erase (pointer1, pointer2) removes elements from pointer1 to pointer2 (including pointer1).After deleting an element in vector,The elements after this position need to move forward, although the current iterator position does not automatically add 1,However, since the subsequent eleme

11、nts move forward, they are equivalent to the automatic pointing of the iterator to the next location.8., v1=v2 judges whether V1 and V2 are equal.9! =, =, these operators maintain customary meaning.10., vector: iterator, p=v1.begin (); P initial value points to the first element of v1. *p takes the

12、value of the element pointing.For const vector, you can only access with vector: const_iterator pointer type.11. p=v1.end (); P points to the next position of the last element of v1.12.v.clear () removes all elements in the container. 12.v.clear () removes all elements in the container.Functional al

13、gorithms in #includeSearch algorithms: find (), search (), count (), find_if (), search_if (), count_if ()Sort by sorting: sort (), merge ()Delete algorithm: unique (), remove ()Generation and mutation: generate (), fill (), transformation (), copy ()Relational algorithms: equal (), min (), max ()So

14、rt (v1.begin (), vi.begin () +v1.size/2); sorting of elements in the first half of V1List: iterator, pMiddle, =find (cList.begin (), cList.end (),A); when found, returns the pointer at the first occurrence of the checked content; otherwise returns end ().Vector: size_type X; vector type count, which

15、 can be used as a loop, as for (int i)A beginner C+ programmer might think that vector subscript operations can add elements, but thats not true:Vector ivec; / / empty vectorFor (vector: size_type = IX = 0; IX = = 10; +ix)Ivecix = IX; / / disaster: ivec has no elementsThe above program attempts to insert 10 new elements in ivec, with elements ranging from 0 to 9 integers. However, h

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 大杂烩/其它

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号