question about C++ STL set

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

Moderator: Board moderators

Post Reply
Learning poster
Posts: 90
Joined: Mon Feb 16, 2004 8:53 pm
Location: Bangalore INDIA

question about C++ STL set

Post by jagadish » Tue Sep 06, 2005 4:34 pm

i am trying to make a set of objects of type two and go like this :

Code: Select all

struct two {
   int     a,b;
int main(){
    set<two> a ;
    two t;
    t.a=1; t.b=3;
    set<two>::iterator  i;
    for(i=a.begin() ; i!=a.end() ; i++)
        cout << a[i].a << a[i].b ;
    return 0;
i get error message at a.insert(t) which i'm not able to comprehend.... plz give me the correct idea to implement this.
if u can think of it .. u can do it in software.

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

Post by Krzysztof Duleba » Tue Sep 06, 2005 4:51 pm

You have to specify a comparator. For instance, you can define operator< in class two.

You could also use std::pair instead of two. Here it would look like

Code: Select all

set<pair<int,int> > a;

Post Reply

Return to “C++”