I know C++, but I don't always know how to use it.

Pages: 12
I have been learning C++ for a little over a year now, and I got a pretty good understanding of some of the fundamental stuff. I can look at code and understand it, but if I have to write something myself from scratch, I don't know how to go about it. I don't know if I should use arrays or vectors, if I should use while or for loops (sometimes, not so often) and so on.
I don't know where I should start in my program either, should I start by making a function or a class? What exactly should I do to make my program do what it's supposed to do, the best possible way.
I think this happened because I been going in and out of the learning process tons of times, tried multiple books and video material. Now my understanding of the language is just screwed up, and I don't know what I should do. There are so many choices regarding code style, that I don't know which to choose from, and my code ends up being an ugly scrabmled mess.
Some advice would be appreciated, thank you.
It's a matter of design. You first need to design your program. There are a number of interacting object (in object oriented programming) or abstract data types (in procedural programming).

Once you have a design, programming is simply a matter of implementing that design.

No well written programs are stared without some guiding design behind it.
What exactly should I do to make my program do what it's supposed to do, the best possible way.
This makes me think of a joke I read somewhere
Two rules about optimization:
1 - don't do it
2 - (for experts only) don't do it yet

About the code style, there's isn't really a "best" one. The advice here is usually to choose one you like and stick to it to the end. What's important is that if you read it you don't get confused. If you're lucky others won't get confused too
Last edited on
What I do is, get pencil and printing paper. Write down in words what I want to do. write int main(){return EXIT_SUCCESS;}, write the 2 major libraries I use, cstdlib and cstdio. write my first variable and that's all it takes.

I don't know if I should use arrays or vectors

When faced with this choice, go with arrays then decide later if vectors is better for you

I don't know where I should start in my program

start with main()

I understand what you mean by not knowing where to begin. I was like that when I first started programming. But the best way to help yourself is to continue coding and soon enough, you will have your very own unique style. Try projecteuler.net to keep yourself busy with coding or try codeacademy.com to learn in an interactive way.

p.s. my name on codeacademy is ArcSoft
Last edited on
When faced with this choice, go with arrays then decide later if vectors is better for you

I think this should be the opposite. Use vectors first as they are far easier to manage and deal with. Only use arrays if you really need them.
I have never used vectors so that is why I said choose arrays. But if you say they are easier o.o I will definitely look into those
Last edited on
I have never used vectors so that is why I said choose arrays

Good textbooks don't even bring up arrays until long after explaining vectors.
That's interesting, because I have never used vectors and I am in second year CS. In fact the first thing we learned in school was arrays and we even did a vector simulation using arrays but not vectors. Any recommended reads for vectors?
Any recommended reads for vectors?

Any good C++ textbook: C++ Primer, Accelerated C++, Programming: Principles and Practice.
Last edited on
From first principles, arrays are easier to understand and work with. The problem is that you can only do basic stuff with them before they get very complicated. As soon as you need to start dynamically re-sizing the array, learn how to use vectors. They are more complicated for only the most basic things but then much easier for everything else.

I don't think asking a beginner to start with vectors when they can barely define variables is a good way to go about it. It was like when I was starting and people were saying "Get to know your compiler". I had no idea what was compiler-vs-language specific at that time and it just wasn't feasible for me to read the difference between the compilers and understand it after writing "Hello world"
Last edited on
It is a common problem of any programmer who is learning a new language irrespective of how experienced he is.
The only way to overcome this situation is more practice that is more programming using the new language. Also it is a good practiice to work in team and perform code reviewing of others.

Good textbooks don't even bring up arrays until long after explaining vectors.

I can not agrre with this categorical statement. Sometimes it is difficult to understand vectors and other STL stuff without having solid knowledge of pointers.
But I can agree that there is a problem of how much time we spend learning that or other language construction. Usually we start from simple constructions and use then during all learning course. So for example we use the for statement in fact every day but more serious constructions that we will use in practice we learn only ar the very end of the course. So we can use loops but we have some difficulties to use for example vectors. So if we will start learning C++ language from vectors it is possible that we will have some gaps in understanding pointers.:)
So my idea is following. To show examples of using vectors starting from the very beginning of the course without any deep explanation of them. But the explanation of pointers shall precede the explanation of vectors.

