Thread: Vectors work when they want to.

  1. #31
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

  2. #32
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #33
    Registered User
    Join Date
    May 2008
    Posts
    141
    Quote Originally Posted by anon View Post
    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
    Ah, okay.. Thanks..

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366

  5. #35
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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"

  6. #36
    Registered User
    Join Date
    Jul 2008
    Posts
    11
    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??

  7. #37
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by zodiacWarrior View Post
    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:
    1. 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?
    2. I want to lookup the value of item 999999 in the above list or vector. Which one is faster, and why?
    3. 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"

  8. #38
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zodiacWarrior View Post
    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??
    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.

  9. #39
    Registered User
    Join Date
    Jul 2008
    Posts
    11
    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?

  10. #40
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by zodiacWarrior View Post
    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?
    vectors aren't sorted, so you'd have to loop through the entire vector. That's not "difficult", it just takes a little while.

  11. #41
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In this case, a map would be better.
    There are lots of different containers that seem to do the same thing, but have different strengths and weaknesses. Your job as a programmer is to use the correct tool for the job.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Adding vectors ??
    By Hussain Hani in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2006, 03:03 AM
  4. Axis based on 2 Vectors
    By durban in forum Game Programming
    Replies: 1
    Last Post: 11-10-2005, 04:38 PM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM