-6

I got a code but it's C++ which I don't know. But there are many similarities between C++ and Java in my opinion. The only things I don't know what they mean / how to write them in Java are these:

u = getNextUglyNumber(twoQ, threeQ, fiveQ, &q); //and what is &q, the &?

twoQ.push(u << 1); //what is << ?

std::cout << u << ' '; //this i dont understand at all

*q = 2; // same as q = 2*q?

if (fiveQ.front() < u) {
        u = fiveQ.front(); //whats front?

Thanks a lot in advance for any kind of help!

Here is also the full code:

typedef std::queue<int> Queue;
int findNthUglyNumber(int n) {
    Queue twoQ, threeQ, fiveQ;
    twoQ.push(2);
    threeQ.push(3);
    fiveQ.push(5);
    int u, q;
    while (n) {
        u = getNextUglyNumber(twoQ, threeQ, fiveQ, &q);
        switch (q) {
        case 2:
            twoQ.push(u << 1);           /// u * 2
        case 3:
            threeQ.push(u << 2 - u);     /// u * 3
        case 5:
            fiveQ.push(u << 2 + u);      /// u * 5
        }
        n--;
        std::cout << u << ' ';
    }
    return u;
}

int getNextUglyNumber(Queue &twoQ, Queue &threeQ, Queue &fiveQ, int &q) {
    int u = twoQ.front();
    *q = 2;
    if (threeQ.front() < u) {
        u = threeQ.front();
        *q = 3;
    }
    if (fiveQ.front() < u) {
        u = fiveQ.front();
        *q = 5;
    }
    switch (*q) {
    case 2:
        twoQ.pop();
        break;
    case 3:
        threeQ.pop();
        break;
    case 5:
        fiveQ.pop();
        break;
    }
    return u;
}
eyesima
  • 215
  • 4
  • 15
  • 3
    "there are many similarities between C++ and Java" dont get mislead by some things where the two might look similar. In fact they are very different. – 463035818_is_not_an_ai May 15 '16 at 21:37
  • 6
    *But there are many similarities between C++ and Java in my opinion.* -- And opinions like that lead to C++ programs looking absolutely weird, are buggy, inefficient, leak memory all over the place, etc. – PaulMcKenzie May 15 '16 at 21:39
  • 2
    I would recommend some C++ tutorial like [this one](http://www.cplusplus.com/doc/tutorial/) (you can look up specifically the things you do not know/understand). – Turing85 May 15 '16 at 21:39
  • Alright ty for these info. But can't you please help me with these few things? I understand most part of the code actually but I don't know these keywords. – eyesima May 15 '16 at 21:46
  • 1
    You have a tutorial page and `<<` is an operator in Java as well. Just look up the keywords and the operators at the tutorial page and read about `<<` (aka. the bitshift-operator) in the oracle tutorials. Should take you 30 mins tops. – Turing85 May 15 '16 at 21:52
  • What is the C++ code supposed to do? Line-by-line translations from C++ to Java almost never work, unless the C++ code is "simple". Second, no one is going to explain pointers and dereferencing just like that. That is a whole topic reserved for C++ books. – PaulMcKenzie May 15 '16 at 21:53
  • @Paul: Is it really too hard to share some basics? I assume it's not that hard if you know about both languages C++ and Java. About the code, it is used to count hamming numbers / 5-smooth number, so 2^i * 3^j * 5^k = u, where i, j, k >= 0. – eyesima May 15 '16 at 21:56
  • 1
    [Two seemingly similar keywords, totally different usage and meaning](http://stackoverflow.com/questions/4971286/javas-final-vs-cs-const/4971323#4971323) – Flexo May 15 '16 at 22:07
  • The similarities between Java and C++ are very superficial. – Christian Hackl May 15 '16 at 22:58
  • @Hamudii The C++ code is bad enough. I suggest you try not do any translations from this code, until it is cleaned up. For example `int getNextUglyNumber(Queue &twoQ, Queue &threeQ, Queue &fiveQ, int &q)` and then this `*q = 2;` That will not compile in C++. Even then, why not get a C++ compiler, compile the code, run it under a debugger, all while you're learning C++. Otherwise you're just doing a blind translation without any confidence that what you're translating actually works. I never would translate from one language to another until I see the original code actually work. – PaulMcKenzie May 16 '16 at 00:14
  • @Hamudii It's not that people aren't helpful enough to 'share some basics'. It's that Java and C++ are fundamentally different. What looks syntactically like a small difference, requires a vast understanding of both languages' intrinsics to fully comprehend. Flexo provides a good example. –  May 16 '16 at 00:26

1 Answers1

1

The first << is the left shift operator which is the same in C++ and Java.

2 << 5 means 2 shifted left 5 times, and is equal to 32.

The second << in std::cout << u << ' ' is the C++ stream insertion operator, and is used to write variables and constants to a stream. Here, it is equivalent to the Java code:

System.out.print(u);
System.out.print(' ');

The push, front, and pop operations on the C++ std::queue add an item to the end of the queue, look at (but do not remove) the item at the front of the queue, and remove and return the item at the front of the queue, respectively. They map to operations on a Java Queue as follows:

Queue<Integer> fiveQ = new LinkedList<>();
fiveQ.add(5); // fiveQ.push(5); in C++
fiveQ.peek(); // fiveQ.front(); in C++
fiveQ.remove(); // fiveQ.pop(); in C++

The trickiest part about translating this to C++ is the &q and *q part of the code (which does not appear to be correct; I cannot compile it with g++ 5.3.0).

In C++ there are pointers, and references. You can pass around the address of a variable, and read it or modify it through that address. The formal function parameters with & are references; the expression &q is taking the address of the q variable. The expression *q is reading and modifying q via that address. You could use an AtomicInteger in Java, or write a MutableInt class:

public class MutableInt {
    private int value = 0;

    public int get() {
        return value;
    }

    public void set(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

You could then create an instance of this for q and pass it around. The functions that need to access and update its value could then do so.

Good luck.

David Conrad
  • 15,432
  • 2
  • 42
  • 54