What does the C ++ vector mean in Objective-C?

I am moving from Objective-C to C ++ and not sure what vectors are. I read the documentation about them, but I do not quite understand. How would you explain C ++ vectors using Objective-C analogues?

+6
source share
6 answers

They are very similar to NSMutableArrays , but vector is a template class and therefore can be used for any type (compatible with the standard template). NSArrays always hold NSObjects .

That is, if you mean std::vector .

+6
source

They are similar to NSMutableArrays, but can contain any data type - pointer or not. However, each vector can have only one type at a time. Also, as in C ++, there are fewer functions, for example. no load / saving plist.

+1
source

A C ++ is a vector (presumably you mean something like std::vector ) basically NSArray , except that it contains whatever type you want (which is a template parameter, for example, std::vector<int> contains int s). Of course, it does not perform memory management (which is what NSArray does), because arbitrary types are not counted. So, for example, std::vector<id> would be rather inappropriate (assuming Obj-C ++).

+1
source

NSArray is a wrapper around CFArray. CFArray can store any type of data.

I don't know much about C ++, but it looks like CFArray can do everything for what you want to use a vector? When you create a CFArray, you give it a CFArrayCallBacks pointer that contains any logic specific to the data type being stored.

Of course, you can always just rename your Obj-C file to * .mm and mix C ++ into your objective-c.

http://developer.apple.com/library/mac/#documentation/CoreFOundation/Reference/CFArrayRef/Reference/reference.html

+1
source

In C ++, an array is basically just a pointer to a continuous block of data - a series of elements. It does not offer built-in methods or higher functionality.

 int intArr[] = {0,1,2,3}; 

coincides with

 int *intArr = (int *)malloc(4*sizeof(int)); for(int i = 0; i < 4; i++) { intArr[i] = i; } 

On the other hand, a vector (std :: vector) is a container for elements (mainly as an array), which also offers additional built-in methods (see http://www.cplusplus.com/reference/stl/vector/ ) eg

 vector<int> intArr; for(int i = 0; i < 4; i++) { intArr.push_back(i); } // this yields the same content; ie intArr = {0,1,2,3} 

Both arrays and vectors can be used for any type of object: int, double, MySpacePirateWizardClass, etc. The big bonus of vectors is additional functionality from built-in functions, such as:

 int arrSize = intArr.size(); // vector also includes useful information int *firstElement = intArr.begin(); // methods for creating pointers to elements intArr.delete(0); // modify elements intArr.insert(0, 2); // modify vector // now: intArr = {2,1,2,3} 

etc.

When I know that I will not miss space (or look at a huge amount of data), I always use vectors because they are so convenient (even just the size () method is sufficient reason).

+1
source

Think of vectors as extended arrays.

If you are new to C ++, this page will become your best friend: http://www.cplusplus.com/reference/stl/vector/

0
source

Source: https://habr.com/ru/post/906567/


All Articles