Reputation: 350
I want to add a pair to a vector of pairs. When I print the vector elements, I don't get the integers I input. Please help.
#include<iostream>
#include<vector>
#include<utility>
using namespace std;
int main()
{
vector<vector<unsigned long int> >matrix;
vector<pair<int,int> >pmat;
int L;
cin>>L;
int n1, n2;
pmat.resize(L);
for(int k = 0; k<L; k++)
{
cin>>n1>>n2;
pair<int,int> p = make_pair(n1,n2);
cout<<p.first<<p.second<<endl;
pmat.push_back(p);
}
for(int k = 0; k<L; k++)
{
cout<<pmat[k].first<<','<<pmat[k].second<<' ';
}
cout<<endl;
return 0;
}
Upvotes: 0
Views: 198
Reputation: 579
pmat.resize(L);
if vector in empty its going to initialize a vector pmat with size L then assign default values to vector so now pmat size is L with default values
for(int k = 0; k<L; k++)
{
cin>>n1>>n2;
pair<int,int> p = make_pair(n1,n2);
cout<<p.first<<p.second<<endl;
pmat.push_back(p);
}
then u r pushing values to pmat L times so the final size is 2*L
for(int k = 0; k<L; k++)
{
cout<<pmat[k].first<<','<<pmat[k].second<<' ';
}
here u r going to read from 0 to L-1 , it contains default values you can see your values from L-1 to 2L-1. so finally what i want say is use reserve instead of resize or pmat.resize(L); comment this line
Upvotes: 0
Reputation: 50667
Method 1: Delete this line:
pmat.resize(L);
You don't need to resize it in the first place as you do push_back()
when adding afterwards.
Method 2: Change the following line
pmat.push_back(p);
to
pmat[k] = p;
You can do resize()
in the first place, but after this, you should not use push_back()
when adding, just use pmat[k] = p
.
PS: you should not mix these two ways up. Always use one of them consistently.
Upvotes: 2
Reputation: 10355
Since you're using pmat.resize(L)
and L times pmat.push_back(...)
, you're ending up having stored 2 * L entries in your vector. However you're printing just the first half, index 0 to L - 1. The values you want are stored from index L to 2 * L - 1.
Just change pmat.resize(L)
to pmat.reserve(L)
.
Alternatively, you can use the resize(L)
, but to end up with L
entries, you need to store each input pair to pmat[k]
, hence you write pmat[k] = p;
.
As a rule of thumb, I recommend using the reserve
+ push_back
approach if you know how many elements you're going to add. The reason is, that resize
initializes the elements, while reserving just asserts that there will be enough space and no reallocation will be necessary with any following push_back.
Upvotes: 2
Reputation: 11973
If you print the size of the vector after reading the values, you will notice a small problem with your program:
./test
2
1 2
12
3 4
34
Size of the vector: 4
0,0 0,0
Huh? Even though I only entered 2 pairs, the size of the vector is 4. How did this happen?
If you look at the documentation for resize, it says
Resizes the container to contain count elements.
So even before you read any values, your vector will already contain 2 elements! Those will be default-constructed and therefore be 0. When you then push_pack
the elements you read in, those will land at the indices 2 and 3 in the vector, so the end vector has twice as much elements as you wanted (4 in this case). You only print out the first half, which are the zero values.
The solution is to use reserve instead of resize, which doesn't create the elements but only reserves space for them, or just delete the call to resize. Using reserve is more efficient though, because then the vector will only need to allocate memory once.
Upvotes: 0
Reputation: 637
You don't want to add more pairs after you allocated them. You can now directly access them.
Just use pmat[k] = p;
instead of pmat.push_back(p);
Upvotes: 1