Vector usage in Cocos2d-x

Source: Internet
Author: User

1. Create a vector object

  • Vector (). Default constructor.

  • Vector (ssize_t capacity ). Create a vector object and set the capacity.

  • Vector (const vector <t> & other ). Create another vector object with an existing vector object, where & other is the reference parameter of the Left value.

  • Vector (vector <t> & other ). Create another vector object with an existing vector object, where & other is the right value reference parameter.

 

2. Add Elements

Adding elements to a vector object must be of the ref object pointer type. The following is a summary of common functions:

  • Void pushback (t object ). Add an element. t indicates the ref object pointer type.

  • Void pushback (const vector <t> & other ). Add all elements in a vector object to the current vector object.

  • Void insert (ssize_t index, t object ). Insert an element at the specified position. ssize_t is an alias of the int type.

 

3. Remove Elements

The following is a summary of common functions for removing elements from a vector <t> container:

  • Void popback (). Remove the last element.

  • Void eraseobject (t object, bool removeall = false ). Removes an element.

  • Iterator erase (iterator position ). The object is removed at the specified position. The parameter is the iterator, And the return value is the next iterator.

  • Iterator erase (iterator first, iterator last ). Specifies the range of objects to be removed (first ~ Last), the parameter is the iterator, And the return value is the next iterator.

  • Iterator erase (ssize_t index ). Removes the element of a specified index. The parameter is ssize_t, And the return value is the next iterator.

  • Void clear (). Remove all elements.

 

4. Replacing and exchanging Elements

We can also use the following function to replace and exchange elements in a vector container:

  • Void swap (T object1, t object2 ). Exchange two elements.

  • Void swap (ssize_t index1, ssize_t index2 ). Swap two specified position elements.

  • Void Replace (ssize_t index, t object ). Replaces the specified position element with an object.

 

5. Search operations

We sometimes need to operate the elements in the vector. Below is a summary of common search functions:

  • Iterator find (t object ). Search for objects in the vector container and return value iterator.

  • T At (ssize_t index ). Returns the elements in the vector container Based on the index position.

  • T front (). Returns the first element.

  • T back (). Returns the last element.

  • T getrandomobject (). Returns a random element.

  • Bool contains (t object ). Returns whether an element exists in the container.

  • Ssize_t getindex (t object ). Returns the location of the specified object.

 

6. Other operation functions

In addition, there are many vector object functions. Below is a summary of common functions:

  • Ssize_t size (). Returns the number of elements.

  • Ssize_t capacity (). Returns the vector capacity.

Vector usage in Cocos2d-x

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.