# Vectors

Page 1 of 1

## 6 Replies - 715 Views - Last Post: 24 October 2008 - 03:53 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=68881&amp;s=0f5cd7acff32b23d49d7d3dac6e5cd15&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 ProfBeginner

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 24-October 08

# Vectors

Posted 24 October 2008 - 02:44 AM

I just have a question: I really need help understanding vectors. The book we are using is not very good at showing how to use them. Is there anywhere I can go to help me understand them in easy layman terms?
Is This A Good Question/Topic? 0

## Replies To: Vectors

### #2 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Vectors

Posted 24 October 2008 - 02:56 AM

I assume we're talking about arrays and not vectors as in algebra.

Consider a situation where you have a bunch of people and you want to measure their heights in centimeters.

person1height = 221;
person2height = 128;
person3height = 187;
person4height = 174;

Which are variables placed pretty much anywhere in memory.
You can let the program reserve memory in sequence which allows for indexing (one of the benefits of vectors)

So instead you have:
int index = 2;
int currentHeight = personheight[index]; /(remember that index start at 0)

where you can just change the index to retrieve a value. Much simpler and allows you to extend the number of people much easier, just change the max-index-value.

So to retrieve 1000 different heights from 1000 persons you can just write

for (index = 0; index < 1000; index++) {
currentHeight = personheight[index];
//Do something with currentHeight
}

currentHeight = person1height;
//Do something with currentHeight
currentHeight = person2height;
//Do something with currentHeight
currentHeight = person3height;
//Do something with currentHeight
currentHeight = person4height;
//Do something with currentHeight
...
...
...
...
currentHeight = person998height;
//Do something with currentHeight
currentHeight = person999height;
//Do something with currentHeight

You see how this will make a ton of differense?!

This post has been edited by Gloin: 24 October 2008 - 03:04 AM

### #3 ProfBeginner

• New D.I.C Head

Reputation: 0
• Posts: 4
• Joined: 24-October 08

## Re: Vectors

Posted 24 October 2008 - 03:21 AM

We are working on Vectors now, and we are writing a program called Class GPA that will let a user input data like multiple student names and their grade Then we need to output the information the user inputs take a total of the class grades divide it by the No_of_Students to get the Class_Gpa. Then we have to list the students that are above the Class_Gpa and List the student Below the the Class_Gpa Using vector containers. I just don't understand how to write it. I can do it with Arrays that I input the information on a txt file, but they are talking about vectors and iterators.

### #4 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Vectors

Posted 24 October 2008 - 04:16 AM

Well, Vectors and arrays are the same thing.
Iterators are the same as loops (for, while)

### #5 NickDMax

• Can grep dead trees!

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Vectors

Posted 24 October 2008 - 08:57 AM

Vectors and arrays are NOT the same thing... they can be use in similar manner -- but they are VERY VERY different.

We have a tutorial on vectors here which I now see that you have looked over.

Iterators are NOT (in any way) the *same as* loops (for OR while OR even a fancy goto). Iterators are often used inside of loops to iterate over a collection, but they are NOT NOT NOT loops --- they are more like pointers than they are like loops and they really are NOT the same as a pointer (though a pointer is a kind of iterator).

An Iterator is a special object that can be used to Iterate over the contents of a container (such as a vector, or a map, or a string, or even an array). The simplest iterators are a pointer to an array, but when you talk about dynamic structures like a vector or structured data like a map or tree then a pointer just will not work.

an iterator is like a pointer:

Iterator it = some iterator;

*it -- dereferences some element in the container.
it++ -- moves to the next element in the container.

Many of the STL Algorithms use Iterators to abstract the container used. So you can use the same method to sort a vector or an array or a string...

understanding Iterators is a pretty important step in understanding STL.

This post has been edited by NickDMax: 24 October 2008 - 09:01 AM

### #6 Gloin

• Expert Schmexpert...

Reputation: 235
• Posts: 4,489
• Joined: 04-August 08

## Re: Vectors

Posted 24 October 2008 - 03:47 PM

I hear what you're saying but in the context I would believe they were talking about vectors as arrays and iterators as loops. I would agree it's not the correct terminology though.

This post has been edited by Gloin: 24 October 2008 - 03:48 PM

### #7 NickDMax

• Can grep dead trees!

Reputation: 2254
• Posts: 9,245
• Joined: 18-February 07

## Re: Vectors

Posted 24 October 2008 - 03:53 PM

yea sometimes I screw up terminology. Sometimes the words make sense in your own head but once someone else reacts to them you realize that your vocabulary was not quite right.