• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Don't understand C++ Iterators and Vectors

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to simulate the following C++ STL vectors and iterator code using Java:


But I am confused at how to do this? I know that there's no pointers in Java so I can't seem to get how can I make myIter point to myVector v?

I tried the following but I have compilation errors:







Please give me some ideas how can I make myIter = v.begin() and how can I print out myIter?

I've been struggling with this so I would appreciate some ideas.

 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all you're in a Java forum. So maybe it would be a nice idea instead of posting C++ code to describe what behaviour you want to achieve with your code. And then it's always a good approach to use the API classes, like java.util.Vector.

An last but not least 'I get compiler errors' is only nice in liaison with a crystabl ball. Otherwise we need you to TellTheDetails.
 
Ana Suvari
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:First of all you're in a Java forum. So maybe it would be a nice idea instead of posting C++ code to describe what behaviour you want to achieve with your code. And then it's always a good approach to use the API classes, like java.util.Vector.

An last but not least 'I get compiler errors' is only nice in liaison with a crystabl ball. Otherwise we need you to TellTheDetails.



I did describe what behaviour I am trying to achieve: I'm trying simulate C++ Vector and Iterator classes using Java.

I don't want to simulate Java's Vector and Iterator.

My purpose is to try to simulate C++'s Vector and Iterator using Java.

I'm sorry for posting code that won't compile. it merely is to show that I tried to work on it but I still can't simulate C++ code. I don't understand how to do this with Java if Java has no pointers.

The behaviour is C++ Iterators and Vectors. That should be a Java version of the first code I posted at the top.

 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Suvari wrote:The behaviour is C++ Iterators and Vectors. That should be a Java version of the first code I posted at the top.


And what exactly does the first (C++) code do? I haven't coded C++ since about seven or eight years, so please enlighten me...
 
Ana Suvari
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:

Ana Suvari wrote:The behaviour is C++ Iterators and Vectors. That should be a Java version of the first code I posted at the top.


And what exactly does the first (C++) code do? I haven't coded C++ since about seven or eight years, so please enlighten me...



I haven't taken C++ for a while but that does not stop the Java Prof for making this assignment. So, I have to assume that Java's Vector and Iterators are made different from C++ or the Java Prof would not be making us do this.

Here's the C++ code that it should simulate close to.




v.begin() should return an integer index to the first element. But I don't get how does myIter equal to v.begin() if it's an integer? The prof made it sound like it should be easy peasy but I guess I still don't have any ideas.

The myIter class should be able to iterate through each element in myVector and display each of them.

I am not seeing what the Prof wants me to do that she thinks is so easy.
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The C++ code sample doesn't compile for me. You cannot assign v.begin() to an int variable in the second loop. In my opinion the second for loop should be equal to the first one and the example probably just shows how to output a vector, sort it and output it sorted again. In Java that wouldn't be so different at all:

 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you saying that your Java instructor is asking you to mimic C++
syntax in Java, or is the assignment to mimick the C++ behavior in
Java. I ask because the first seems a bit odd while the second can
yield quite a few ah-hah moments as to how the languages compare
and differ. Just askin'...

Jimn ... ...
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ana Suvari wrote:Here's the C++ code that it should simulate close to.




v.begin() should return an integer index to the first element. But I don't get how does myIter equal to v.begin() if it's an integer? The prof made it sound like it should be easy peasy but I guess I still don't have any ideas.

The myIter class should be able to iterate through each element in myVector and display each of them.

I am not seeing what the Prof wants me to do that she thinks is so easy.



Look inside the loop how you get a single element from the vector, then you know what type myIter really is. Then remember that in c / c++ pointers simply ARE integers which you can add to, subtract from etc...

Finally, look at the API for the java Vector or ArrayList classes. You will see that you can access the elements in two ways, either by using an Iterator, or by accessing them with an integer index. That should be enough to allow you to simulate the two loops in the c++ code
 
Peter Taucher
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so the different possibilities of iterating through a list are looked for (like the enhanced foor loop, using Iterator directly or index-based foor loop and get() method). That shouldn't be so difficult.
 
Listen. That's my theme music. That's how I know I'm a super hero. That, and this tiny ad told me:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic