my question is, how can i allocate a vector of ints?
ive tried several options like
but that doesnt work :(Code:int *ptr;
int elements = 10
ptr = new vector<int> [elements];
Printable View
my question is, how can i allocate a vector of ints?
ive tried several options like
but that doesnt work :(Code:int *ptr;
int elements = 10
ptr = new vector<int> [elements];
To create a vector of 10 ints, all set to 0:
Code:std::vector<int> elements(10);
yes, but i want a dynamic vector, like this:
but with a vector instead of an arrayCode:int choise ;
int *ptr ;
cin >> choise;
ptr = new int [choise];
ah ........ i just figured out that vector isnt a build in type.. im sorry!
but i still dont know how to allocate
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.Quote:
but i want a dynamic vector
Your example might be written using vectors as:
Code:std::vector<int> choices;
int choice;
cin >> choice;
choices.push_back(choice);
k ty
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;
}
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).
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";
}
}
}
/****************************************************************************/
/****************************************************************************/
Why did you include sstream and new ?