I was solving a simple problem on SPOJ called FASHION... its easy. I just wanted to get hang of iterators. But then I am running into a peculiar problem.
This is my code,
#include <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
using namespace std;
int hotMax(vector<int> &, vector<int> &);
int main()
{
int iter,m_f;
scanf("%d", &iter);
vector<int> a,b;
while(iter--){
scanf("%d", &m_f);
a.resize(m_f);
b.resize(m_f);
vector<int>::iterator it;
for(it = a.begin(); it != a.end(); it++){
scanf("%d", it);
}
for(it = b.begin(); it != b.end(); it++){
scanf("%d", it);
}
printf("%d\n", hotMax(a,b));
}
return 0;
}
int hotMax(vector<int> &a, vector<int> &b){
std::sort(a.begin(), a.end());
std::sort(b.begin(), b.end());
int result = 0;
vector<int>::iterator it1,it2;
for(it1 = a.begin(),it2 = b.begin(); it1 != a.end(); it1++,it2++){
result+= (*it1) * (*it2);
}
return result;
}
I get this warning on Code-blocks,
/home/harshal/c++ tutorial/SAMER08F/main.cpp|22|warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘std::vector::iterator {aka __gnu_cxx::__normal_iterator >}
/home/harshal/c++ tutorial/SAMER08F/main.cpp|25|warning: format ‘%d’ expects argument of type ‘int*’, but argument 2 has type ‘std::vector::iterator {aka __gnu_cxx::__normal_iterator >}’ [-Wformat=]|
These are corresponding to the scanf("%d", it); but then it runs perfectly in codeblocks,
It gives a SIGILL in ideone and also on SPOJ.
When i replace the scanf with cin>> *it, it runs perfectly on SPOJ and ideone.
I shall be very thankful if you could give me an insight on this. I tried to just place it in the scanf, coz it's kind of a generalized pointer to the vector.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire