Basically, in C++ you can't put any code (unless it is a definition or declaration) outside functions. All that the program does happens within one function or another.
So your problem has nothing to do with vector specifically.
Code:#include <iostream> int a = 1; //declare a global variable, OK a += 1; //nope, can't do this outside functions std::cout << a << '\n'; //nor this
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
>> Proof it works without int main:
>> http://cboard.cprogramming.com/showthread.php?t=105607
It didn't work there either:
http://cboard.cprogramming.com/showthread.php?p=775417
It's not that the compiler doesn't like you, it's just like a big ole calculator. If you push the wrong buttons it gives you the wrong answer. If you write a program that doesn't adhere to the rules of the language then it will not compile. Every program needs an entry point, programs don't just start wherever they feel like it. If you don't have an int main then it just wont run.
You can call your compiler or the C++ language names as much as you like until you're blue in the face, it doesn't care. But if you want to do programming you just have to obey the rules of the language, that's just how it is. You're free to complain and protest loudly about your code not compiling, if you must, but if your goal is to learn to program then it wont help you. What will help is having plenty of reference material handy.
Last edited by iMalc; 07-30-2008 at 12:18 AM.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
This is a basic question, but why would we want to use vectors when it seems a linked list does the same thing?
Put another way, why use linked lists when vectors do the same thing??
No, they are very different things. Here's some homework for you:
- What does it take to remove an item from the start of a vector, and how long does that take compared to a list when there are 1 million items?
- I want to lookup the value of item 999999 in the above list or vector. Which one is faster, and why?
- I have a number of pointers to the items in my list/vector and I want to remove an item. For which one of these will that cause problems?
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Vectors are random access -- you can do vec[6], and get the seventh element in the vector, in constant time. To get the seventh element of a linked list you have to go through the first six and follow the links. (And as 6 becomes 60, or 600, you can imagine what will happen.)
Inserting into the middle of a linked list is easy. Inserting into the middle of a vector is a pain.
Thanks for the info.
How difficult is it to search in a vector? Say I have a vector x = [ 1, 3, 5, 2, 5, 5] and I want to know in which indices, if any, the number 5 occurs. Would that be difficult?