Cocos2dx 3.0 transition article () ValueVector and Vector have to tell the story, cocos2dxvector

Source: Internet
Author: User

Cocos2dx 3.0 transition article () ValueVector and Vector have to tell the story, cocos2dxvector

Voting address of this Article: Http://vote.blog.csdn.net/Article/Details? Articleid = 37834689

The day before yesterday, I saw a very tangled option: one day when you met an alien, the aliens warmly invited you to play on their planet. How do you choose

1: Go, but you may never be back.
2: No, but aliens will go away from your memory.

This is a very exciting question ?! For a seemingly simple question, different answers hide different meanings.
------------------
The fish and the bear's paw cannot have both sides. Similar examples of this will often happen in life. Similarly, if you have learned Cocos2dx3.0, you may encounter such A Tangle:
That is: Value and Vector (Map ).
Why? Let me hear about it.
-----------------

In Cocos2dx 2.0,
Where should we store an int type data? That's right. Put it in CCArray, As follows:
Int I = 10; CCArray _ array = CCArray: create (); // create a CCArray array _ array-> addObject (CCInteger: create (I )); // put int type data into the Array
What is the purpose of storing a CCObject object? Yes. CCArray:
CCSprite * sp = CCSprite: create ("star.png"); // create an Genie... _ array-> addObject (sp); // Add the genie to the array

In Cocos2dx3.0,
We all know that CCArray has been dumped.(In fact, you can use _ Array instead). How should I store a Ref (after 3.0, CCObject is renamed as Ref) object? I thought of it right away. CCArray substitution: VectorThe sample code is as follows:
Auto sp = Sprite: create ("star.png ");... vector <Sprite *> sp_vec; // create a Sprite * type container sp_vec.pushBack (sp); // Add the Sprite to the container
If you are not very familiar with the operation of Vector, you can read the previous blog: http://blog.csdn.net/star530/article/details/19170853

Next, the question arises. If we want to store a data type, such as int type data, can we use Vector? The answer is no. In the official instructions of Vector, there is such a sentence:

T in cocos2d: Vector <T> must be a pointer to a cocos2d: Ref subclass object. It cannot be another data type or native type, because we have integrated the Cocos2d-x's memory management model into cocos2d: Vector <T>.

There is a saying: God closes a door for you and will surely open a window for you... no Ye is left here...Since Vector cannot accommodate any element of the data type, there must be something that can replace it. Yes, ValueVectorOn the stage of history.
When I saw ValueVector for the first time, I got stuck. What kind of cake is this? A few seconds later I reflected that this product must be the illegitimate child of Value and Vector. I was so witty that such an abstract name would allow me to quickly think of the answer, so I could not help but feel proud of it.
First go to The CCValue. h header file and check its declaration:
typedef std::vector<Value> ValueVector;
As you can see, ValueVector is actually a std: vector container that stores values.Here is a little different from my previous guesses. The following describes how to store several int-type data in ValueVector.
int a = 10;int b = 20;ValueVector val_vec;val_vec.push_back(Value(a));val_vec.push_back(Value(b));

The code above is to create two int-type variables and then put them into the ValueVector,Note: Because ValueVector can only store Value-type elements, int-type a and B variables must be converted to Value type before they can be put into ValueVector.

For some usage of Value, you can see the previous blog: http://blog.csdn.net/star530/article/details/21651751

Bytes ------------------------------------------------------------------------------------------------------------

When it comes to ValueVector, let's take some simple operations:
1. Read the Plist (xml) configuration file.
. As follows:
ValueVector star_val = FileUtils::getInstance()->getValueVectorFromFile("star.plist");
However The plist file read by ValueVector is limited to the plist file whose format starts with an array.For example:
<array><dict><key>name</key><string>star</string><key>isCool</key><string>yes</string></dict></array>
If a file starts with the dict dictionary type, use ValueMap instead. This is the next article. Skip.

