Question regarding the language

I just want to know , when you code , do you still look up some phrases or do you do it all out of your head?

for example , if you were to write a program that does a specific thing, you plan it and write it afterwards, do you just keep on writing or do you lookup codes if you aren't sure, or keep retrying till it works?

Does anyone have maybe a link or something I can follow that would help me remember codes to not look it up a lot? Im fairly new so I still google a few sets of codes here and there to help me in my understanding.


also below why do people write
 
  std::Insert here

instead of "using namespace std;"

is it just for visibility? or just more smoother to write?
The more you use something, the more you'll memorize it.

Think of it as learning a spoken language. You will probably need to look up words or whole phrases all the time, but as you use it more and more, you'll simply remember and memorize things. With programming, it's a lot easier and faster to memorize and learn than learning a new spoken language.

However, no matter how good someone gets, learning a new language means googling syntax and such.


Learncpp.com -> best learning tool I can show anyone for intro to programming and C++.


also below why do people write

std::Insert here

instead of "using namespace std;"

You shouldn't use "using namespace std", it's bad practice. It basically says, "I may be using things from the std namespace without saying so"

However, this means that you can have naming conflicts. Imagine you create a function with a specific name while there exists a similar function with the same name in the std:: namespace. Which function gets called? If the wrong function was being called, would you realize it?


I've gotten shot in the foot from "using namespace std" plenty of times before. Usually, it isn't a problem for simple programs, but I've had friends doing simple coding assignments for their professors who ran into issues. They can't figure out what's wrong, and simply removing "using namespace std" fixes their code.


I once was running a quick program I made which wouldn't work. After several minutes of confusion, I simply removed "using namespace std" and it worked - like magic. A function I thought was using was never called - instead its std:: counterpart was being called and ruined everything.

There are vast things in the std:: namespace, and you can't avoid name conflicts for long.


EDIT: You can read here about using namespace std:

https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Last edited on
I routinely look stuff up in most languages. I can do a LOT in c++ without any help, probably anything I wanted to do, but if I do not check occasionally I will end up reinventing something that was not in the language in the 98 standard that I have forgotten or never knew was there. There are a lot of little oddball things in c++ now that you would not expect to be there, but are -- an oft noted example on this site is the GCD algorithm. Yes, its only 5 lines or so, but its in the language so remaking it serves no purpose.

using namespace std was highly recommended for a while and then it started causing trouble so the community did a 180 on that idea.

instead, though, you can make smaller using statements that pull in what you need, to avoid spreading the std all over your code. Up to you.
an example: using std::cout;
you can make a little header of the most common ones and include that, just make sure not keep it small, avoid the temptation to add everything you use to it and only the constantly repeating ones. One of the things the c++ committee needs to do IMHO is split std:: up into smaller namespaces (std:: can include them all) eg per header file name. I think this is done in part, eg I think using namespace vector works, but not everything, I don't think using namespace cmath does. I have not experimented with everything though, and maybe I missed some stuff there too.

if you want to express an idea and cannot, look it up right away. That is just syntax or libraries, and it will come in time. You will never guess it.
Last edited on
do you still look up some phrases or do you do it all out of your head?

IMO the tough part is knowing what to say. Knowing how to say it is just a memory exercise. Google helps.

By now I've used std::vector's fill constructor hundreds or thousands of times but it's still something I have to look up every time.
e.g., is it std::vector xs(x, 10) or std::vector xs(10, x)?

Last edited on
It is often helpful to remember that optional args are on the right. The fill value is optional (defaults to default initialiser) so to create a vector of 10 elements each with value x then its std::vector xs(10, x);

If fill is not optional (eg for std::string), it still takes the same form.

Topic archived. No new replies allowed.