Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a C++ vector relate to in Objective-C?

I'm moving from Objective-C to C++ and am not sure what vectors are. I've read documentation about them, I'm not quite clear though. How would you explain C++ vectors using Objective-C analogies?

like image 309
Marty Avatar asked Jan 21 '12 22:01

Marty


3 Answers

They're pretty similar to NSMutableArrays but vector is a template class and so can be instanciated for any (standard-template-library compatible) type. NSArrays always hold NSObjects.

That is, assuming you mean std::vector.

like image 138
smparkes Avatar answered Oct 14 '22 23:10

smparkes


They're like NSMutableArrays but can hold any data type - pointer or not. However, each vector can only ever hold one type at a time. Also as it's C++ there are fewer functions e.g. no plist loading/saving.

like image 26
jrtc27 Avatar answered Oct 14 '22 22:10

jrtc27


A C++ vector (presumably you mean something like std::vector) is basically an NSArray, except it holds any type you want (which is the template parameter, e.g. a std::vector<int> holds ints). Of course, it doesn't do memory management (which NSArray does), because arbitrary types aren't retain-counted. So for example a std::vector<id> would be rather inappropriate (assuming Obj-C++).

like image 42
Lily Ballard Avatar answered Oct 14 '22 23:10

Lily Ballard