I have a problem inserting values in a vector.
I have the following syntax.
int x;
cin>>x;
for(int i = 0; i<x; i++)
Vectorul.push_back(x);
}
But something is wrong because it doesn't add me more than one element to the vector.
Can anyone tell me how to add more elements to the vector?
Thank you
Then with the exception of a missing '{' on the for loop your code looks good. What are you doing to check the contents of the vector?
EDIT: Just to be sure the code you have takes your input for 'x' and puts it into 'Vectorul' 'x' number of times. So if you entered say '7' then 'Vectorul' will hold '7' 7's. If you want to do something else please indicate so.
#include<iostream>
#include<vector>
usingnamespace std;
typedef vector<int> VECTORINT;
int main() {
VECTORINT Vectorul;
int x;
cin>>x;
for(int i = 0; i<x; i++){
Vectorul.push_back(x);
}
cout<<"The dimension of the vector is: " <<Vectorul.size()<<endl;
cout<<"The maximum dimension of the vector is: "<<Vectorul.max_size()<<endl;
cout<<"The capacity of the vector is : "<<Vectorul.capacity()<<endl;
Vectorul.reserve(1000);
cout<<endl<<"After allocating space for 1000 elements : " <<endl;
cout<<"The dimension of the vector is: " <<Vectorul.size() << endl;
cout<<"The maximum dimension of the vector is: "<<Vectorul.max_size()<<endl;
cout<<"The capacity of the vector is : " <<Vectorul.capacity()<<endl;
Vectorul.resize(2000);
cout<<endl<<"After allocating space for 2000 elements : ";
cout<<"The dimension of the vector is: " <<Vectorul.size()<<endl;
cout<<"The maximum dimension of the vector is: "<<Vectorul.max_size()<<endl;
cout<<"The capacity of the vector is : "<<Vectorul.capacity()<<endl;
system("pause");
return 0;
}
To show the elements in a Vector I like to use a simple for loop like this:
1 2
for(int i = 0; i < MyVector.size(); i++)
{std::cout << MyVector[i] << std::endl;}
This allows you to modify the size of the array, which is what vectors are for, without having to also go back and modify this line.
NOTE: The "std::" can of course be omitted since you're using the std namespace, but I put them in there to be clear about what you are doing.
EDIT: the "size()" member function of the vector class returns the number of elements in the vector, NOT the maximum position. So if you were to but i <= MyVector.size() you would be checking the memory address of datatypesize places past the vector which would be garbage usually.
EDIT: I deleted a post I had made without thinking. I'm sorry if you had already read that.
That would be trickey. You would need to have a conditional check to see when the user is done entering elements for the array in order to exist the loop. I'd say something like while(isdigit(x)){/*...CODE CODE CODE*/} would keep allowing the user to enter elements into the vector until a non numerical character is met.
#include<iostream>
#include<vector>
usingnamespace std;
typedef vector<int> VECTORINT;
int main() {
VECTORINT Vectorul;
int x;
int num;
cin>>num; //the number of numbers that must be entered
for(int i = 0; i<num; i++){cin>>x;
Vectorul.push_back(x);
}
cout<<"The dimension of the vector is: " <<Vectorul.size()<<endl;
cout<<"The maximum dimension of the vector is: "<<Vectorul.max_size()<<endl;
cout<<"The capacity of the vector is : "<<Vectorul.capacity()<<endl;
Vectorul.reserve(1000);
cout<<endl<<"After allocating space for 1000 elements : " <<endl;
cout<<"The dimension of the vector is: " <<Vectorul.size() << endl;
cout<<"The maximum dimension of the vector is: "<<Vectorul.max_size()<<endl;
cout<<"The capacity of the vector is : " <<Vectorul.capacity()<<endl;
Vectorul.resize(2000);
cout<<endl<<"After allocating space for 2000 elements : ";
cout<<"The dimension of the vector is: " <<Vectorul.size()<<endl;
cout<<"The maximum dimension of the vector is: "<<Vectorul.max_size()<<endl;
cout<<"The capacity of the vector is : "<<Vectorul.capacity()<<endl;
system("pause");//this is bad!
return 0;
}