Using Vectors

This is a discussion on Using Vectors within the C++ Programming forums, part of the General Programming Boards category; I am currently working on vectors and I understand them but I would understand them better if I knew where ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    3

    Using Vectors

    I am currently working on vectors and I understand them but I would understand them better if I knew where in a program they would be used.

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Anywhere you'd normally use an array.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Especially if you don't know how much space you'll need.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    76
    and also if you don't know the size of a data container...

    example
    Code:
    #include <iostream>
    #include <vector>     // a must for vector use
    using namespace std;
    
    int main()
    {
         vector<int> iVector;   // you have to specify the vector type by a class or a structure
         int answer;
         int temp;
    
         cout<<"How many numbers do you want to enter? ";
         cin>>answer;
    
         for(int i=0; i < answer; i++)
        {
              answer--;
              cout<<answer<<" Enter a number: ";     // display how many numbers the user has left to enter
              cin>>temp;
              iVector.push_back(temp);    
    // this is magic... the temporary integer is pushed onto the back of the vector...and given a location in that vector, which you can access using iVector[anumber]
         }
    return0;
    }
    i prefer using vectors over arrays any day...
    Only thing I hate doin, though, is that to access an object of a vector class (ex. iVector.erase()) you have to create a vector iterator that points to a location in the vector

    ex.
    Code:
    class aClass
    {
         public:
              int number;
    }
    
    aClass theClass;
    vector<aClass> theClassV;
    vector<aClass>::iterator theClassVIterator;
    
    for(int i=0;i<=3;i++)
    {
         theClass[i].number=i;
         theClassV.push_back(theClass);
    }
    
    //                                                          0             1               2
    // so now theClassV vector holds {theClass, theClass, theClass}
    // at the storage spaces 0, 1, 2
    // If i wanted to delete what was at location 1 I would have to do this...
    
    for(i=0;i<=2;i++)
    {
         theClassVIterator++;
    }
    
    theClassV.erase(theClassVIterator);
    this is the only way ive found how to make the iterator the position of the vector you want...there probably is a pro somewhere around here who can be sure...
    Last edited by gL_nEwB; 05-17-2006 at 03:25 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,618
    Um, gL_nEwB, despite the name, theClassVIterator is a vector<aClass>.
    Assuming such an element exists, if you wanted to delete the element at 'location 1', you would use:
    Code:
    theClassV.erase(theClassV.begin() + 1);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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 can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. 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
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21