Re: java to c++?
In article <1191422409.816039.34940@g4g2000hsf.googlegroups.com>,
SneakyElf@gmail.com says...
i have written code in java, how would it look in c++?
int amtOfLetters = currInput.length;
HashMap letters = new HashMap(amtOfLetters);
for(int currCharPos = 0; currCharPos < amtOfLetters; currCharPos++) {
if (letters.get(currInput[currCharPos]) == null) {//doesn't exist yet
letters.put(currInput[currCharPos], 1)
} else {//if it is already there
letters.put(currInput[currCharPos],
letters.get(currInput[currCharPos]) + 1)
If you really wanted to, you could probably get that code to compile as
C++, so it'd like precisely identical. Then again, you probably don't
really want that.
The obvious version in C++ would be something like:
std::map<char, int> letters;
for (int i=0; i<currInput.length(); ++i)
++letters[currInput[i]];
OTOH, since you're apparently only using char as the index, you can
pretty easily use a vector instead:
std::vector<int> letters(UCHAR_MAX, 0);
for (int i=0; i<currInput.length(); ++i)
++letters[(unsigned char)currInput[i]];
IIRC, in Java the char type is really 16 bits, but unless you're really
short of memory, using something like 256K for the array is often
reasonable, using extra memory to improve speed. If you want to support
a really complete character set, chances are that an array won't be very
practical though.
--
Later,
Jerry.
The universe is a figment of its own imagination.