2. Insert an element into the ValueVector.As mentioned above, ValueVector is actually a vector ordered container storing the Value type, so its You can directly use the vector ordered container to insert elements.. Example:
Int a = 10; std: string B = "star is so cool"; ValueVector star_val; star_val.push_back (Value (a); star_val.push_back (Value (B )); // convert the type to the Value type before adding it to the ValueVector.

3. Extract the elements in ValueVector.The following example is used:
int a1 = star_val.at(0).asInt();std::string b1 = star_val.at(1).asString();CCLOG("a1 = %d ,b1 = %s",a1,b1);
The above code is easy to understand, that is Extract the elements in star_val placed at the positions 0 and 1 and convert them to the int and string types respectively. AsInt () and asString () are functions used to implement type conversion.

4. Delete the element in ValueVector.
There are three common ways to delete elements in containers:
1) Delete the last element in the container,
Star_val.pop_back (); // directly Delete the last element in the container
2) use erase to delete an element in the container?Why should I add a question mark before it? Assume that I want to delete element a in star_val. The Code is as follows:
auto star_iter = std::find(star_val.begin(),star_val.end(),a);star_val.erase(star_iter);
The above two lines of code still have a large amount of information. First, we need to know that erase deletes a single element directed by an iterator, rather than directly like this:
Star_val.erase ();

This operation is incorrect,What is an iterator?Let me give you a small example:


Assume that a classroom is a vector container. Every student is an element in the vector, and the seat number corresponding to the student is the iterator. Suppose there is a new teacher erase. When she was in class, you turned to the girl next to her, and the teacher was furious. She planned to let you get out of the classroom immediately, but she doesn't know your name at all, so she can only do this:

"The 3rd-column and 5th-row child shoes that make small moves... Don't point to it. It's just you. Do you want to get out of here"


Let's forget this hot girl. Now that the parameters in erase must be an iterator, what is the iterator corresponding to element? Here we need to use the find algorithm, which will return the iterator of a in the container. (For specific usage of std: find, click here: Click me !!!)
Finally, I Have To shyly tell you that this method of deleting elements cannot be compiled successfully !!! The = operator is not overloaded in the Value, while the data type in std: find must implement the = Operator. Therefore, you cannot use the search method and delete it.
Someone must have shot the table here: Nima, this method cannot be deleted. Why are you writing so much? If my pants are all taken off, will you show me this? &...... % *.
My answer is: Haha... do you know how long I used this erase? That's right! I am now at the pace of revenge! I'm not happy with you ~!

3) Put away the angry watermelon knife in your hand and take a deep breath.
Since the specified element cannot be deleted, can I break the jar and delete all the elements? The answer is yes:
star_val.clear();
Delete all elements with clear to solve your physiological problems from the root cause ~

Bytes -----------------------------------------------------------------------------------------------------------------------

Well, the usage of ValueVector is mentioned here, and the following is a summary and supplement:
1. A Vector can only be used to store elements of the Ref type. It cannot store elements of the data type;
2. ValueVector can only be used to store elements of the Value type, because Value is actually a data type, so it can be considered that ValueVector can only be used to store data types. Do not put elements of the Ref type, otherwise, it will be very exciting.
3. ValueVector can be placed in the ValueVector, provided that the ValueVector is converted to the Value type, and the element of the Vector type cannot be stored in the Vector, as shown below:

ValueVector star_val; ValueVector star_val2; star_val.push_back (Value (star_val2); // The correct Vector <Ref *> star_vec; // error!

OK. Write it here.

Bytes ----------------------------------------------------------------------------------------------------------------------

Respect Original, reprinted please indicate Source: http://blog.csdn.net/star530/article/details/37834689

This article has participated in the CSDN blog competition. If you think this article is helpful to you, please vote for me! I will always be grateful to you! Voting address:

Http://vote.blog.csdn.net/Article/Details? Articleid = 37834689 (Drop down the bottom of the page to vote)




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.