Last edited on
How are pointers even relevant?
They are relevant that to understand the mechanism that is used behind vectors. A programmer should understand why he has to use vectors instead of allocating memory in the heap. That is he should see the preference of using vectors. The same is valid for std::string. He should know that std::string in fact is a wrapper over a raw character array, that std::string::data() and std::string::c_str() are the same and that he can use even s[0] for an empty std::string of course without assigning any value.
I'm largely self taught myself and I learned most of what i know about c++ through sites like this and microsoft. Coming from a position that I see you are in that i remember i was in, don't over think things. Experimentation is good if you are trying to learn a language, thats how I learned everything from pointers and arrays to template structures and functions.

Thats pretty much the only way you will TRUELY learn because you supplement the theory that you read about; through experimentation is validation. If you are working on a large project, have no design, and don't know how to code using proper syntax and semantics then you might as well quit until you read more theory until you can do what you intend. Knowledge only gets deeper and deeper.

As for an answer to your question arrays are generally used when you know that the size of your set will not change throughout the course of an application.

for example:
int sammy [/*insert fixed length here*/]  ={0,0,0,0};//up to sammy sub n-1 

Vectors conversely are dynamically sized you use them when you will be adding and resizing the data set you are working with frequently.

You wouldn't use an array if you need to add names to a running database because it would overflow (there is no space for new members that exceed the fixed length of the array). You would use either a list or vector for that.
A list is generally used if you don't care about random access, the ability to supply an index into the array and retrieve the data. A list is used in general if the number of items is relatively unknown or mutable and you want to be able to insert or remove elements, it is sequentially accessed.

Hope I helped.
Last edited on
A programmer, yes, should know the semantics of string's member functions to that level and even deeper. But not a beginner student. You don't start driving school by learning how to bleed brake lines.

(I don't understand that "instead of", by the way, but either way, this is a lounge topic)
Pointers are relavent because all dynamically sized structures use new*(size)/t*malloc(size). An array is a fixed set of pointers, a list is a pointer to a list of pointers. Vectors is an array a dynamically sized set of pointers. The integer index you use to access an element in the array can be interpreted as a pointer (to physical memory via virtual memory). Pointers are the shit, they cause people headaches and they allow for more ways to abuse or solve a problem.

source(s): Wikipedia, Computer Architecture, Experience
Last edited on
As for me if I do not know internals of a class or a function I fill myself irresolutely. For example when somebody is studing function strlen it is always interesting to know how it works. And after you will write the function yourself you get confidence in its using (sorry for my bad English), The most annoying thing is when you do not have any idea how something is working. For example at present I am learning C# and I do not like that the material is presented for "beginners". After reading some chapter of a book on C# I have filling that I was deceived. I was shown some simple things but I was not said how they work.:)
Last edited on
@vlad from moscow yes, when you know in general how something works you can make effective use of it. but the important thing imo is to not get bogged down in all of the little things. For instance copying a string is simple there are only 2 ways of copying a string back to front and front to back using an array. Its not that big of a deal, but i wholehearted agree that its frustrated when books or tutorials show you how to do stuff but don't tell you in general how you could apply logic to achieve the same result yourself.
vlad wrote:
As for me if I do not know internals of a class or a function I fill myself irresolutely.

I think you're confusing knowing the internals with knowing the requirements. Assuming you don't feel that way about C++, the internals of which vendor's std::vector do you know?

Let's take LLVM libc++ as an example, since it's not bloated by ancient compatibility issues: http://llvm.org/svn/llvm-project/libcxx/trunk/include/vector . Can you tell the purpose of each class in that hierarchy? Can you explain how push_back works?

std::cin or even std::malloc can be more complicated. Nobody should have to learn how to build their own vector, input stream, or allocation function before learning how to use them.
Last edited on
I ran into this same problem awhile back.
In class, we were always given very short programs to make.
However, when I tried to make a larger program, I had a good idea where to start, but it would end up as a mess later.

But, I figured out what the problem was.
When I was given an assignment to make a program, I followed the paragraph that explained what I needed to do, it served as an outline for me.
So, I decided one day to write an outline or create some form of organizational tool for a larger project and I was actually able to finish it.

For me, the whole problem was just staying organized, and knowing what to do next.

As for your code being an ugly mess, I would say just to keep the indenting consistent and correct, and use descriptive variable names, it helps a lot when I start to get confused.

As for the multiple choices for coding style, the best choice is to do something that you can easily read because, after all, it is your project and you are the one who has to work on it.
Pages: 12