I am trying to write 3 different types of algorithms which create arrays using vectors STL( 1-st lesson).
When creating vector a and c, size() seems to be correct, however for vector b size() is doubled. Why is that happening ?
#include <iostream>
#include <vector>
usingnamespace std;
int main()
{
vector <int> a;
vector <int> b;
vector <int> c;
int n;
cout<<" Enter number of elements in vector a "; cin>>n;
for(int i=0,m; i<n; i++)
{
cout<<"vector <int> a - element ["<<i<<"] ="; cin>>m;
a.push_back(m);
cout<<"a"<<a[i]<<endl;
}
cout<<"a size "<<a.size()<<endl;
cout<<" Enter number of elements in vector b "; cin>>n;
for(int i=0,m; i<n; i++)
{
cout<<"vector <int> b - element ["<<i<<"] ="; cin>>m;
// b.insert(b.begin(),m);
b.insert(b.begin()+i,m);
b.insert(b.end(),m);
cout<<"b"<<b[i]<<endl;
}
cout<<"b size "<<b.size()<<endl;
cout<<" Enter number of elements in vector c "; cin>>n;
for(int i=1,m; i<=n; i++)
{
c.resize(i);
cout<<"enter element["<<i<<"]"; cin>>m;
c[i]=m;
cout<<"c"<<c[i]<<endl;
}
cout<<"c size "<<c.size()<<endl;
return 0;
}
line 26 cout<<" Enter number of elements in vector b "; cin>>n;
Enter number of elements in the vector b 6
vector <int> b - element [0] =12
b12
vector <int> b - element [1] =223
b223
vector <int> b - element [2] =212
b212
vector <int> b - element [3] =32
b32
vector <int> b - element [4] =12
b12
vector <int> b - element [5] =1
b1
b size 12, obviously size should be 6, something is wrong in my code :S
Just had a thought. The ".insert" is better used to put something in an already populated vector. Although it does work the way that you are using it. It does seem like the hard way to do it.
Andy - what you said seems to be so appropriate, even the keyword "insert" means to put smthg into smtgh else, I guess that was just a practical experience for my HW (creating vectors using 3 different methods) .
vector <int> c;
cout<<" Enter number of elements in the vector c "; cin>>n;
for(int i=1, m; i<=n; i++)
{
c.resize(i);
cout<<"enter element["<<i<<"]"; cin>>m;
c[i]=m;
}
cout<<"c size "<<c.size()<<endl;
for(int i=1; i<=c.size(); i++)
{
cout<<c[i]<<' ';
}
cout<<endl;
when I am displaying values it doesn't show the array itself, still for push_back(), insert() it displays easily and correct, what could be the issue ?
vector elements, like array elements, start at 0, not 1. There is also no need to resize inside the loop. Just set the size once when the size is known:
#include <vector>
#include <iostream>
int main()
{
size_t n {};
std::cout << "Enter number of elements in the vector c ";
std::cin >> n;
std::vector<int> c(n);
for (int i = 0; i < n; ++i) {
std::cout << "Enter element [" << i << "] ";
std::cin >> c[i];
}
std::cout << "c size " << c.size() << '\n';
for (int i = 0; i < c.size(); ++i)
std::cout << c[i] << ' ';
std::cout << '\n';
}
#include <vector>
#include <iostream>
int main()
{
size_t n {};
std::cout << "Enter number of elements in the vector c ";
std::cin >> n;
std::vector<int> c(n);
for (size_t i {}; auto & e : c) {
std::cout << "Enter element [" << i++ << "] ";
std::cin >> e;
}
std::cout << "c size " << c.size() << '\n';
for (constauto& e : c)
std::cout << e << ' ';
std::cout << '\n';
}
Can be done that way and would work - but the overhead of re-allocations could be severe. Resizing should be done as few times as possible to avoid the overheads.
I guess the issue occurs because I was trying to use specifically resize inside the loop.
I can't think of any reason why that would cause an issue (apart from poor performance, as seeplus noted)? What are you thinking of when you say this?
To me, the problem you're seeing is far more likely to be that you're writing past the end of the vector on each iteration of the loop - for reasons seeplus has already explained.