Page 1 of 1

vector->at(i) ?

Posted: Sat Mar 05, 2005 12:15 pm
by mbkt
Hi, I have submited a solution which uses the function vector::at(int) and i got CE...

What am i doing wrong? this simple code causes a Compile Error:

Code: Select all

#include <vector>

int main(){
    vector<int>* vc = new vector<int>();
    return vc->at(0);
}

Code: Select all

 no matching function for call to `vector<int,allocator<int> >::at (int)

Help me plz!

Posted: Sat Mar 05, 2005 12:59 pm
by Krzysztof Duleba
LOL! Don't you understand "no matching function for call to `vector<int,allocator<int> >::at (int)"?
Let me explain it: in judge's g++ 2.95, there is no matching function for call to vector<>::at (int).
You have to use operator[] instead.

Posted: Sat Mar 05, 2005 1:11 pm
by mbkt
Yes, i understand it, i also tried using operator [] but it's a vector<>*, so i cannot call operator [], then i get CE too:

Code: Select all

class vector<int,allocator<int> >' used where a `int' was expected
There's some way i can use that operator in a pointer type?
(Sorry, but i'm quite new in C++...)

Thanks for helping!

Posted: Sat Mar 05, 2005 3:48 pm
by Aleksandrs Saveljevs
You can do it like this: "(*p)[1]". For example,

Code: Select all

vector<int>* p;
p=new vector<int>();
p->push_back(4);
p->push_back(8);
printf("%d\n", (*p)[1]);
delete p;

Posted: Sat Mar 05, 2005 7:26 pm
by mbkt
It works! Thanks a lot! :)