vector->at(i) ?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
mbkt
New poster
Posts: 3
Joined: Mon Feb 28, 2005 1:53 pm

vector->at(i) ?

Post by mbkt » Sat Mar 05, 2005 12:15 pm

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!

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sat Mar 05, 2005 12:59 pm

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.

mbkt
New poster
Posts: 3
Joined: Mon Feb 28, 2005 1:53 pm

Post by mbkt » Sat Mar 05, 2005 1:11 pm

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!

Aleksandrs Saveljevs
New poster
Posts: 39
Joined: Fri Nov 14, 2003 11:18 pm
Location: Riga, Latvia
Contact:

Post by Aleksandrs Saveljevs » Sat Mar 05, 2005 3:48 pm

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;

mbkt
New poster
Posts: 3
Joined: Mon Feb 28, 2005 1:53 pm

Post by mbkt » Sat Mar 05, 2005 7:26 pm

It works! Thanks a lot! :)

Post Reply

Return to “C++”