vector

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
From what I can tell you're trying to add 'x' to the vector 'x' number of times?
yes
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.
Last edited on
I send you the whole code:
I really don't know how to show the elements of the vector:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<vector>

using namespace 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;
}
Yeah, that makes a difference. Line 8 is NOT how you declare a vector. Vectors should be declared like this:
 
std::vector<int> Vectorul;

Replace line 5 with the above code and from there you would use "Vectorul" as a global variable and delete line 8.

EDIT: Clarification.
Last edited on
But I used "using namespace std; "
And how do I show all the elements of the vector?
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.
Last edited on
Basically what I want to do is to read from the console an indefinite number of numbers and use them after for some calculations.
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.
Last edited on
I put the cin command in the for loop but it tells me that the x variable is not initialized.
int x=0;

for(int i = 0; i<x; i++){
cin>>x;
Vectorul.push_back(x);
}
Yeah, sorry about that. I thought I deleted that in time but I got a phone call so I rushed it :( my bad. See the post I "replaced" it with.
The problem is that is giving me the error that x is not initialized.
That code isn't working. I'll try to come up with something simple to understand and post it back here.
I wait.
Thank you.
That is the right code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<vector>

using namespace 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;
}
Ok, I see what you did. Good work I was having the hardest time with this for some reason.
Thank you.
Is working.
Now i have to figure out how to work with them.
Feel free to post questions. The reason I said not to write Line 5 in your code is because it complicates things by brining typedef into the equation.
Topic archived. No new replies allowed.