allocating a vector?

This is a discussion on allocating a vector? within the C++ Programming forums, part of the General Programming Boards category; my question is, how can i allocate a vector of ints? ive tried several options like Code: int *ptr; int ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    allocating a vector?

    my question is, how can i allocate a vector of ints?
    ive tried several options like

    Code:
    int *ptr;
    int elements = 10
    ptr = new vector<int> [elements];
    but that doesnt work

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    To create a vector of 10 ints, all set to 0:
    Code:
    std::vector<int> elements(10);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    yes, but i want a dynamic vector, like this:

    Code:
    int choise ;
    int *ptr ;
    cin  >> choise;
    ptr = new int [choise];
    but with a vector instead of an array

    ah ........ i just figured out that vector isnt a build in type.. im sorry!
    but i still dont know how to allocate
    Last edited by epidemic; 01-21-2007 at 09:39 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    but i want a dynamic vector
    A vector is a dynamic array. You might want to read SGI's Standard Template Library Programmer's Guide on vectors, or read cppreference.com's entry on vectors.

    Your example might be written using vectors as:
    Code:
    std::vector<int> choices;
    int choice;
    cin  >> choice;
    choices.push_back(choice);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    86
    k ty

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You should never want to create a dynamic anything. If you are forced to, e.g. when you want to create an array that is based on the size of some user input, then you have to take care of deleting your creation.

    Vectors take care of everything for you. They grow to whatever size you need, and you're not burdened with the responsibility of deleting them.

    You can certainly get a pointer to a vector if, say, you want to pass the vector to a function:
    Code:
    void someFunc(vector<int>* vec)
    {
    	vector<int>::iterator iter = vec->begin();
    	while(iter != vec->end())
    	{
    		cout<<*iter<<endl;
    		++iter;
    	}
    }
    
    int main()
    {
    	vector<int> v;
    	v.push_back(3);
    	v.push_back(-2);
    
    	vector<int>* ptr = &v;
    	someFunc(ptr);
    
    	return 0;
    }
    Last edited by 7stud; 01-21-2007 at 11:22 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Of course, the use of a pointer to a vector as in 7stud's example is not actually advisable since someFunc() could take a (const) reference instead, or have an iterator pair passed to it (or have a generic algorithm used in place of it, for that matter).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    86

    tnx guys

    its all clear now. i was comparing a vector to an array but these seem to be 2 diffrent things.

    this is my final program i made with your info's

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <sstream>
    #include <string>
    #include <new>
    #include <vector>
    #include <ctime>
    
    
    /* FUNCTION PROTOTYPES */
    /****************************************************************************/
    void Announcement  ();
    void GetInput      (std::vector <int> &);
    void Fillvector    (std::vector <int> &);
    void Printvector   (std::vector <int> &);
    
    
    //___________________________________________
    
    /****************************************************************************/
    
    
    int main ()
    {
      std::vector <int> arr ;
      Announcement  (); // printing announcements.
      GetInput      (arr); // getting the Array size.
      Fillvector    (arr); // filling the Array with random numbers
      Printvector   (arr); // print a vector.
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /* FUNCTION BODIES */
    /****************************************************************************/
    /****************************************************************************/
    void Announcement ()
    {
         std::cout <<"Copyright @ Epidemic corperation z0mG .\n\n";
                
    
    }
    /****************************************************************************/
    void GetInput      (std::vector <int> &arr)
    {
      long unsigned    Count = 0;
           int    Check = 0;
      std::string Safe  = "\0";
      std::cout <<"\nHowmany items do u want in your vector?"<< std::endl;
      std::cin  >> Count;
      std::cin.clear();
    
      while (Check != 1  )
      {
        try
        {
    
          arr.resize(Count);
          std::cout <<"Allocated "<< Count << " items with Succes!"<< std::endl;
          Check = 1;
        }
        catch (std::bad_alloc &memoryAllocationException )
        {
          std::cin.clear();
    
          std::cerr <<"Exception occurred:"<< std::endl;
          std::cerr << memoryAllocationException.what() << std::endl;
          std::cerr <<"you do not have enough memory, please pick a smaller size\n"<<std::endl;
    
          std::cin >> Count;
    
        }
      }
    
    
    
    }
    /****************************************************************************/
    void Fillvector (std::vector <int> &arr)
    {
      unsigned int minimum = 0;
      unsigned int maximum = 0;
      std::cout <<"Please enter the range where-in the generator will produce\n"
                <<"\"RANDOM\" numbers." << std::endl;
      std::cin  >> minimum;
      std::cin  >> maximum;
    
      std::srand (std::time(0));
      for (unsigned int x = 0; x<arr.size(); x++)
      {
        arr[x] = std::rand () % maximum + minimum;
      }
    }
    /****************************************************************************/
    void Printvector (std::vector <int> &arr)
    {
    
      for (unsigned int x = 0; x < arr.size(); x++)
      {
        std::cout <<"\t"<<arr[x];
        if (x % 10 == 0 )
        {
          std::cout <<"\n";
        }
      }
    }
    
    /****************************************************************************/
    
    
    
    /****************************************************************************/

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    903
    Why did you include sstream and new ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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