invalid-iterator-argument.
Feb 8, 2013 at 3:56pm UTC
Hi, I am trying to create a program that will take 10 numbers and then print the sum of the all two adjacent numbers and then sum of 1st and last,2nd and 2nd last;here is the 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int > input;
for (int i=0;i!=10;i++)
{
int x;
cin>>x;
input.push_back(x);
}
for (auto it=input.begin();it!=input.end()-1;it++)
{
int k;
k=*it+(*(it+1));
cout<<k<<" " <<endl;
auto j=input.end()-it;
cout<<*it+*j<<" " <<endl;
}
}
.. but at the 2nd output the compiler is giving the error invalid type argument of unary '*'. where am I wrong?
Feb 8, 2013 at 4:07pm UTC
input.end()-it
returns the distance of it from the end of the vector which is of type std::iterator_traits<std::vector<int >::iterator>::difference_type
(typedef of some integer type).
Feb 8, 2013 at 4:23pm UTC
Thanks, now I have re wriitten the code and it works fine. its a bit long though!
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>
#include <string>
using namespace std;
int main()
{
vector<int > input;
for (int i=0;i!=10;i++)
{
int x;
cin>>x;
input.push_back(x);
}
auto j=input.end();
for (auto it=input.begin();it!=input.end()-1;it++)
{
int k;
k=*it+(*(it+1));
cout<<k<<" " ;
if (j !=input.begin())
{
j--;
cout<<*it+*j<<" " ;
}
}
}
Topic archived. No new replies allowed.