POV-Ray : Newsgroups : povray.off-topic : My first C++ program : Re: My first C++ program Server Time
30 Sep 2024 13:21:18 EDT (-0400)
  Re: My first C++ program  
From: Invisible
Date: 19 Sep 2008 09:11:17
Message: <48d3a4f5$1@news.povray.org>
>> using namespace std;
> 
>   Don't do that.

So noted.

>> bool StackCheck(vector<int> stack, int min)
> 
>   You are passing the vector by value here, which may not be a good idea,
> especially if the vector is big. That's because the entire vector will be
> copied.

Right, OK.

> bool StackCheck(const std::vector<int>& stack, int min)
> 
>   Usually with big objects (such as vectors) you basically always want
> to pass by const reference rather than by value.

Ooo... I didn't realise you could do that. (Pass a const reference, that 
is.) So that means, what? If you accidentally try to change it, you'll 
get a compiler warning? Can you do that with non-reference types? (Not 
that it would matter if you were to change those - except that your 
program might not do what you intended.)

>>    int x = stack.back(); stack.pop_back();
>>    int y = stack.back(); stack.pop_back();
>>    stack.push_back(x + y);
> 
>   You could also save a bit of writing by doing it like this:
> 
>     int x = stack.back(); stack.pop_back();
>     stack.back() += x;
> 
>   Or if you want to really minimize things:
> 
>     stack.at(stack.size()-2) += stack.back();
>     stack.pop_back();
> 
>   Of course it starts being a bit obfuscated.

...ouch.

>   While it is perfectly valid to omit the return value of main()
> (IIRC it defaults to EXIT_SUCCESS), it's generally recommended to
> return EXIT_SUCCESS or EXIT_FAILURE (which are defined in some header
> file).

Noted.

Since you're here... do you happen to know the C++ name for stderr?

>> At present, I haven't figured out how to convert a string to an integer 
>> yet, so this "calculator" is pretty much useless.
> 
>   It depends on what your input syntax is. Making a parser in C++ is not
> one of the easiest things. Especially a stack calculator is difficult
> because you can't know what the type of the next input element will be
> in advance, but you have to simply read it as a string and then examine
> its contents. The next element might be a number or an operator, and you
> can't know which until you read it as a string and examine it.

I was thinking along the lines of "each line of input is either +, -, or 
a number". But I don't know how to convert a string into a number. 
Apparently the ">>" operator will do this if you hand it a number-type 
variable, so there must be some library function somewhere that does the 
conversion. I just don't know it's name yet.

>> Also, there's an interesting glitch where the program won't continue if 
>> you enter a blank line - you must enter *something* or the program won't 
>> continue. Presumably this is just the defined behaviour of the >> 
>> operator...
> 
>   If you call >> with a string, then it will read the input until it finds
> a whitespace-delimited string. >> is not line-based in any way, it's
> whitespace-based.

Well, that's consistent at least.

>> PS. Nano is a horrid, horrid text editor!
> 
>   Why don't you use something better?

Because my virtual machine is slow enough already without starting X11, 
and Nano is the only text-mode editor I have available (other than Vim 
and Emacs anyway).


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.