2

Possible Duplicate:
What's the C++ version of Java's ArrayList

I was reading a book called "Cracking the Coding Interview" and most (all?) of the code is in Java and arrayList is used a lot. During an interview, would using a vector instead of arrayList be acceptable if the language is C++? I'm asking because I haven't seen even one example of C++ code for similar questions using a vector but I'm not sure if there's a significant difference or not.

And is there also an equivalent in C?

Community
  • 1
  • 1
user1136342
  • 4,731
  • 10
  • 30
  • 40
  • C has no vector implementation natively. Chained lists are what is easiest to implement – Eregrith Feb 04 '12 at 22:10
  • 4
    You might want to read a book on the STL before you start preparing to interview for C++ jobs. – millimoose Feb 04 '12 at 22:11
  • Also, the C standard library doesn't really implement general purpose data structures. You can get them from third-party libraries like [GLib](http://developer.gnome.org/glib/unstable/glib-data-types.html) – millimoose Feb 04 '12 at 22:13
  • 1
    @Inerdial: Better still, you might want to read a [modern C++ book](http://stackoverflow.com/questions/388242). The STL has been obsolete for more than a decade. – Mike Seymour Feb 05 '12 at 00:11

2 Answers2

4

The answer is two-fold: Firstly, you cannot compare utility classes between C++ and Java like that - different languages come with different cultures, naming conventions etc. If there was a Vector class in a C++ library, there's no connection whatsoever to any Vector class in Java, except for the name.

Secondly, the Vector class in Java is in practice deprecated, and I would discourage you from using it. In fact, forget about it :) The combination of List and ArrayList is the way to go. Use interfaces where you can, say:

List myList = new ArrayList();

Example deliberately missing generic typing.

Per
  • 636
  • 5
  • 8
3

Ignoring synchronization, the main difference between Vector and ArrayList is that Vector is a resizable array (similar to a C++ STL Vector) and ArrayList is a List that happens to be backed by an array.

arraylist-vs-vectors

In the back end, they are both arrays with functions on top to assist the programmer. Now, how different are they fundamentally?

check here : http://www.reddit.com/r/learnprogramming/comments/l6o65/arraylist_java_vs_vectors_c/

Community
  • 1
  • 1